#include /* printf, etc */ #include /* almost always need this with system programming */ #include /* fork, getpid, getppid */ #include /* free, malloc, etc */ #include /* Another fork test program - this one does the following: at the command line we specify the number of child processes. the parent forks that many times. Each process prints somthing and quits with different exit code. parent waits around for children to die, keeps count, prints some status information. last child to die is the winner! Some things to try: find out if the result is repeatable. remove all I/O from children and repeat experiments. have all children sleep for a bit and see if that changes anything. */ /* this function is only called by the child */ void child(int cnum) { pid_t me = getpid(); printf("Child # %d is running (%u)\n",cnum,me); exit(cnum); } /* this function is only called in the parent */ void parent(int nchildren) { pid_t me = getpid(); int cstat; int i; printf("Parent is running (%u)\n",me); for (i=0;i