0

Would someone please explain to me what this code does? In particular I don't understand the part with the signal handling.

void sigchld_handler(int signum) {
 printf("A\n");
}
int main(void) {
 printf("X\n");
 new_action.sa_handler = sigchld_handler;
 new_action.sa_flags   = 0;
 sigaction(SIGCHLD, &new_action, NULL);
 if(fork() == 0) {
  sleep(2);
  printf("B\n");
  sleep(2);
  printf("C\n");
 } else {
  sleep(2);
  printf("D\n");
  sleep(6);
  printf("E\n");
 }
 return EXIT_SUCCESS;
}

I tried out the code and the result is:

X

D // after 2 seconds

B // after 2 seconds

C // after 4 seconds

A // after 4 seconds

E // after 4 seconds

4

0 回答 0