POSIX timers and POSIX signals handling

cposixsignalstimer

I am developing a simple software to check if I am able to program using what I studied about POSIX timers and signal.

I am trying to do a simple program that starts a timer and emits a signal a certain amount of nanoseconds

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.

Various prints like

prinf("1\n")

are to check where the program exits prematurely.
I putted

struct sigevent sigeventStruct

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.

if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1)

is to create a POSIX timer. POSIX MONOTONIC CLOCK is the kind of timer, &sigeventStruct is the pointer to the structure that describes how it is the event generated by the timer expiration. &timer1 is the pointer to the name of the specific timer.

if(timer_settime(timer1, NULL, &tempoIniziale, &tempoFinale) == -1)

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: <>
&tempoIniziale and &tempoFinale are pointers to itimerspec structs. I have not understood well what is the meaning of &old_timer. In GAPIL book you can read:
<>

struct sigaction, oldSigAzione

sigaction structs that will be passed as parameter to sigaction POSIX signal handler

sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione)

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, &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.

My question is:
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) ?

Here my code:

#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <stdbool.h>

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(&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, &sigeventStruct, &timer1) == -1)
    {
        printf( "Errore timer_create: %s\n", strerror( errno ) );
    }

    printf("7\n");
    struct itimerspec tempoIniziale;
    printf("8\n");
    memset(&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, &tempoIniziale, NULL) == -1) // timer armed
    {
        printf( "Errore timer_settime: %s\n", strerror( errno ) );
    }
    printf("11\n");
    for(int i = 0; i< 10; i++)
    {
        printf("ciclo %d\n", i);
    }

    struct sigaction oldSigAzione;
    printf("12\n");
    memset(&oldSigAzione, 0, sizeof oldSigAzione);
    printf("13\n");
    oldSigAzione.sa_handler = termination_handler;
    printf("14\n");
    sigemptyset (&oldSigAzione.sa_mask);

    printf("15\n");
    oldSigAzione.sa_flags = 0;

    printf("16\n");
    sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione);
    printf("17\n");
    if(oldSigAzione.sa_handler == SIG_IGN)
    {
        printf("Segnale ignorato\n");
    }
    printf("18\n");
    for(int i = 0; i < 1000000000000; i++)
    {

    }
    printf("19\n");
    printf("number of expirations %d\n", timer_getoverrun(timer1));
    return 0;
}

Best Answer

In the call to timer_create(), the first argument should be a clockid such as CLOCK_MONOTONIC. _POSIX_MONOTONIC_CLOCK is just a macro that you can use to test at compile time whether the system supports CLOCK_MONOTONIC.

The first argument to sigaction() should be the signal number, not SIGEV_SIGNAL. In your case you are using signal 10, but that is not a good choice because usually that will be one of the predefined OS signals. Usually you would use a user-defined signal like SIGUSR1 or a realtime signal like SIGRTMIN, since these are defined for use by the application. Also your call to sigaction() does not set a signal handler because the second argument is NULL. The second argument should be a pointer to your new signal action. Instead you are using the third argument which is used to return the previous signal action. That allows you to set a new signal action temporarily, while saving the old signal action, and then when you are done with the new action you can set it back to the saved value. Since you never change it again, you do not have any need to get the old value. Also to be robust the signal action should be set before starting the timer, since execution of your program could be delayed (for example if the system is really bogged down) and the timer could expire before you reach the point in your code where you set the signal handler.

And as R mentioned, printf() is not async-signal-safe so it should not be called from your signal handler.