본문 바로가기

컴퓨터

dup( ), dup2( ) int dup (int filedes); int dup2 (int filedes , int filedes2); •기능: 사용 중인 파일 디스크립터를 복사 •리턴 값: 성공하면 할당 받은 파일 디스크립터 번호, 실패하면 -1 –dup( ) 함수는 할당 가능한 가장 작은 번호를 리턴한다. –dup2( ) 함수는 filedes2를 리턴한다. •파일 테이블의 해당 항목의 참조 계수를 1 증가 시킨다. 파일 디스크립터 항목의 플래그 (FD_CLOEXEC)는 초기값 (0) 으로 설정된다 •dup2( ) 의 기능 –dup2(fd, STDIN_FILENO) : fd를 표준 입력으로 redirection –dup2(fd, STDOUT_FILENO) : fd를 표준 출력으로 redirection –dup2(fd, STDE.. 더보기
Dup2 를 이용한 Redirection 과 원상복귀 Redirection 이란 어떤 실행에 대한 결과나 입력을 파일에 저장하거나 파일에서 읽어오는 거로써 Unix 계열의 shell 에서 ?>./a.out > b.txt 혹은 ?>cat < a.txt 이런 것을 말하는 것이다. c에서 dup2 를 이용하면 Terminal 에 관한 input 과 output 을 쉽게 redirection 을 할 수 있다 원형은 int dup2(int oldfd, int newfd) 그 input 과 output 의 fd는 stdin = 0 , stdout = 1 이다. output 을 파일로 redirection 하기 위해선 int fd = open("a.txt", O_WRONLY | O_CREAT); dup2(fd, 1); close(fd); printf("HI"); 하면 .. 더보기
Socket inheritance with fork/dup2/exec Hi, I am redirecting the stdout of a child process to a socket via the standard fork/dup2/exec paradigm and then reading and displaying the output. This works fine if the exec'd child process is compiled using gcc under cygwin. However, it fails with an "Invalid file handle" error when compiled using VC8 under windows. I've included both the parent and child code below. I am running cygwin 1.5.2.. 더보기
간단한 pipe()에 대한 소스 #include #include #include main() { int fds[2]; pipe(fds);/* Create the pipe */ /* First child reconnects stdin to downstream end of pipe and closes the upstream end */ if (fork() == 0) { dup2(fds[0], 0); close(fds[1]); execlp("sort", "sort", 0); } /* Second child reconnects stdout to upstream end of pipe and closes the downstream end */ else if (fork() == 0) { dup2(fds[1], 1); close(fds[0]); ex.. 더보기
dup2() 함수 원형 [1] #include int dup2(int fildes, int fildes2); [4] #include int dup2(int oldhandle, int newhandle); 설명 dup2(새로 지정할 file descriptor 번호, 입출력 핸들 종류) newhandle이 가리키고 있는 file descriptor를 oldhandle로 바꾼다. [4] dup2는 유닉스 시스템 III하에서는 실행되지 않는다. dup2는 원래의 파일핸들과 같은 내용 새로운 파일핸들을 생성한다. ■ 동일한 파일 열기나 장치 ■ 동일한 파일 포인터 ■ 동일한 액세스 모드(읽기, 쓰기,읽기/쓰기) dup2는 newhandle값의 새로운 파일핸들을 생성한다. dup2가 호출되었을 때 newhandle과 연관된 .. 더보기
dup2 기능 파일핸들(oldhandle)을 존재하는 파일핸들(newhandle)로 복사한다. 문법 #include int dup2(int oldhandle, int newhandle); DOS UNIX Windows ANSI C C++ only ■ ■ ■ 주석 dup2는 유닉스 시스템 III하에서는 실행되지 않는다. dup2는 원래의 파일핸들과 같은 내용 새로운 파일핸들을 생성한다. ■ 동일한 파일 열기나 장치 ■ 동일한 파일 포인터 ■ 동일한 액세스 모드(읽기, 쓰기,읽기/쓰기) dup2는 newhandle값의 새로운 파일핸들을 생성한다. dup2가 호출되었을 때 newhandle과 연관된 파일이 열려있으면 파일은 닫혀진다. newhandle,과 oldhandle은 creat, open,dup,dup2 등의 .. 더보기
fork() execl() wait() - 자료 출처 : www.joinc.co.kr * fork는 자식프로세스를 만들기 위해서 사용되는 프로세스 생성기이다. #include pid fork(void); 실패할 경우 -1을 성공할 경우 자식 프로세스는 0, 부모 프로세스는 이외의 값이 리턴된다. * execl() : 현재 프로세스 이미지를 새로운 프로세스 이미지로 바꾼다. #include int execl(const char *path, const char *arg, ...); 처음 인자 : 파일의 경로 두번째 부터의 인자 : 파일의 경로에 넘겨줄 인자 마지막 인자 : 널(0, NULL)값을 써주어야 한다. * wait() : 자식 프로세스 종료를 기다린다. #include #include pid_t wait(int *status); 자식프로.. 더보기
fork 함수와 wait 함수의 이해! 자식프로세스의 시작.. 그리고 끝! fork함수는 fork함수를 호출하는 순간! 자식 프로세스가 생성이 됩니다. 그래서 간편하게 프로세스를 만들때 fork를 씁니다. 그런데... 언제 끝나는지 알 수 있을까요? ^.^ 뭐 안다면 아는 방법은 많지만... 이번에는 wait를 써서 알아볼까 합니다. 1 #include 2 #include 3 #include 4 5 int main(int argc, char **argv) { 6 pid_t pid; 7 int data = 10; 8 int status = -111; 9 10 if (data == 10) 11 { 12 pid = fork(); //fork 함수 호출!! 자식프로세스에서 이 아래의 내용이 실행됩니다! 13 if (pid == 0) //자식프로세스는 pid가 0입니다. 14 { 15 .. 더보기