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 9044905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:06:24+00:00 2026-06-16T11:06:24+00:00

I am trying to implement a basic event loop with pselect , so I

  • 0

I am trying to implement a basic event loop with pselect, so I have blocked some signals, saved the signal mask and used it with pselect so that the signals will only be delivered during that call.

If a signal is sent outside of the pselect call, it is blocked until pselect as it should, however it does not interrupt the pselect call. If a signal is sent while pselect is blocking, it will be handled AND pselect will be interrupted. This behaviour is only present in OSX, in linux it seems to function correctly.

Here is a code example:

#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

int shouldQuit = 0;

void signalHandler(int signal)
{
    printf("Handled signal %d\n", signal);
    shouldQuit = 1;
}

int main(int argc, char** argv)
{    
    sigset_t originalSignals;     
    sigset_t blockedSignals;
    sigemptyset(&blockedSignals);
    sigaddset(&blockedSignals, SIGINT);
    if(sigprocmask(SIG_BLOCK, &blockedSignals, &originalSignals) != 0)
    {
        perror("Failed to block signals");
        return -1;
    }

    struct sigaction signalAction;
    memset(&signalAction, 0, sizeof(struct sigaction));

    signalAction.sa_mask = blockedSignals;

    signalAction.sa_handler = signalHandler;

    if(sigaction(SIGINT, &signalAction, NULL) == -1)
    {
        perror("Could not set signal handler");
        return -1;
    }

    while(!shouldQuit)
    {
        fd_set set;
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO, &set);
        printf("Starting pselect\n");
        int result = pselect(STDIN_FILENO + 1, &set, NULL, NULL, NULL, &originalSignals);
        printf("Done pselect\n");
        if(result == -1)
        {
            if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
            {
                perror("pselect failed");
            }
        }
        else
        {
            printf("Start Sleeping\n");
            sleep(5);
            printf("Done Sleeping\n");
        }
    }

    return 0;
}

The program waits until you input something on stdin, then sleeps for 5 seconds. To create the problem, “a” is typed to create data on stdin. Then, while the program is sleeping, an INT signal is sent with Crtl-C.

On Linux:

Starting pselect
a
Done pselect
Start Sleeping
^CDone Sleeping
Starting pselect
Handled signal 2
Done pselect

On OSX:

Starting pselect
a
Done pselect
Start Sleeping
^CDone Sleeping
Starting pselect
Handled signal 2
^CHandled signal 2
Done pselect
  • 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-06-16T11:06:25+00:00Added an answer on June 16, 2026 at 11:06 am

    Confirmed that it acts that way on OSX, and if you look at the source for pselect (http://www.opensource.apple.com/source/Libc/Libc-320.1.3/gen/FreeBSD/pselect.c), you’ll see why.

    After sigprocmask() restores the signal mask, the kernel delivers the signal to the process, and your handler gets invoked. The problem here is, that the signal can be delivered before select() gets invoked, so select() won’t return with an error.

    There’s some more discussion about the issue at http://lwn.net/Articles/176911/ – linux used to use a similar userspace implementation that had the same problem.

    If you want to make that pattern safe on all platforms, you’ll have to either use something like libev or libevent and let them handle the messiness, or use sigprocmask() and select() yourself.

    e.g.

        sigset_t omask;
        if (sigprocmask(SIG_SETMASK, &originalSignals, &omask) < 0) {
            perror("sigprocmask");
            break;
        }
    
        /* Must re-check the flag here with signals re-enabled */
        if (shouldQuit) 
            break;
    
        printf("Starting select\n");
        int result = select(STDIN_FILENO + 1, &set, NULL, NULL, NULL);
        int save_errno = errno;
        if (sigprocmask(SIG_SETMASK, &omask, NULL) < 0) {
            perror("sigprocmask");
            break;
        }
    
        /* Recheck again after the signal is blocked */
        if (shouldQuit) 
            break;
    
        printf("Done pselect\n");
    
        if(result == -1)
        {
            errno = save_errno;
            if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
            {
                perror("pselect failed");
            }
        }
    

    There are a couple of other things you should do with your code:

    • declare your ‘shouldQuit’ variable as volatile sig_atomic_t

      volatile sig_atomic_t shouldQuit = 0;

    • always save errno before calling any other function (such as printf()), since that function may cause errno to be overwritten with another value. Thats why the code above aves errno immediately after the select() call.

    Really, I strongly recommend using an existing event loop handling library like libev or libevent – I do, even though I can write my own, because it is so easy to get wrong.

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

Sidebar

Related Questions

I'm trying to decide how to implement a very basic licensing solution for some
I'm trying to implement a basic popup window that asks the user if they
I'm trying to implement basic auditing with some of my models (like CreatedAt, UpdatedAt,
I'm trying to implement a suspend event that transitions the object to the :suspended
I'm trying to implement a basic jQuery infinite carousel. As much for the learning
General use case I am trying to implement a basic shell. Description I need
I'm trying to implement a very basic chat/echo web page. When the client visits
I am trying to implement a simple code tester : a very basic version
I'm trying to implement anti-aliased pixel rendering. My basic idea is to render 4
This is a very basic example of what I am trying to implement in

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.