Advertisement
xeritt

Linux signal SIGUSR1 and SIGUSR2 ping pong

May 29th, 2021 (edited)
321
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8.        
  9. static void signal_handler(int);
  10. int i, pid1, pid2, status;
  11.  
  12. int main( int argc, char *argv[], char *env[] ){
  13.     if( signal( SIGUSR1, signal_handler) == SIG_ERR  ){
  14.         printf("Pærent: Unable to create handler for SIGUSR1\n");
  15.     }
  16.  
  17.     if( signal( SIGUSR2, signal_handler) == SIG_ERR  ){
  18.         printf("Pærent: Unable to create handler for SIGUSR2\n");
  19.     }
  20.  
  21.     printf( "Parent pid = %d\n", pid1=getpid() );
  22.  
  23.     if( (pid2 = fork()) == 0 ){
  24.         printf( "Child pid = %d\n", getpid() );
  25.         printf( "Child %d: sending parent %d SIGUSR1 \n", getpid(), pid1);
  26.         sleep(5);
  27.         kill( pid1, SIGUSR1 );
  28.    
  29.         for( ;; ); /* loop forever */
  30.  
  31.     } else {
  32.         wait(&status);
  33.        
  34.         if( WIFEXITED( status ) )
  35.         {
  36.             printf( "Child return - %d\n", WEXITSTATUS( status ) );
  37.         }
  38.         else
  39.         {
  40.             /* Well it didn't exit properly.  Was it a signal? */
  41.             if( WIFSIGNALED( status ) )
  42.             {
  43.                 /*
  44.                  * Yes. A signal killed the child process.  Now we can extract
  45.                  * the signal information from status
  46.                  */
  47.                 printf( "Child died on signal - %d\n", WTERMSIG( status ));
  48.             }
  49.         }
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55. static void signal_handler(int signo){
  56.  
  57.     /* signo contains the signal number that was received */
  58.     switch( signo ){
  59.         /* Signal is a SIGUSR1 */
  60.         case SIGUSR1:
  61.             printf( "Process %d: received SIGUSR1 \n", getpid() );
  62.             if(pid1==getpid()) /* it is the parent */
  63.             {
  64.              printf( "Process %d (it is the parent) send SIGUSR1 to %d...\n", getpid(),pid2 );
  65.              kill( pid2, SIGUSR1 );
  66.             }
  67.             else /* it is the child */
  68.             {
  69.               printf( "Process %d send SIGUSR2 to %d...\n", getpid(), pid1 );
  70.               kill(pid1, SIGUSR2);
  71.             }
  72.         break;
  73.        
  74.         /*  It's a SIGUSR2 */
  75.         case SIGUSR2:
  76.             printf( "Process %d: received SIGUSR2 \n", getpid() );
  77.             if(pid1==getpid())
  78.             {
  79.              printf( "Process %d (it is the parent) send SIGUSR2 to %d...\n", getpid(),pid2 );
  80.              kill( pid2, SIGUSR2 );
  81.             }
  82.             else /* it is the child */
  83.             {
  84.               printf( "Process %d will terminate itself using SIGINT\n", getpid());
  85.               kill(getpid(), SIGINT);
  86.               exit(0); //don't work
  87.             }
  88.         break;
  89.  
  90.         default:
  91.         break;
  92.     }
  93.     return;
  94. }
  95.  
  96.  
Advertisement
RAW Paste Data Copied
Advertisement