2012-04-15 11 views
8

Sto scrivendo un programma di mapreduce che utilizza più pipe di I/O (un pipe per processo) per ottenere risultati finali. Sto avendo un problema con la creazione dei processi. In particolare, sto ottenendo il seguente errore:fork() - più chiamate di sistema e di processo

wait error: Interrupted system call 

Questo è il mio codice che genera processi:

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

Dopo questo, io sono la creazione di un thread

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

Il threadFunction utilizza select () per scoprire quale file descriptor è pronto per essere letto e letto e mette i dati in un dizionario.

Durante l'esecuzione del debugger gdb forma, l'output del programma:

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 

non so come risolvere il problema. Eventuali suggerimenti?

Grazie!

risposta

9

È necessario mettere la chiamata wait() in un ciclo e se restituisce un errore (-1) e errno == EINTR continuare il ciclo. Qualsiasi altro errore è un errore reale e dovrebbe essere trattato come tale.

Cose come timer profiling possono causare segnali da inviare al processo, tuttavia il segnale probabilmente causando l'interruzione è SIGCHLD, che come si sa è chiamato quando un processo figlio cambia stato.

EDIT: OK, io scriverò la risposta in codice:

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

non sono sicuro di quello che vuoi dire. Si prega di elaborare. – Krzysiek

+1

@Rafcio Ho aggiornato la mia risposta. – trojanfoe

+1

Grazie. Ha senso! :) – Krzysiek

Problemi correlati