/* Demonstrate what happens when the Parent gets killed (Q5.e Midterm) */ #include #include #include #include main(argc,argv) int argc; char *argv[]; {pid_t IsParent; int ReadError=1,Pipefd[2],N; div_t Result; printf("!\n"); pipe(Pipefd); IsParent=fork(); if (IsParent) { dup2(Pipefd[1],1); close(Pipefd[1]); close(Pipefd[0]); N=0; while (1) { write(1,&N,sizeof(int)); /* Continually write Numbers to the Pipe */ Result=div(N++,10); fprintf(stderr,"Just sent %d Down the Pipe",N); sleep(1); } } else { /* Child receives thru the pipe */ dup2(Pipefd[0],0); close(Pipefd[1]); close(Pipefd[0]); while (ReadError) { if ((ReadError=read(0,&N,sizeof(int)))<0){ /* Continually write Numbers to the Pipe */ perror("Got a -1"); exit(1); } Result=div(N,10); fprintf(stderr,"Received %d From the Pipe - Read Result: %d\n",N,ReadError); if (N==5) kill(getppid(),9); sleep(2); /* Slow Things Down */ } } }