8

複数の I/O パイプ (プロセスごとに 1 つのパイプ) を使用して最終結果を取得する mapreduce プログラムを作成しています。プロセスの作成に問題があります。具体的には、次のエラーが発生します。

wait error: Interrupted system call

これはプロセスを生成する私のコードです:

while (values[inc]!=NULL) //provided array of text lines
{
    if ((pid = fork()) == -1) {
        perror("fork error");
        exit(EXIT_FAILURE);
    }    

    else if (pid == 0) {             /* start of child process      */
        printf("Child process...\n");
        /* pipes[inc][1] is a file descriptor to which myMap writes some data
           using the write() system call
           mr is a struct that holds other function pointers */
        mr->myMap(pipes[inc][1],values[inc]); 
        exit(0);
    }
    else {                           /* start of parent process     */
        printf("Parent process...\n");

        if ((wpid = wait(&status)) == -1)
        /* Wait for child process.      */  
            perror("wait error");
        else {                       /* Check status.                */
            if (WIFSIGNALED(status) != 0)
                printf("Child process ended because of signal %d\n",
                       WTERMSIG(status));
            else if (WIFEXITED(status) != 0)
                printf("Child process ended normally; status = %d\n",
                       WEXITSTATUS(status));
            else
                printf("Child process did not end normally\n");
        }
        //close(fd[1]);

        printf("Parent process ended\n");
    }
    inc++;
}

この後、1つのスレッドを作成しています

pthread_t newThread;
pthread_create(&newThread,NULL,threadFunction,values);
pthread_join(newThread,NULL);

threadFunction は select() 関数を使用して、読み取りの準備ができているファイル記述子を見つけ、それを読み取り、データを辞書に入れます。

フォーム gdb デバッガーを実行すると、プログラムは次のように出力します。

Parent process...
Child process...
wait error: Interrupted system call
Parent process ended
Parent process...
Child process ended normally; status = 0
Parent process ended
Parent process...
Child process...
Child process...
wait error: Interrupted system call
Parent process ended

問題を解決する方法がわかりません。助言がありますか?

ありがとう!

4

1 に答える 1

11

呼び出しをループに入れwait()、エラー(-1)が返された場合errno == EINTRは、ループを続行する必要があります。その他のエラーは実際のエラーであり、そのように扱う必要があります。

プロファイリングタイマーのようなものは、シグナルがプロセスに送信される原因となる可能性がありますが、おそらく中断を引き起こすシグナルはですSIGCHLD。これは、ご存知のように、子プロセスが状態を変更したときに呼び出されます。

編集:わかりました、私はコードで答えを書きます:

do
{
    wpid = wait(&status);
}
while (wpid == -1 && errno == EINTR);
if (wpid == -1)
{
    perror("wait error");
    return -1;
}
else
{
    // we have wait status
    ...
}
于 2012-04-15T08:44:30.853 に答える