| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | |
| 5 | #include <signal.h> |
| 6 | #include <sys/wait.h> |
| 7 | |
| 8 | pid_t children[5]; |
| 9 | |
| 10 | void announce_death() { |
| 11 | |
| 12 | printf("i'm pid #%lu and i died.\n", (long) getpid()); |
| 13 | } |
| 14 | |
| 15 | int main(int argc, char *argv[]) { |
| 16 | |
| 17 | pid_t our_pid; |
| 18 | our_pid = getpid(); |
| 19 | |
| 20 | for(int i = 0; i < 5; i++) { |
| 21 | if((children[i] = fork()) == 0) |
| 22 | goto child; |
| 23 | else |
| 24 | sleep(1); |
| 25 | } |
| 26 | |
| 27 | for(int i = 0; i < 5; i++) { |
| 28 | |
| 29 | kill(children[i], SIGTERM); |
| 30 | wait(0); |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | |
| 35 | child: |
| 36 | printf("i'm pid #%lu!\n", (long)getpid()); |
| 37 | atexit(announce_death); |
| 38 | exit(0); |
| 39 | } |