/* */ /* Physics 673 Computer Assignment #1 */ /* Trevor Nickle */ /* November 13, 1996 */ /* */ /* Purpose */ /* */ /* To create a program that will spawn a secondary */ /* Process. Have this process terminate normally without*/ /* Having the calling process terminate before hand. */ /* Once the orignal process has determined that the */ /* Child process has terminated then terminate the */ /* Parent process. All zombie processes should be */ /* Included. */ /* */ /* Variables */ /* */ /* STAT Integer pointer that is used to satisfy the necessary */ /* Requirements of the waitpid routine. */ /* T Integer variable that stores the returned value from */ /* The fork routine. T = 0 inside the child and */ /* T = Child Process ID # inside the parent */ /* W Integer variable that stores the returned value from */ /* A waitpid routine. Equals process id if process is */ /* Alive. Equals -1 if process is dead. */ /* PARENT Integer value that stores the id of the parent */ /* CHILD Integer value that stores the id of the child */ /* */ #include#include #include #include int main() { int *stat; int t,w,parent,child; printf("PARENT: This Program Creates A Child\n"); printf(" \n"); parent = getpid(); printf("PARENT: The Parent Process Id is %d\n",parent); printf(" \n"); system("ps -l | grep a.out | grep -v grep"); /* Shows Parent is Alive */ printf(" \n"); printf("PARENT: A Child Will Now Be Born\n"); printf(" \n"); t = fork(); /* Child process is spawned here */ if (t==0) { /* Child process executes only this part of the if */ printf("CHILD: Child Born\n"); printf(" \n"); printf("CHILD: Here I am!\n"); printf(" \n"); child = getpid(); printf("CHILD: My Process ID is %d\n",child); printf(" \n"); system("ps -l | grep a.out | grep -v grep"); /* Shows Parent & Child */ printf(" \n"); /* Alive and well */ printf("CHILD: Life Sucks - I'm Going To Die Now\n"); } /* Child executions end here. Child becomes a Zombie */ else { /* Parent Executes Only This Part of the code */ sleep(7); printf(" \n"); system("ps -l | grep a.out | grep -v grep"); /* Shows Zombie Process */ printf(" \n"); printf("PARENT: Child Process %d Is A Zombie!\n",t); w = waitpid(t,stat,0); while (w!=-1) { /* This While confirms that the child has died */ w = waitpid(t,stat,0); } /* Once parent is here, child is dead and removed */ /* Parent is now allowed to terminate */ printf(" \n"); printf("PARENT: Lets See What My Child Is Up To?\n"); printf(" \n"); system("ps -l | grep a.out | grep -v grep"); /* Shows only parent proc */ printf(" \n"); printf("PARENT: Child Process %d Is Dead And Removed - Time To Exit\n",t); printf(" \n"); } return 0; }