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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:07:50+00:00 2026-06-06T05:07:50+00:00

Simple code: #include <signal.h> #include <stdio.h> #include <unistd.h> #include <iostream> using namespaces td; bool

  • 0

Simple code:

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

using namespaces td;                                                             
bool unblock = false;                                                  
long int ile = 0;                                                 

void ouch(int sig) {
    cout << "signal " <<  sig << endl;                               
    unblock = true;                                                  
}                                                                
int main(){                                                          
    struct sigaction act;                                           
    act.sa_handler = ouch;                                           
    sigemptyset(&act.sa_mask);                                  
    act.sa_flags = 0;                                             
    sigaction(SIGINT, &act, 0);                                    

    do {                                                         
        cout << "." << endl;                                         
    } while(!unblock);                                                          

    cout << "EXIT" << endl;                                          
}

Now i compile the code and i get the “a.out”.
When i run the a.out like this:

[przemek@localhost test]$ ./a,out

it runs just as expected and when i press CTRL+C the program exits just as needed
But when i run the program like this (which is needed in my project):

[przemek@localhost test]$ ./a.out&

The os passes the control to the shell and i am unable to break the loop by CTRL+C

I need to run this code in bash script and i need to run it in background. Is it possible to somehow catch the signals in such situation?

  • 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-06T05:07:53+00:00Added an answer on June 6, 2026 at 5:07 am

    When you press ctrl-c you send signal 2 (SIGINT) to current process (the process that runs in foreground on the terminal). You can send the signal to process that runs in background (was started with &) using kill:

    $ kill -2 %%
    

    When you say %%you mean the last background process. Of course you can specify another one. You need to know its PID (see ps(1)) or JobID (bash(1)/jobs).

    I want to note also that you can use %-notation only inside shells with jobs management (e.g. in bash). When you are not in such a shell you can use only PIDs.

    The PID of the last started process is in $!. That may be useful in some scripts.

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

Sidebar

Related Questions

look at the following simple code: #include <iostream> #include <string> using namespace std; int
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have a very simple piece of code: #include <stdio.h> #include <glib.h> int main(int
Can anybody help me with this simple code?? #include <iostream> using namespace std; void
I have this simple code #include <stdio.h> #include <OpenGL/glext.h> #include <OpenGL/gl.h> int main (int
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
I have this very simple code: #include <stdio.h> #include <stdlib.h> int maxArr(int *arr) {
This is a simple code that open and print the file content. #include <stdio.h>
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:

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.