Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* program to send signal to child */
- #include<stdio.h>
- #include<signal.h>
- void sighandler_child(int sig_num){
- printf("\n\nSignal %d caught by the process with pid = %d\n",sig_num,getpid());
- }
- int main(){
- pid_t pid;
- int termstatus;
- pid = fork();
- if(pid==-1)
- printf("Error whlie creating child process");
- //parent sends the signal
- if(pid > 0){
- sleep(1); //to let the child establish the handler before parent starts execution
- printf("about to send signal to child\n");
- kill(pid,SIGUSR2);
- printf("sent signal to child\n");
- wait(&termstatus);
- printf("\n\nParent: Child terminated...now I am gonna terminate\n\n");
- }
- if(pid==0){
- /* to establish the signal handler..
- the type of the second argument is "sighandler_t" which is actually a pointer to a function...
- void (*sighandler_t)(int)
- */
- if(signal(SIGUSR2,sighandler_child) == SIG_ERR){
- printf("\n\nError while establishing signal handler\n\n");
- return 2;
- }
- sleep(3); //to let the child wait for the parent to send signal
- printf("\nChild: I am exiting\n\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement