/* Forks and then the parent sends a signal to the child. The signal */ /* sent is the command line argument. After this signal is sent, the */ /* child resumes the endless loop. You'll get the system prompt. */ /* Next, issue the kill command to the child (use 'ps xg' to get the */ /* pid) with different arguments (except SIGKILL). Stop it for good */ /* using kill -9 */ #include #include #include #include #define period 3 void handler(int); main(argc,argv) int argc; char *argv[]; {pid_t ChildID; int i,flag=0; if (argc!=2) { fprintf(stderr,"Form: sigdemo \n"); exit(1); } if ((ChildID=fork()) < 0) { perror("Fork!!!!"); exit(1); } else if (ChildID) { /* Parent */ fprintf(stderr,"Will Send Signal %d in %d Seconds \n",atoi(argv[1]),period); sleep(period); fprintf(stderr,"Parent: Sending Signal\n"); kill(ChildID,atoi(argv[1])); } else { /* Child */ struct sigaction action; action.sa_handler = (void (*)(int))handler; for (i=1;i<32;i++) { // if (signal(i,handler)==SIG_ERR) { old way if (sigaction(i,&action,NULL)<0) { /* Do nothing. Try this: Write the signal number here so you can discern */ /* which ones cause an error in the signal() call. Why are they an error?*/ } } do { if (!flag && (getppid()==1)) { fprintf(stderr,"Parent Finished. Child's Parent now init\n"); flag++; } } while(1); } } void handler(int sig) {fprintf(stderr,"Child: Caught Signal %d\n",sig); //return(NULL); }