| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <time.h> |
| 14 |
#include <signal.h> |
| 15 |
#include <sys/wait.h> |
| 16 |
|
| 17 |
const int NUM_PROCS=15; |
| 18 |
|
| 19 |
int main (int argc, char *argv[]) |
| 20 |
{ |
| 21 |
int i; |
| 22 |
pid_t children[NUM_PROCS]; |
| 23 |
pid_t pid; |
| 24 |
int status; |
| 25 |
|
| 26 |
for (i = 0; i < NUM_PROCS; i++) |
| 27 |
{ |
| 28 |
pid = fork (); |
| 29 |
if (pid) |
| 30 |
{ |
| 31 |
children[i] = pid; |
| 32 |
} |
| 33 |
else |
| 34 |
{ |
| 35 |
int wait_time = 0; |
| 36 |
pid = getpid (); |
| 37 |
printf ("process %d started.\n", pid); |
| 38 |
|
| 39 |
|
| 40 |
execl ("/usr/bin/hashcash", "/usr/bin/hashcash", "-mb25", "foo", NULL); |
| 41 |
exit(0); |
| 42 |
|
| 43 |
|
| 44 |
srand (time (NULL) + pid); |
| 45 |
wait_time = rand () % 20; |
| 46 |
printf ("%d: waiting %d seconds.\n", pid, wait_time); |
| 47 |
sleep (wait_time); |
| 48 |
printf ("%d: exiting.\n", pid); |
| 49 |
exit (0); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pid = wait (&status); |
| 54 |
|
| 55 |
printf("process %d exited.\nkilling all children.\n", pid); |
| 56 |
|
| 57 |
for (i = 0; i < NUM_PROCS; i++) |
| 58 |
{ |
| 59 |
printf ("%d\n", children[i]); |
| 60 |
kill (children[i], SIGKILL); |
| 61 |
} |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |