1

プロセス間通信を可能にするために、3つのプロセスを適切に接続しようとしています。親のSTDINを取得し、ストリーム内の単語を処理するスキャナーという1つのプロセスがあります。単語の長さが奇数の場合は1つのプロセスに送信され、偶数の場合は別のプロセスに送信されます。これらのプロセスは、STDIN(私が推測する)を介してこれらの単語を受け取り、STDOUTを介してスキャナープロセスに情報を出力する必要があります。偶数/奇数のSTDOUTをスキャナーにリダイレクトする必要があります。スキャナーは、(読み取りを使用して)読み取り、単語を出力/処理します。これは学術的な演習であり、実践的な演習ではありません。写真は次のようになります。

パイプのセットアップ

これが私のコードが現在どのように見えるかです。問題は、何を複製し、何を閉じるかが正確にわからないことです。私がそれを理解したら、私は行ってもいいはずです!何かアドバイスをいただければ幸いです。

ファイル記述子:

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");
 }
4

1 に答える 1

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]);

「奇数」プロセスと同じです。

参考までに、dup2の例を次に示します。

于 2012-03-10T13:55:32.300 に答える