title: How to Ignore Signal in C?
body: <p>I want to ignore signal for first 100 seconds of the thread life cycle, I developed this solution but its not working, what part Im missing?</p>
  
  <p><strong>Problem statement:</strong>
  Write a program that has two threads - creating thread (thread A) and created thread (thread B). After creating thread B, thread A waits for thread B to terminate and then it terminates itself. Thread B terminates only under a special circumstance described later. Signal SIGINT (ctrl C) is blocked in thread B (i.e. only thread A handles SIGINT). Every time, the user presses ctrl C, thread A tries to cancel thread B. Thread B keeps pending any cancellation request received during the first 100 seconds of its life. Any cancellation request is honored once the first 100 seconds of the thread B's life are over. Also, thread B prints its current lifetime in terms of seconds after every 10 seconds. </p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/time.h&gt;
  
  #define handle_error_en(en, msg) \
                 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
  
  void function_to_time(void);
  
  
  static void *thread_func(void *arg)
  {
  
      struct sigaction act;
      sigaction(SIGINT,NULL,&amp;act);
      act.sa_handler = SIG_IGN;
      sigaction(SIGINT,&amp;act,NULL); 
  
      sigset_t sigmask;
      sigemptyset(&amp;sigmask);
      sigaddset(&amp;sigmask, SIGALRM);
      sigprocmask(SIG_BLOCK,&amp;sigmask,NULL); 
  
      sigemptyset(&amp;sigmask);
      sigaddset(&amp;sigmask,SIGALRM);
      sigaddset(&amp;sigmask,SIGINT);
  
      alarm(100);
  
      int sigintsReceived = 0;
      int sigalrmsReceived = 0;
  
      while( 1 == 1)
      {
          int recvdSig;
          sigwait(&amp;sigmask,&amp;recvdSig);
  
          if( recvdSig == SIGALRM ) 
          {
              if( sigalrmsReceived == 0 )
              {
                  sigalrmsReceived++;
                  printf("\npthread_cancel is enabled\n");
  
                  sigset_t sigmask2;
                  sigemptyset(&amp;sigmask2);
                  sigaddset(&amp;sigmask2, SIGINT);
                  sigprocmask(SIG_BLOCK,&amp;sigmask2,NULL); 
  
                  sigaction(SIGINT,NULL,&amp;act);
                  act.sa_handler = SIG_DFL;
                  sigaction(SIGINT,&amp;act,NULL); 
              }
  
          }
      };
  
      return 0;
  }
  
  
  int main(void)
  {
  
     sigset_t set;
  
     pthread_t thr;
     void *res;
     int s;
  
     s = pthread_create(&amp;thr, NULL, &amp;thread_func, (void *)&amp;set);
     if (s != 0)
         handle_error_en(s, "pthread_create");
  
     sigemptyset(&amp;set);
     sigaddset(&amp;set, SIGINT);
     pthread_sigmask(SIG_BLOCK, &amp;set, NULL);   
  
      int recvdSig;
      sigwait(&amp;set,&amp;recvdSig);
  
      if( recvdSig == SIGINT ) 
      {
          printf("Signal received\n");
          printf("main(): sending cancellation request\n");
          s = pthread_cancel(thr);
  
          if (s != 0)
              handle_error_en(s, "pthread_cancel");
      }
  
      s = pthread_join(thr, &amp;res);
      if (s != 0)
      handle_error_en(s, "pthread_join");
  
      if (res == PTHREAD_CANCELED)
      printf("main(): thread was canceled\n");
      else
      printf("main(): thread wasn't canceled (shouldn't happen!)\n");
      exit(EXIT_SUCCESS);
  
  }
  </code></pre>