Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:15+00:00 2026-05-28T22:09:15+00:00

I am developing a simple software to check if I am able to program

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:09:16+00:00Added an answer on May 28, 2026 at 10:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a simple software in .NET C# for blind people that includes
I'm developing a simple p2p program that is meant to work over the Internet,
Hello all I am developing a software that uses amazon simple queue service and
I've been developing a simple website using asp.net MVC and I'm starting to add
I am developing a simple platform game in Java using BlueJ. Over the next
I am developing a simple translator from MathML to Latex, using Lex and Yacc.
I'm an engineering student and I'm developing a simple software based on HTML, PHP
Recently I'm developing a software that parses and displays XML information from a website.
I am developing a piece of software that records video from a live source
I'm developing software that writes to a tiny LCD screen (less than 1 x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.