2012-04-15 3 views
8

최종 결과를 얻기 위해 여러 개의 I/O 파이프 (프로세스 당 하나의 파이프)를 사용하는 mapreduce 프로그램을 작성 중입니다. 프로세스를 만드는 데 문제가 있습니다. 특히, 나는 다음과 같은 오류를 얻고 것은 : 나는이 쓰레드가 선택 사용하는 하나 개의 스레드fork() - 여러 프로세스와 시스템 호출

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

를 생성하고이 후

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++; 
} 

(:

wait error: Interrupted system call 

이 프로세스를 생성합니다 내 코드입니다) 함수를 사용하여 읽을 준비가 된 파일 설명자를 찾고이를 읽고 사전에 데이터를 넣습니다.

형태 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 

내가 문제를 해결하는 방법을 모르겠어요. 어떤 제안?

감사합니다.

답변

9

wait() 호출을 루프에 넣고 오류 (-1)를 반환하고 errno == EINTR 루프를 계속 수행해야합니다. 다른 오류는 실제 오류이므로이를 그대로 처리해야합니다. 신호가 발생할 수 프로파일 링 타이머와 같은

상황은 그러나 신호가 아마도 방해를 일으키는 원인이되는 프로세스에 전송되도록하면 상태을 자식 프로세스를 변경 때 호출되는 알고있는 SIGCHLD이다.

편집 : OK, I 코드에 답을 쓸 것이다 :

do 
{ 
    wpid = wait(&status); 
} 
while (wpid == -1 && errno == EINTR); 
if (wpid == -1) 
{ 
    perror("wait error"); 
    return -1; 
} 
else 
{ 
    // we have wait status 
    ... 
} 
+0

난 당신이 무슨 뜻인지 모르겠어요. 제발 좀 더 자세히 설명해주세요. – Krzysiek

+1

@Rafcio 답변을 업데이트했습니다. – trojanfoe

+1

감사합니다. 말된다! :) – Krzysiek