简体   繁体   English

sigaction SIGINT,在带有&(背景)的shell中运行的程序未收到信号

[英]sigaction SIGINT, programm run in shell with & (background) doesn't get the signal

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". 现在,我编译代码并得到“ a.out”。 When i run the a.out like this: 当我像这样运行a.out时:

[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): 它按预期运行,当我按CTRL + C时,程序按需要退出。但是当我运行这样的程序(在我的项目中需要)时:

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

The os passes the control to the shell and i am unable to break the loop by CTRL+C 操作系统将控制权传递给外壳,而我无法通过CTRL + C来中断循环

I need to run this code in bash script and i need to run it in background. 我需要在bash脚本中运行此代码,并且需要在后台运行它。 Is it possible to somehow catch the signals in such situation? 在这种情况下是否可能以某种方式捕获信号?

When you press ctrl-c you send signal 2 (SIGINT) to current process (the process that runs in foreground on the terminal). 当您按ctrl-c您将信号2(SIGINT)发送到当前进程(该进程在终端的前台运行)。 You can send the signal to process that runs in background (was started with &) using kill : 您可以使用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). 您需要知道其PID(请参阅ps(1))或JobID(bash(1)/ jobs)。

I want to note also that you can use %-notation only inside shells with jobs management (eg in bash ). 我还要指出,您只能在具有作业管理功能的shell中使用%表示法(例如,在bash )。 When you are not in such a shell you can use only PIDs. 如果您不在这种外壳中,则只能使用PID。

The PID of the last started process is in $! 上次启动的进程的PID在$! . That may be useful in some scripts. 在某些脚本中这可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM