title: My signal handler sample is not working
body: <p><strong>1.I have written a piece of sample code and that will catch SIGALRM signal exit the main process.</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;signal.h&gt;
  #include&lt;sys/signal.h&gt;
  
  static int sig_flag=0;
  static void mysignal(int sig)
  {
          sig_flag=1;
  }
  
  void installsignal(int sig,void( *signalhandler)(int))
  {
      struct sigaction action={0};
      action.sa_handler=signalhandler;
      action.sa_flags=0;
          sigemptyset(&amp;action.sa_mask);
          if(sigaction(sig,&amp;action,NULL)&lt;0)
          {
                  printf("can not catch signal signum:%d\n",sig);
          }
  }
  main()
  {
          installsignal(SIGALRM,mysignal);
          if(sig_flag==1)
          {
                  printf("\n Signal has been caought\n");
                  exit(0);
          }
          while(1)
          {
                  printf("\nHello world\n");
                  sleep(1);
          }
          exit(0);
  }
  </code></pre>
  
  <p><strong>2.When i  have changed my program as given below then its works:-</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;signal.h&gt;
  #include&lt;sys/signal.h&gt;
  
  static int sig_flag=0;
  static void mysignal(int sig)
  {
   sig_flag=1;
  }
  
  void installsignal(int sig,void( *signalhandler)(int))
  {
      struct sigaction action={0};
      action.sa_handler=signalhandler;
      action.sa_flags=0;
      sigemptyset(&amp;action.sa_mask);
      if(sigaction(sig,&amp;action,NULL)&lt;0)
      {
       printf("can not catch signal signum:%d\n",sig);
      }
  }
  main()
  {
      installsignal(SIGALRM,mysignal);
      while(1)
      {
       printf("\nHello world\n");
       if(sig_flag==1)
       {
        printf("\n Signal has been caought\n");
        exit(0);
       }
       sleep(1);
      }
          exit(0);
  }
  </code></pre>
  
  <p><strong>Why first one sample code is not working</strong></p>