title: POSIX timers and POSIX signals handling
body: <p>I am developing a simple software to check if I am able to program using what I studied about POSIX timers and signal.</p>
  
  <p>I am trying to do a simple program that starts a timer and emits a signal a certain amount of nanoseconds</p>
  
  <p>The following program does not work well, so I wrote some comments about my code so you can check if I correctly studied or not.
  You can find the complete code listing at the bottom of the page.</p>
  
  <p>Various prints like <pre>prinf("1\n")</pre> are to check where the program exits prematurely.
  I putted <pre>struct sigevent sigeventStruct</pre> as structure for expirations events generated by the timer.
  First parameter is setted to SIGEV_SIGNAL so this is the kind of signal it will emit. ///
  The various memset you can read in code listing are to zero initialized structures.</p>
  
  <pre>if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1)</pre>
  
  <p>is to create a POSIX timer. POSIX MONOTONIC CLOCK is the kind of timer, &amp;sigeventStruct is the pointer to the structure that describes how it is the event generated by the timer expiration. &amp;timer1 is the pointer to the name of the specific timer.</p>
  
  <pre>if(timer_settime(timer1, NULL, &tempoIniziale, &tempoFinale) == -1)</pre>
  
  <p>with this procedure, the timer is armed, so you can make it generate expirations.
  timer1 is the name of the timer, 0 is the flags. GAPIL book says: &lt;>
  &amp;tempoIniziale and &amp;tempoFinale are pointers to itimerspec structs. I have not understood well what is the meaning of &amp;old_timer. In GAPIL book you can read:
  &lt;></p>
  
  <pre>struct sigaction, oldSigAzione</pre>
  
  <p>sigaction structs that will be passed as parameter to sigaction POSIX signal handler</p>
  
  <pre>sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione)</pre>
  
  <p>SIGEV_SIGNAL is the kind of signals it has to handle, NULL Is where it could be placed a pointer to a const struct sigaction, &amp;oldSigAzione is the pointer to the sigaction struct I mentioned before. Here again I have not understood the difference between these two pointers to sigaction struct.</p>
  
  <p><b>My question is:</b>
  why the program exits before printing the number 19 of printf("19\n"); and why does not excecutes the printf("Timer scaduto\n"); inside function void termination_handler(int signum) ?</p>
  
  <p>Here my code:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/fcntl.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdbool.h&gt;
  
  void termination_handler(int signum)
  {
      printf("Timer scaduto\n");
  }
  
  int main()
  {
      printf("Start\n");
      printf("1\n");
      struct sigevent sigeventStruct; // sigevent struct that will be used by timer1 timer
      printf("2\n");
      memset(&amp;sigeventStruct, 0, sizeof sigeventStruct); // zero initialize struct
      printf("3\n");
      sigeventStruct.sigev_notify = SIGEV_SIGNAL; // kind of notification of timer1 expiration
      printf("4\n");
      sigeventStruct.sigev_signo = 10;
      printf("5\n");
  
      timer_t timer1; // create a timer identifier
      printf("6\n");
      if(timer_create(_POSIX_MONOTONIC_CLOCK, &amp;sigeventStruct, &amp;timer1) == -1)
      {
          printf( "Errore timer_create: %s\n", strerror( errno ) );
      }
  
      printf("7\n");
      struct itimerspec tempoIniziale;
      printf("8\n");
      memset(&amp;tempoIniziale, NULL, sizeof tempoIniziale); // zero initialize struct
      printf("9\n");
      tempoIniziale.it_value.tv_nsec = 100000000;
      //tempoIniziale.it_interval.tv_nsec = 10000;
      printf("10\n");
  
  
      if(timer_settime(timer1, 0, &amp;tempoIniziale, NULL) == -1) // timer armed
      {
          printf( "Errore timer_settime: %s\n", strerror( errno ) );
      }
      printf("11\n");
      for(int i = 0; i&lt; 10; i++)
      {
          printf("ciclo %d\n", i);
      }
  
      struct sigaction oldSigAzione;
      printf("12\n");
      memset(&amp;oldSigAzione, 0, sizeof oldSigAzione);
      printf("13\n");
      oldSigAzione.sa_handler = termination_handler;
      printf("14\n");
      sigemptyset (&amp;oldSigAzione.sa_mask);
  
      printf("15\n");
      oldSigAzione.sa_flags = 0;
  
      printf("16\n");
      sigaction (SIGEV_SIGNAL, NULL, &amp;oldSigAzione);
      printf("17\n");
      if(oldSigAzione.sa_handler == SIG_IGN)
      {
          printf("Segnale ignorato\n");
      }
      printf("18\n");
      for(int i = 0; i &lt; 1000000000000; i++)
      {
  
      }
      printf("19\n");
      printf("number of expirations %d\n", timer_getoverrun(timer1));
      return 0;
  }
  </code></pre>