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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:49:13+00:00 2026-05-24T10:49:13+00:00

using select() with pipe – this is what I am doing and now I

  • 0

using select() with pipe – this is what I am doing and now I need to catch SIGTERM on that. how can I do it? Do I have to do it when select() returns error ( < 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-24T10:49:15+00:00Added an answer on May 24, 2026 at 10:49 am

    First, SIGTERM will kill your process if not caught, and select() will not return. Thus, you must install a signal handler for SIGTERM. Do that using sigaction().

    However, the SIGTERM signal can arrive at a moment where your thread is not blocked at select(). It would be a rare condition, if your process is mostly sleeping on the file descriptors, but it can otherwise happen. This means that either your signal handler must do something to inform the main routine of the interruption, namely, setting some flag variable (of type sig_atomic_t), or you must guarantee that SIGTERM is only delivered when the process is sleeping on select().

    I’ll go with the latter approach, since it’s simpler, albeit less flexible (see end of the post).

    So, you block SIGTERM just before calling select(), and reblock it right away after the function returns, so that your process only receives the signal while sleeping inside select(). But note that this actually creates a race condition. If the signal arrives just after the unblock, but just before select() is called, the system call will not have been called yet and thus it will not return -1. If the signal arrives just after select() returns successfully, but just before the re-block, you have also lost the signal.

    Thus, you must use pselect() for that. It does the blocking/unblocking around select() atomically.

    First, block SIGTERM using sigprocmask() before entering the pselect() loop. After that, just call pselect() with the original mask returned by sigprocmask(). This way you guarantee your process will only be interrupted while sleeping on select().

    In summary:

    1. Install a handler for SIGTERM (that does nothing);
    2. Before entering the pselect() loop, block SIGTERM using sigprocmask();
    3. Call pselect() with the old signal mask returned by sigprocmask();
    4. Inside the pselect() loop, now you can check safely whether pselect() returned -1 and errno is EINTR.

    Please note that if, after pselect() returns successfully, you do a lot of work, you may experience bigger latency when responding to SIGTERM (since the process must do all processing and return to pselect() before actually processing the signal). If this is a problem, you must use a flag variable inside the signal handler, so that you can check for this variable in a number of specific points in your code. Using a flag variable does not eliminate the race condition and does not eliminate the need for pselect(), though.

    Remember: whenever you need to wait on some file descriptors or for the delivery of a signal, you must use pselect() (or ppoll(), for the systems that support it).

    Edit: nothing better than a code example to illustrate the usage.

    #define _POSIX_C_SOURCE 200809L
    #include <errno.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/select.h>
    #include <unistd.h>
    
    // Signal handler to catch SIGTERM.
    void sigterm(int signo) {
        (void)signo;
    }
    
    int main(void) {
        // Install the signal handler for SIGTERM.
        struct sigaction s;
        s.sa_handler = sigterm;
        sigemptyset(&s.sa_mask);
        s.sa_flags = 0;
        sigaction(SIGTERM, &s, NULL);
    
        // Block SIGTERM.
        sigset_t sigset, oldset;
        sigemptyset(&sigset);
        sigaddset(&sigset, SIGTERM);
        sigprocmask(SIG_BLOCK, &sigset, &oldset);
    
        // Enter the pselect() loop, using the original mask as argument.
        fd_set set;
        FD_ZERO(&set);
        FD_SET(0, &set);
        while (pselect(1, &set, NULL, NULL, NULL, &oldset) >= 0) {
            // Do some processing. Note that the process will not be
            // interrupted while inside this loop.
            sleep(5);
        }
    
        // See why pselect() has failed.
        if (errno == EINTR)
            puts("Interrupted by SIGTERM.");
        else
            perror("pselect()");
        return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to do this using SELECT but how can I do this
I have tried to install Net::Arping package using cpan and I can't do that
The name of a temporary table such as #t1 can be determined using select
i have two select using as a dropdownlist for country/state everything works as i
I have a flat file that is pipe delimited and an database table. I
I have developed a recursive FTP-download script, in PHP5, that allows you to select
I have been using SELECT Author, ISNULL(MAX(CASE Status WHEN 'Duplicate' THEN NumDocs END),'') AS
I can list all views in SQL Server 2008 by using SELECT * FROM
I need to get (or pipe) the output from a process that is already
using select statement we can call functions inside the stored procedure, is there another

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.