1

The code written below is to handle the received signals and reap the zombies by the parent process "from System Programming course of CMU".

Q1. what is the rule of -1 "the first arg of the waitpid function"? should not we pass the pid of the zombie we are reaping instead?

Q2. For the loop here, does it check all zombies each time a signal received by any precedent zombie?

int ccount = 0;
void child_handler2(int sig)
{
int child_status;
pid_t pid;
while ((pid = waitpid(-1, &child_status, WNOHANG)) > 0) {
   ccount--;
   safe_printf("Received signal %d from process %d\n",sig, pid);
 }
}

void fork14()
have sent this signal
{
pid_t pid[N];
int i, child_status;
linux> ./forks 14
ccount = N;
signal(SIGCHLD, child_handler);
for (i = 0; i < N; i++)
   if ((pid[i] = fork()) == 0) {
      sleep(1); /* deschedule child */
      exit(0); /* Child: Exit */
   }

while (ccount > 0)
pause(); /* Suspend until signal occurs */
}
Hazem Alabiad
  • 1,032
  • 1
  • 11
  • 24
  • @PatrickArtner Thanks, I got it. It means any child process. How about Q2? – Hazem Alabiad Jan 14 '18 at 16:29
  • This: https://stackoverflow.com/questions/33508997/waitpid-wnohang-wuntraced-how-do-i-use-these might help you, it explains about waitpid and WNOHANG ... what does `have sent this signal` do in your sourcecode? – Patrick Artner Jan 14 '18 at 16:31
  • @PatrickArtner thanks again :-) I did not get them when I first googled the subjet. – Hazem Alabiad Jan 14 '18 at 16:33
  • *In this of System programming course of CMU* Ouch. I wish people teaching C wouldn't be so sloppy. Given `ccount = N;` and `N != 0`, `while (ccount > 0)` can be replaced with an infinite loop by the compiler. – Andrew Henle Jan 14 '18 at 16:37
  • If you use `waitpid()` to wait for a specific child that has already been reaped, it returns immediately with an error indication (ECHILD, though the interpretation of 'no child processes' becomes 'the child that you tried waiting for doesn't exist'). It also returns with an error if the PID waited for isn't a child of this process, etc. – Jonathan Leffler Jan 14 '18 at 21:02
  • a `zombie` process is one that is no longer in the process chain. Therefore, it is still sucking CPU cycles, but is not visible to any other functions. So the only way to kill that process is to reboot the computer. – user3629249 Jan 17 '18 at 03:49

1 Answers1

0

Q1. "-1" means to check for all child processes.

Q2. when a child process dies, Kernel sends a message to its parent to reap it. And because the nature of signals receiving which cannot be queued "at most one can be received at a time" which may lead to discarding some later signals if received before the handler finishes handling the previous signal, and may not. So, we can't make sure that we will have N zombies at that time as some of them may have been handled automatically by the handler. That is why at each time we reap a zombie we check if there is another zombie to reap and reap it or (them). So, checking here is to prevent waiting for a child which has been reaped already to save the parent from freezing "if we let it waits for a reaped zombie".

Hazem Alabiad
  • 1,032
  • 1
  • 11
  • 24