2012-01-20 3 views
0

가능/올바를 수 있습니까? "자식"프로세스의 fd1 [1]에서 쓰기를 수행하면 "father"프로세스의 fd2 [0]을 읽을 수 있습니까?포크 후 파이프 만들기

main(){ 
    pid_t pid; 
    pid = fork(); 
    if(pid <0){ 
     return -1; 
    } 
    if(pid == 0){ 
     int fd1[2]; 
     int fd2[2]; 
     pipe(fd1); 
     pipe(fd2); 
     close fd1[1]; 
     close fd2[0]; 
     //writes & reads between the fd1 pipes 
     //writes & reads between the fd2 pipes 
    }else{ 
     int fd1[2]; 
     int fd2[2]; 
     pipe(fd1); 
     pipe(fd2); 
     close fd1[1]; 
     close fd2[0]; 
     //writes & reads between the fd1 pipes 
     //writes & reads between the fd2 pipes 
    } 
} 
+3

귀하의 궁금한 점은 무엇입니까? 나는 너 자신을 시험 할 수없는 어떤 것 같지 않아. – Tudor

답변

4

아니, 프로세스 사이의 통신에 사용되는 파이프 작성해야 전에 fork() (그렇지 않으면, 당신은 읽기와 쓰기 끝이 다른 프로세스에 의해 사용되어야하기 때문에,이를 통해 보낼 수있는 쉬운 방법이 없습니다) .

가 소켓에 밴드 메시지의 부족으로 프로세스 간 파일 기술자를 보낼 더러운 트릭이있다,하지만 난 정말

3

당신은을 분기하기 전에 설정에 파이프 필요 못생긴 세부 사항을 잊어 버렸습니다 .

int fds[2]; 

if (pipe(fds)) 
    perror("pipe"); 

switch(fork()) { 
case -1: 
    perror("fork"); 
    break; 
case 0: 
    if (close(fds[0])) /* Close read. */ 
     perror("close"); 

    /* What you write(2) to fds[1] will end up in the parent in fds[0]. */ 

    break; 
default: 
    if (close(fds[1])) /* Close write. */ 
     perror("close"); 

    /* The parent can read from fds[0]. */ 

    break; 
}