If I google for "SIGCHLD" the first sentence in the results is literally this: "The SIGCHLD signal is sent to the parent of a child process when it exits, is interrupted, or resumes after being interrupted."
Basically the program is forking N times -- that's what the first for loop is doing -- and then waiting on each of the N processes to exit. In addition, the first program installs a signal handler for SIGCHLD so that whenever that signal is received, 3 is added to val...
If you want to compile and run this, all you need is some header files. printf needs stdio.h, SIGCHLD and signal are in signal.h, and exit is in stdlib.h. You may need others as well, e.g. waitpid is supposed to require sys/types.h and sys/wait.h, and fork is supposed to require unistd.h, etc. The first three were all I needed on my particular machine since some headers include others, but might as well be safe:
Code:
$ cat foo.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define N 4
int val = 9;
void handler(sig) { val += 3; return;}
int main() {
pid_t pid;
int i;
signal(SIGCHLD,handler);
for (i=0;i<N;i++) {
if ((pid =fork()) == 0) {
val -= 3;
exit(0); } }
for (i=0;i<N;i++) {
waitpid(-1,NULL,0); }
printf("val = %d\n",val); }
$ gcc foo.c -o foo
$ ./foo
val = 21
$
Looks like 21 is one possible output.