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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:50:45+00:00 2026-05-27T02:50:45+00:00

I have the following code. When I compile it with the gnu extensions (

  • 0

I have the following code. When I compile it with the gnu extensions (-std=gnu99), the program will catch 5 SIGINT before ending (which I would expect). When compiled without it (-std=c99) ends after the second (and only outputs one line).

What am I missing?

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

int int_stage = 0;
int got_signal = 0;

void sigint(int parameter)
{
  (void)parameter;
  got_signal = 1;
  int_stage++;
}

int main()
{
  signal(SIGINT,sigint);

  while(1)
  {
    if (got_signal)
    {
      got_signal = 0;
      puts("still alive");
      if (int_stage >= 5) exit(1);
    }
  }
  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-27T02:50:46+00:00Added an answer on May 27, 2026 at 2:50 am

    Use sigaction(2) rather than signal(2).

    The Linux man page has this, in particular, in the Portability section:

    In the original UNIX systems, when a handler that was established using signal() was invoked by the
    delivery of a signal, the disposition of the signal would be reset to SIG_DFL, and the system did
    not block delivery of further instances of the signal. System V also provides these semantics for
    signal(). This was bad because the signal might be delivered again before the handler had a chance
    to reestablish itself. Furthermore, rapid deliveries of the same signal could result in recursive
    invocations of the handler.

    BSD improved on this situation by changing the semantics of signal handling (but, unfortunately,
    silently changed the semantics when establishing a handler with signal()). On BSD, when a signal
    handler is invoked, the signal disposition is not reset, and further instances of the signal are
    blocked from being delivered while the handler is executing.

    The situation on Linux is as follows:

    • The kernel’s signal() system call provides System V semantics.

    • By default, in glibc 2 and later, the signal() wrapper function does not invoke the kernel system
      call. Instead, it calls sigaction(2) using flags that supply BSD semantics. This default behav‐
      ior is provided as long as the _BSD_SOURCE feature test macro is defined. By default, _BSD_SOURCE
      is defined; it is also implicitly defined if one defines _GNU_SOURCE, and can of course be explic‐
      itly defined.
      On glibc 2 and later, if the _BSD_SOURCE feature test macro is not defined, then signal() provides
      System V semantics. (The default implicit definition of _BSD_SOURCE is not provided if one
      invokes gcc(1) in one of its standard modes (-std=xxx or -ansi) or defines various other feature
      test macros such as _POSIX_SOURCE, _XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)

    Using std=gnu99, you’re getting BSD semantics. Using -std=c99, you’re getting System V semantics. So the signal handler is “reinstalled” in one case (BSD), and the signal disposition is reset back to SIG_DFL in the other (System V).

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

Sidebar

Related Questions

I have the following code which i m trying to compile: #include <boost/filesystem/convenience.hpp> #include
I have the following code which I'm really struggling to compile: #include <stdio.h> #include
I have the following code which is failing to compile. #include <stdio.h> #include <log4cpp/Category.hh>
I have following code snippet that i use to compile class at the run
I have the following code that won't compile and although there is a way
I have the following error when I try to compile my code in g+
I have the following bit of legacy C++ code that does not compile: #include
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I have the following code, which compiles but doesn't bring back any data. Here
I'm trying to use NetBeans to compile C code and have the following versions

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.