|
| | #include<signal.h>
/* Description : This program runs a few processes which send to each other
signals through the kill command. The signals activate the procedure
sigCathcher which sends a signal to the next process. Each child process is
exiting after receiving the signal. The parent is collecting all the zombies
and then exiting.
*/
int cpid[5];//holds the pids of the childs
int j;//pointer to cpid
int sigCatcher(){ // function to activate when a signal is caught
printf("PID %d caught one\n",getpid());
signal(SIGINT,sigCatcher); // reset the signal catcher
if(j>-1)
kill(cpid[j],SIGINT);// send signal to next child in cpid
}
int main(){
int i;
int zombie;
int status;
int pid
signal(SIGINT,sigCatcher); // set the signal catcher to sigCatcher
for(i=0;i<5;i++){
if((pid=fork())==0){ // create new child
printf("PID %d ready\n",getpid());
j=i-1;
pause(); // wait for signal
exit(0); // end proccess (become a zombie)
} else
// Only father update the cpid array.
cpid[i]=pid;
}
sleep(2); // allow children time to enter pause
kill(cpid[4],SIGINT); // send signal to first child
sleep(2); // wait for children to become zombies
for(i=0;i<5;i++){
zombie=wait(&status); // collect zombies
printf("%d is dead\n",zombie);
}
exit(0);
}
|