如何正确使用dup2 / close来连接这三个进程?

时间:2012-02-26 22:23:10

标签: c fork pipe dup2

我正在尝试正确连接三个进程,以便允许它们之间的进程间通信。我有一个进程,scan,它接受父进程STDIN,然后处理流中的单词。如果字长是奇数,则将其发送到一个进程,如果是偶数,则将其发送给另一个进程。这些过程应该通过STDIN(我假设)接受这些话,然后通过STDOUT将一些信息输出回扫描过程。偶数/奇数的STDOUT应重定向到扫描器,然后读取(使用读取)然后输出/处理字。这是一个学术练习,而不是一个实践练习。以下是它的图片:

Pipe setup

这是我的代码目前的样子。问题是我不确定要复制什么以及要关闭什么。一旦我弄明白,我应该好好去!任何建议将不胜感激。

文件描述符:

int scannertoeven[2]; int scannertoodd[2];
int eventoscanner[2]; int oddtoscanner[2];
//Pipe stuff here (ommitted)

代码:

 //Create the child processes
 if ((scanner_pid = fork())  == 0) {

      //We need the scanner pid so even and odd can send signals to it
      char pidofparent[sizeof(getpid())];
      sprintf(pidofparent, "%i", getpid());

      //Even stuff
      if ((even_pid = fork()) == 0) {
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Not sure which ones to close
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);


           //Correct?
           close(STDIN_FILENO);
           dup2(scannertoeven[0], STDIN_FILENO);
           close(STDOUT_FILENO);
           dup2(eventoscanner[1], STDOUT_FILENO);


          if(execl("./evenodd", "even", pidofparent,  NULL ) == -1) {
               printf("execl Error!");
               exit(1);
          }

     //Odd Stuff
     } else if ((odd_pid = fork()) == 0){
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);

           //Not sure which ones to close
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Correct?
           close(STDIN_FILENO);
           dup2(scannertoodd[0], STDIN_FILENO);
           close(STDOUT_FILENO);
           dup2(oddtoscanner[1], STDOUT_FILENO);

          if(execl("./evenodd", "odd", pidofparent,  NULL ) == -1) {
               printf("execl Error!");
               exit(1);
          } 


      //Actual Scanner
      } else {

           // Not sure which ones to close- this is very wrong
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Not sure what to dup either
           dup2(scannertoodd[1], STDOUT_FILENO);
           dup2(scannertoeven[1], STDOUT_FILENO);
           if(execl("./scanner", "scanner", stoeven, stoodd, eventos, oddtos, NULL ) == -1) {
                printf("execl Error!");
                exit(1);
           }

           //Wait twice here, or three times in main?
           waitpid(odd_pid, &status2, 0);
           waitpid(even_pid, &status3, 0);
      }
 //Main
 } else {
      //Close Pipes
      close(scannertoodd[0]); close(scannertoeven[0]); close(eventoscanner[0]); close(oddtoscanner[0]);
      close(scannertoodd[1]); close(scannertoeven[1]); close(eventoscanner[1]); close(oddtoscanner[1]);
      //Wait for children to finish
      waitpid(scanner_pid, &status1, 0);
      printf("Done\n");
 }

1 个答案:

答案 0 :(得分:3)

不确定逻辑。但是你使用dup2的方式不对。

“均匀”过程中的以下代码:

close(STDIN_FILENO);
dup2(scannertoeven[0], STDIN_FILENO);
close(STDOUT_FILENO);
dup2(eventoscanner[1], STDOUT_FILENO);

应该是:

dup2(scannertoeven[0], STDIN_FILENO);
// You should close scannertoeven[0], not STDIN. After this dup2, the even
// process will receive input from scannertoeven[0]
close(scannertoeven[0]);
// Note the the scannertoeven[0] is not "really" closed, just that the file
// is "attached" to STDIN

dup2(eventoscanner[1], STDOUT_FILENO);
// Same as above. After this dup2, all the even process's output will go
// to eventoscanner[1]
close(eventoscanner[1]);

与“奇怪”过程相同。

以下是an example of dup2,供您参考。