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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:10:16+00:00 2026-06-04T08:10:16+00:00

In the following program, if I uncomment _XOPEN_SOURCE line, my program terminates when I

  • 0

In the following program, if I uncomment _XOPEN_SOURCE line, my program terminates when I hit C-c, same program doesn’t terminate If I don’t comment that line. Anyone knows in what ways does _XOPEN_SOURCE affect signal handling? I am on linux with gcc (4.6.3) and glibc (2.15).

/* #define _XOPEN_SOURCE 700 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

typedef void (*sighandler_t)(int);

void handle_signal(int signo)
{
    printf("\n[MY_SHELL] ");
    fflush(stdout);
}

int main()
{
    int c;
    signal(SIGINT, SIG_IGN);
    signal(SIGINT, handle_signal);
    printf("[MY_SHELL] ");
    while ((c = getchar()) != EOF) {
        if (c == '\n')
            printf("[MY_SHELL] ");
    }
    printf("\n");
    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-06-04T08:10:18+00:00Added an answer on June 4, 2026 at 8:10 am

    The problem is that the signal() function can have two different forms of behaviour when installing a signal handling function:

    • System V semantics, where the signal handler is “one-shot” – that is, after the signal handling function is called, the signal’s disposition is reset to SIG_DFL – and system calls that are interrupted by the signal are not restarted; or
    • BSD semantics, where the signal handler is not reset when the signal fires, the signal is blocked whilst the signal handler is executing, and most interrupted system calls are automatically restarted.

    On Linux with glibc, you get the BSD semantics if _BSD_SOURCE is defined, and the System V semantics if it is not. The _BSD_SOURCE macro is defined by default, but this default definition is suppressed if you define _XOPEN_SOURCE (or a few other macros too, like _POSIX_SOURCE and _SVID_SOURCE).

    Under System V semantics, if the read() system call underlying getchar() is interrupted by SIGINT then getchar() will return EOF with errno set to EINTR (this will cause your program to exit normally). In addition, after the first SIGINT the disposition of this signal is reset to the default, and the default action for SIGINT is to terminate the process (so even if your program survived the first SIGINT, the second would cause it to exit abnormally).

    The solution is not to use signal() at all for installing signal-handling functions; instead, you should use sigaction(), which is portable – it gives the same semantics everywhere. With sa_flags set to SA_RESTART, sigaction() it will give the BSD semantics, which is what you want.

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

Sidebar

Related Questions

This is my program - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include
The following program is essentially the same as the one described here . When
The following program forks off a child, that runs /bin/sleep 10 repeatedly. The parent
The following program shows that we can use return or pthread_exit to return a
The following program doesn't output desired data (on VC2008 compiler) #include <stdio.h> #include <string.h>
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
I have the following program to open lot's of sockets, and hold them open

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.