Note that there are some explanatory texts on larger screens.

plurals
  1. POError with signal handling for processes in linux
    primarykey
    data
    text
    <p>The following is the code fragment :</p> <pre><code>#include&lt;sys/types.h&gt; #include&lt;stdio.h&gt; #include&lt;signal.h&gt; #include&lt;stdlib.h&gt; #include&lt;unistd.h&gt; #include&lt;string.h&gt; sig_atomic_t child1 ; sig_atomic_t child2 ; void child1_handler( int sig_num ) { int ret ; if(sig_num == SIGUSR2 ) { printf("\n Recieved sigusr2 for child1\n"); ret = kill( getppid() , SIGUSR2 ) ; if( ret != 0 ) kill( getpid() , SIGTERM ); } else if(sig_num == SIGUSR1 ) { /* child 1 does something */ printf("\n Recieved sigusr1 for child1\n"); printf("\n child 1 is doing \n"); kill( child2 , SIGUSR2 ); } } void child2_handler( int sig_num ) { if( sig_num == SIGUSR2 ) { /* child2 does somethign */ printf("\n Recieved sigusr2 for child2\n"); printf("\n child2 is doing \n"); kill( child1 , SIGUSR2 ); kill( getpid() , SIGTERM ); } } void parent_handler( int sig_num ) { int ret ; if( sig_num == SIGUSR2 ) { printf("\n Recieved sigusr2 for parent\n"); ret = kill( child1 , SIGUSR1 ) ; if( ret != 0 ) { /* parent does something */ printf( "\n Parent does something \n" ); exit(0); } } } int main() { struct sigaction sa_parent , sa_child1 , sa_child2 ; pid_t temp_id1 , temp_id2 ; int temp ; memset(&amp;sa_parent , 0 , sizeof(sa_parent) ) ; memset(&amp;sa_child1 , 0 , sizeof(sa_child1) ) ; memset(&amp;sa_child2 , 0 , sizeof(sa_child2) ) ; /* parent */ printf( " \n Inside parent \n" ); sa_parent.sa_handler = &amp;parent_handler ; sigaction( SIGUSR2 , &amp;sa_parent , NULL ); temp_id1 = fork() ; if( temp_id1 == 0 ) { child1 = getpid() ; /* child1 */ printf("\n inside child1 \n" ); sa_child1.sa_handler = &amp;child1_handler ; sigaction( SIGUSR1 , &amp;sa_child1 , NULL ) ; sigaction( SIGUSR2 , &amp;sa_child1 , NULL ) ; temp_id2 = fork() ; if( temp_id2 == 0 ) { child2 = getpid() ; /* child2 */ printf( "\n inside child2 \n" ); sa_child2.sa_handler = &amp;child2_handler ; sigaction( SIGUSR2 , &amp;sa_child2 , NULL ); kill( child1 , SIGUSR2 ); } wait(&amp;temp); } wait(&amp;temp); return 0 ; } </code></pre> <p>I am expecting the output to depict that first </p> <pre><code>child1 is doing child2 is doing parent does something </code></pre> <p>However the output produced is as given below.. </p> <pre><code>Inside parent inside child1 inside child2 Recieved sigusr2 for child1 Recieved sigusr2 for parent Recieved sigusr1 for child1 child 1 is doing Recieved sigusr2 for child1 User defined signal 1 [ what is going wrong ? ] </code></pre> <p>Please ignore the "recieved sign" and "Inside..." lines as they were meant to mark locations in code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload