2012-01-20 18 views
0

これは可能ですか正しいですか?私が "child"プロセスの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; 
} 
関連する問題