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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:12:52+00:00 2026-06-04T13:12:52+00:00

I have been studying signals in Linux. And I’ve done a test program to

  • 0

I have been studying signals in Linux. And I’ve done a test program to capture SIGINT.

#include <unistd.h>
#include <signal.h>
#include <iostream>
void signal_handler(int signal_no);
int main() {
  signal(SIGINT, signal_handler);
  for (int i = 0; i < 10; ++i) {
  std::cout << "I'm sleeping..." << std::endl;
  unsigned int one_ms = 1000;
  usleep(200* one_ms);
  }
  return 0;
}
void signal_handler(int signal_no) {
  if (signal_no == SIGINT)
    std::cout << "Oops, you pressed Ctrl+C!\n";
  return;
}

While the output looks like this:

I'm sleeping...
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
I'm sleeping...
I'm sleeping...

I understand that when pressing Ctrl+C, processes in foreground process group all receives a SIGINT(if no process chooses to ignore it).

So is it that the shell(bash) AND the instance of the above program both received the signal? Where does the "^C" before each "Oops" come from?

The OS is CentOS, and the shell is bash.

  • 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-04T13:12:53+00:00Added an answer on June 4, 2026 at 1:12 pm

    It is the terminal (driver) that intercepts the ^C and translates it to a signal sent to the attached process (which is the shell) stty intr ^B would instruct the terminal driver to intercept a ^B instead. It is also the terminal driver that echoes the ^C back to the terminal.

    The shell is just a process that sits at the other end of the line, and receives it’s stdin from your terminal via the terminal driver (such as /dev/ttyX), and it’s stdout (and stderr) are also attached to the same tty.

    Note that (if echoing is enabled) the terminal sends the keystrokes to both the process (group) and back to the terminal. The stty command is just wrapper around the ioctl()s for the tty driver for the processes “controlling” tty.

    UPDATE: to demonstrate that the shell is not involved, I created the following small program. It should be executed by its parent shell via exec ./a.out (it appears an interactive shell will fork a daughter shell, anyway) The program sets the key that generates the SIGINTR to ^B, switches echo off, and than waits for input from stdin.

    #include <stdio.h>
    #include <string.h>
    #include <termios.h>
    #include <unistd.h>
    #include <signal.h>
    #include <errno.h>
    
    int thesignum = 0;
    void handler(int signum);
    
    void handler(int signum)
    { thesignum = signum;}
    
    #define THE_KEY 2 /* ^B */
    
    int main(void)
    {
    int rc;
    struct termios mytermios;
    
    rc = tcgetattr(0 , &mytermios);
    printf("tcgetattr=%d\n", rc );
    
    mytermios.c_cc[VINTR] = THE_KEY; /* set intr to ^B */
    mytermios.c_lflag &= ~ECHO ; /* Dont echo */
    rc = tcsetattr(0 , TCSANOW, &mytermios);
    printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );
    
    printf("Setting handler()\n" );
    signal(SIGINT, handler);
    
    printf("entering pause()\n... type something followed by ^%c\n", '@'+THE_KEY );
    rc = pause();
    printf("Rc=%d: %d(%s), signum=%d\n", rc, errno , strerror(errno), thesignum );
    
    // mytermios.c_cc[VINTR] = 3; /* reset intr to ^C */
    mytermios.c_lflag |= ECHO ; /* Do echo */
    rc = tcsetattr(0 , TCSANOW, &mytermios);
    printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );
    
    return 0;
    }
    

    intr.sh:

    #!/bin/sh
    echo $$
    exec ./a.out
    echo I am back.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been studying unity3d for a few months and i have done some
I have been studying DDD in-depth on my own time; reading everything about it
I have been studying unicode and its Python implementation now for two days, and
Over the last few weeks I have been studying the MVC design pattern for
I've recently been studying TDD, attended a conference and have dabbled in few tests
I have been studying swing for developing gui applications since a few days. The
I have been studying this code to generate random text: from collections import defaultdict,
I have been studying the ECMAScript specification and have found that it is extremely
I have been studying .NET 4.0 Code Contracts and looking on stackoverflow as well
I'm a Maven newbie, have been studying Maven for a week now, have 5

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.