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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:50:31+00:00 2026-05-22T17:50:31+00:00

The following code catches the SIGINT signal only once and then interrupts (program exists):

  • 0

The following code catches the “SIGINT” signal only once and then interrupts (program exists):

#include <signal.h>
#include <stdio.h>

void IntHandler(int value);

void CatchIntSignal()
{
    struct sigaction intAction;
    intAction.sa_handler = IntHandler;
    sigemptyset(&intAction.sa_mask);
    intAction.sa_flags = 0;
    //if to uncomment any of "0" or "SIG_IGN" - IntHandler will be never called:
    //intAction.sa_sigaction = 0/*SIG_IGN*/;
    if(sigaction(SIGINT, &intAction, NULL) != 0)
    {
        printf("sigaction() failed.\n");
    }
}

void IntHandler(int value)
{
    printf("IntHandler(%d)\n", value);
    //uncommenting this does not help:
    //CatchIntSignal();
}

int main()
{
    CatchIntSignal();
    getchar();
    return 0;
}

How must I modify this code to preserve program’s exit after SIGINT catching?
If to set intAction.sa_sigaction to 0 or to SIG_IGN – IntHandler will never be called – but why? Which undefined value does it have to say system that “it’s necessary to call IntHandler”? If I will set some handler to intAction.sa_sigaction – this handler will be called (but IntHandler will not). How does system know that I did set something to intAction.sa_sigaction?

  • 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-22T17:50:32+00:00Added an answer on May 22, 2026 at 5:50 pm

    Your problem is that the sa_handler and sa_sigaction fields of struct sigaction are actually the same field. Quoting the (OSX) manpage for sigaction(2):

     struct  sigaction {
             union __sigaction_u __sigaction_u;  /* signal handler */
             sigset_t sa_mask;               /* signal mask to apply */
             int     sa_flags;               /* see signal options below */
     };
    
     union __sigaction_u {
             void    (*__sa_handler)(int);
             void    (*__sa_sigaction)(int, struct __siginfo *,
                            void *);
     };
    
     #define sa_handler      __sigaction_u.__sa_handler
     #define sa_sigaction    __sigaction_u.__sa_sigaction
    

    So your assignment to sa_sigaction is overwriting the handler you already set. You should just set sa_handler and leave sa_sigaction alone. This is how CatchIntSignal should read:

    void CatchIntSignal()
    {
        struct sigaction intAction;
        intAction.sa_handler = IntHandler;
        sigemptyset(&intAction.sa_mask);
        intAction.sa_flags = SA_RESTART;
        if(sigaction(SIGINT, &intAction, NULL) != 0)
            printf("sigaction() failed: %s\n", strerror(errno));
    }
    

    (You may need to add #includes of string.h and errno.h to get the strerror(errno) bit to compile. Always include strerror(errno) in error messages triggered when system calls fail.)

    (CORRECTION: added SA_RESTART to flags per ninjalj.)

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

Sidebar

Related Questions

The following bit of code catches the EOS Exception using (var reader = new
The following code: template <typename S, typename T> struct foo { void bar(); };
The following code already exists in one of the javascript files i am working
The following code using boost::asio will not compile: #ifndef _SERVER_H_ #define _SERVER_H_ #include Connection.h
I have the following code: SaveFileDialog saveGC1File = new SaveFileDialog(); private void GCSavePlacementOneButton_Click(object sender,
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code should find the appropriate project tag and remove it from the

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.