views:

85

answers:

1

how can something print 3 times when it only goes the printing code twice? I'm coding in C and the code is in a SIGCHLD signal handler I created.

void chld_signalHandler() {
 int pidadf = (int) getpid();
 printf("pidafdfaddf: %d\n", pidadf);

 while (1) {
  int termChildPID = waitpid(-1, NULL, WNOHANG);

  if (termChildPID == 0 || termChildPID == -1) {
   break;
  }

  dll_node_t *temp = head;
  while (temp != NULL) {
   printf("stuff\n");
   if (temp->pid == termChildPID && temp->type == WORK) {
    printf("inside if\n");

    // read memory mapped file b/w WORKER and MAIN
    // get statistics and write results to pipe
    char resultString[256];

    // printing TIME
    int i;
    for (i = 0; i < 24; i++) {
     sprintf(resultString, "TIME; %d ; %d ; %d ; %s\n",i,1,2,temp->stats->mboxFileName);
     fwrite(resultString, strlen(resultString), 1, pipeFD);
    }

    remove_node(temp);
    break;
   }
   temp = temp->next;
  }
  printf("done printing from sigchld \n");
 }
 return;
}

the output for my MAIN process is this:

MAIN PROCESS 16214 created WORKER PROCESS 16220 for file class.sp10.cs241.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld 
MAIN PROCESS 16214 created WORKER PROCESS 16221 for file class.sp10.cs225.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld 

and the output for the MONITOR process is this:

MONITOR: pipe is open for reading
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs241.mbox
MONITOR: end of readpipe 

( I've taken out repeating lines so I don't take up so much space )

Thanks, Hristo

+1  A: 

From the small amount of information we have...

  1. The main process creates worker process with pid = 16220
  2. Worker process 16220 runs and terminates
  3. The signal handler runs and apparently the second node in the "head" list has a record for process id 16220 ("stuff" prints twice and "inside if" prints once).
  4. The main process creates worker process with pid = 16221
  5. Worker process 16221 runs and terminates
  6. The signal handler runs and apparently the second node in the "head" list has the record for process id 16221 ("stuff" prints twice and "inside if" prints once).

That is about all we can glean from the data you have provided. If you were to pass a stat parameter for to waitpid you could see why the worker processes terminated by printing out termChildPID and the termination reason in the handler.

If your question is why does "stuff" print twice then take a look at what "head" points to.

daddyman
Thank you for your response. I figured out my problem and fixed it.
Hristo