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

  • Home
  • SEARCH
  • 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 6367231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:31:21+00:00 2026-05-25T00:31:21+00:00

I am implementing a simple timer that throws a RT signal upon expiration. What

  • 0

I am implementing a simple timer that throws a RT signal upon expiration. What I want to do is to register a signal handler (using sigaction) that gets called when the signal occurs. Meanwhile the main code waits until the signal is called using sigwaitinfo.

Implementing either a signal handler or sigwaitinfo exclusively works fine. However when both are used, the signal handler is never called. I tried switching the order; i.e. registering the handler before blocking the signal. Makes no difference.

Here is the code

// gcc -Wall -o sigwait_example sigwait_example.c -lrt
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <string.h>

#define install_handler(sig,sa) if( sigaction(sig, &sa, NULL) == -1 ){ \
                                    perror("sigaction"); }
#define SIG SIGRTMIN+1

volatile int flag=0;

void handler(int signum){
    flag++;
}

int main(void){
    struct itimerspec its;
    sigset_t blocked;
    siginfo_t si;
    timer_t timerid;
    struct sigevent evt;
    struct sigaction sa;

    evt.sigev_notify = SIGEV_SIGNAL;
    evt.sigev_signo = SIG;
    evt.sigev_value.sival_ptr = &timerid;
    if ( timer_create(CLOCK_REALTIME, &evt, &timerid) ){
        perror("timer_create");
    }

    //setup timer
    its.it_value.tv_sec = 0;
    its.it_value.tv_nsec = 0.1*1E9;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;

    //arm the timer 
    if ( timer_settime(timerid, 0, &its, NULL) )
        perror("timer_settime");

    sigemptyset(&blocked);
    sigaddset(&blocked, SIG);
    //add SIG to blocked signals
    pthread_sigmask(SIG_BLOCK, &blocked, NULL);

    sa.sa_flags = SA_SIGINFO; //use this flag to set custom handler
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    install_handler(SIG,sa);

    while ( sigwaitinfo(&blocked, &si) == -1 && errno == EINTR );
    printf("received signal: %s, flag=%d\n",strsignal(si.si_signo),flag);       
    //while(flag==0) sleep(1); //use this when only signal handler is used

    timer_delete(timerid);

    return 0;
}

I am doing this mostly for educational purposes, since I need to learn as much as possible about how threads are sent/blocked as I will be using them in threads.

  • 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-25T00:31:21+00:00Added an answer on May 25, 2026 at 12:31 am

    It’s not possible because sigwaitinfo() removes the signal from the queue.

    You can, however, use sigaction(SIG, NULL, &sa) to retrieve the sigaction struct of this signal and execute the handler.

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

Sidebar

Related Questions

I am implementing a simple directory listing script in PHP. I want to ensure
I am implementing a fairly simple calendar on a website using PHP and MySQL.
We're implementing a new solution in our classic ASP environment that's using COM interop
I'm writing a simple private messenger using Django and am implementing message threading. Each
I'm writing a simple parser in C++ that works using a map of string
I was implementing some simple Producer/Consumer program that had some semaphores and shared memory.
I'm implementing a service that has several different ways it can be accessed: Using
I want to have a simple app that display the current date. Say for
I'm implementing a simple Graph library for my uni project and since this is
I'm implementing a simple update mechanism for an application I'm writing the last part

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.