信号是指示发生事件的过程的通知.信号也称为软件中断,无法预测它的发生,因此它也称为异步事件.
信号可以用数字或名称指定,通常信号名称以SIG开头.可以使用命令kill -l(列表信号名称为l)检查可用信号,如下所示 :
无论何时发出信号(以编程方式或系统生成的信号),都会执行默认操作.如果您不想执行默认操作但希望在接收信号时执行自己的操作,该怎么办?这对所有信号都有可能吗?是的,可以处理信号,但不能处理所有信号.如果你想忽略信号怎么办?这可能吗?是的,可以忽略该信号.忽略信号意味着既不执行默认操作也不处理信号.可以忽略或处理几乎所有信号.无法忽略或处理/捕获的信号是SIGSTOP和SIGKILL.
总之,对信号执行的操作如下:<
默认操作
处理信号
忽略信号
如上所述,可以处理信号,改变默认操作的执行.信号处理可以通过两种方式之一完成,即通过系统调用,signal()和sigaction().
#includetypedef void (*sighandler_t) (int);sighandler_t signal(int signum, sighandler_t handler);
系统调用signal()会在生成信号时调用注册的处理程序,如signum中所述.处理程序可以是SIG_IGN(忽略信号),SIG_DFL(设置信号返回默认机制)或用户定义的信号处理程序或函数地址之一.
此系统调用成功返回带整数参数且没有返回值的函数的地址.如果出现错误,此调用将返回SIG_ERR.
虽然使用signal()可以调用用户注册的相应信号处理程序,但需要进行微调,例如屏蔽应该阻止的信号,修改信号的行为,其他功能是不可能的.这可以使用sigaction()系统调用.
#include< signal.h> int sigaction(int signum,const struct sigaction * act,struct sigaction * oldact)
此系统调用用于检查或改变信号动作.如果act不为null,则从act中安装信号signum的新操作.如果oldact不为null,则前一个操作将保存在oldact中.
sigaction结构包含以下字段 :
字段1 : 处理程序在sa_handler或sa_sigaction中提到.
void(* sa_handler)(int); void(* sa_sigaction)(int,siginfo_t *,void *);
sa_handler的处理程序指定基于signum执行的操作,并指示SIG_DFL指示默认操作或SIG_IGN以忽略信号处理的信号或指针函数.
sa_sigaction的处理程序指定信号编号作为第一个参数,指向siginfo_t结构的指针作为第二个参数和指向用户上下文的指针(检查getcontext()或setcontext()以进一步细节)作为第三个参数.
结构siginfo_t包含信号信息,如要传递的信号编号,信号值,进程ID,发送过程的真实用户ID等.
Field 2 : 要阻止的信号集.
int sa_mask;
此变量指定在执行信号处理程序时应阻止的信号掩码.
第3栏 : 特殊标志.
int sa_flags;
此字段指定一组标志,用于修改信号的行为.
字段4 : 恢复处理程序.
void(* sa_restorer)(void);
此系统调用在成功时返回0,在失败的情况下返回-1.
让我们考虑一些示例程序.
首先,让我们从一个生成异常的示例程序开始.在这个程序中,我们试图执行除零操作,这会使系统生成异常.
/* signal_fpe.c */#includeint main() { int result; int v1, v2; v1 = 121; v2 = 0; result = v1/v2; printf("Result of Divide by Zero is %d\n", result); return 0;}
编译和执行步骤
Floating point exception (core dumped)
因此,当我们尝试执行算术运算时,系统已经生成了带有核心转储的浮点异常,这是信号的默认动作.
现在,让我们修改代码,使用signal()系统调用来处理这个特定信号.
/* signal_fpe_handler.c */#include#include #include void handler_dividebyzero(int signum);int main() { int result; int v1, v2; void (*sigHandlerReturn)(int); sigHandlerReturn = signal(SIGFPE, handler_dividebyzero); if (sigHandlerReturn == SIG_ERR) { perror("Signal Error: "); return 1; } v1 = 121; v2 = 0; result = v1/v2; printf("Result of Divide by Zero is %d\n", result); return 0;}void handler_dividebyzero(int signum) { if (signum == SIGFPE) { printf("Received SIGFPE, Divide by Zero Exception\n"); exit (0); } else printf("Received %d Signal\n", signum); return;}
编制和执行步骤
Received SIGFPE, Divide by Zero Exception
如上所述,信号由系统生成(执行某些操作,例如除以零等)或用户可以也以编程方式生成信号.如果你想以编程方式生成信号,请使用库函数raise().
现在,让我们采用另一个程序来演示处理和忽略信号.
假设我们使用raise()引发了一个信号,那么会发生什么?在发出信号之后,停止执行当前过程.然后停止的过程会发生什么?可以有两种情况 - 首先,在需要时继续执行.其次,终止(使用kill命令)进程.
要继续执行已停止的进程,请将SIGCONT发送到该特定进程.您还可以发出fg(foreground)或bg(background)命令来继续执行.这里,命令只会重新开始执行最后一个进程.如果停止多个进程,则仅恢复最后一个进程.如果要恢复先前停止的进程,则恢复作业(使用fg/bg)和作业编号.
以下程序用于使用raise()引发信号SIGSTOP功能.用户按下CTRL + Z(Control + Z)键也可以生成信号SIGSTOP.发出此信号后,程序将停止执行.发送信号(SIGCONT)继续执行.
在以下示例中,我们使用命令fg恢复已停止的进程.
/* signal_raising.c */#include#include #include int main() { printf("Testing SIGSTOP\n"); raise(SIGSTOP); return 0;}
编制和执行步骤
Testing SIGSTOP[1]+ Stopped ./a.out./a.out
现在,增强之前的程序以继续通过从另一个终端发出SIGCONT来执行已停止的进程.
/* signal_stop_continue.c */#include#include #include #include void handler_sigtstp(int signum);int main() { pid_t pid; printf("Testing SIGSTOP\n"); pid = getpid(); printf("Open Another Terminal and issue following command\n"); printf("kill -SIGCONT %d or kill -CONT %d or kill -18 %d\n", pid, pid, pid); raise(SIGSTOP); printf("Received signal SIGCONT\n"); return 0;}
编制和执行步骤
Testing SIGSTOPOpen Another Terminal and issue following commandkill -SIGCONT 30379 or kill -CONT 30379 or kill -18 30379[1]+ Stopped ./a.outReceived signal SIGCONT[1]+ Done ./a.out
在另一个终端
In another terminal
kill -SIGCONT 30379
到目前为止,我们已经看到了处理系统生成的信号的程序。 现在,让我们看看通过程序(使用raise()函数或通过kill命令)生成的信号。 该程序生成信号SIGTSTP(终端停止),其默认操作是停止执行。 但是,由于我们现在正在处理信号而不是默认操作,因此它将到达已定义的处理程序。 在这种情况下,我们只是打印消息并退出。
/* signal_raising_handling.c */#include#include #include void handler_sigtstp(int signum);int main() { void (*sigHandlerReturn)(int); sigHandlerReturn = signal(SIGTSTP, handler_sigtstp); if (sigHandlerReturn == SIG_ERR) { perror("Signal Error: "); return 1; } printf("Testing SIGTSTP\n"); raise(SIGTSTP); return 0;}void handler_sigtstp(int signum) { if (signum == SIGTSTP) { printf("Received SIGTSTP\n"); exit(0); } else printf("Received %d Signal\n", signum); return;}
Compilation and Execution Steps
Testing SIGTSTPReceived SIGTSTP
我们已经看到了执行默认操作或处理信号的实例。 现在,该忽略信号了。 在这里,在此示例程序中,我们通过SIG_IGN注册了要忽略的信号SIGTSTP,然后引发了信号SIGTSTP(终端停止)。 当信号SIGTSTP产生时,将被忽略。
/* signal_raising_ignoring.c */#include#include #include void handler_sigtstp(int signum);int main() { void (*sigHandlerReturn)(int); sigHandlerReturn = signal(SIGTSTP, SIG_IGN); if (sigHandlerReturn == SIG_ERR) { perror("Signal Error: "); return 1; } printf("Testing SIGTSTP\n"); raise(SIGTSTP); printf("Signal SIGTSTP is ignored\n"); return 0;}
Compilation and Execution Steps
Testing SIGTSTPSignal SIGTSTP is ignored
到目前为止,我们已经观察到我们有一个信号处理程序来处理一个信号。 我们可以有一个处理程序来处理多个信号吗? 答案是肯定的。 让我们考虑一个程序。
以下程序执行以下操作:
步骤1:注册一个处理程序(handleSignals)以捕获或处理信号SIGINT(CTRL + C)或SIGQUIT(CTRL + \)
步骤2:如果用户生成信号SIGQUIT(通过终止命令或使用CTRL + \的键盘控制),则处理程序仅将消息打印为return。
步骤3:如果用户第一次生成信号SIGINT(通过终止命令或使用CTRL + C的键盘控制),则它将修改信号以从下一次执行默认操作(使用SIG_DFL)。
步骤4:如果用户第二次生成信号SIGINT,它将执行默认操作,即终止程序。
/* Filename: sigHandler.c */#include#include #include void handleSignals(int signum);int main(void) { void (*sigHandlerInterrupt)(int); void (*sigHandlerQuit)(int); void (*sigHandlerReturn)(int); sigHandlerInterrupt = sigHandlerQuit = handleSignals; sigHandlerReturn = signal(SIGINT, sigHandlerInterrupt); if (sigHandlerReturn == SIG_ERR) { perror("signal error: "); return 1; } sigHandlerReturn = signal(SIGQUIT, sigHandlerQuit); if (sigHandlerReturn == SIG_ERR) { perror("signal error: "); return 1; } while (1) { printf("\nTo terminate this program, perform the following: \n"); printf("1. Open another terminal\n"); printf("2. Issue command: kill %d or issue CTRL+C 2 times (second time it terminates)\n", getpid()); sleep(10); } return 0;}void handleSignals(int signum) { switch(signum) { case SIGINT: printf("\nYou pressed CTRL+C \n"); printf("Now reverting SIGINT signal to default action\n"); signal(SIGINT, SIG_DFL); break; case SIGQUIT: printf("\nYou pressed CTRL+\\ \n"); break; default: printf("\nReceived signal number %d\n", signum); break; } return;}
Compilation and Execution Steps
To terminate this program, perform the following:1. Open another terminal2. Issue command: kill 74 or issue CTRL+C 2 times (second time it terminates)^CYou pressed CTRL+CNow reverting SIGINT signal to default action To terminate this program, perform the following:1. Open another terminal2. Issue command: kill 74 or issue CTRL+C 2 times (second time it terminates)^\You pressed CTRL+\To terminate this program, perform the following:1. Open another terminal2. Issue command: kill 120Terminated
Another Terminal
kill 71
Second Method
To terminate this program, perform the following:1. Open another terminal2. Issue command: kill 71 or issue CTRL+C 2 times (second time it terminates)^CYou pressed CTRL+CNow reverting SIGINT signal to default actionTo terminate this program, perform the following:1. Open another terminal2. Issue command: kill 71 or issue CTRL+C 2 times (second time it terminates)
我们知道,处理信号,我们有两个系统调用,即,无论是信号()或sigaction的()。到现在为止,我们已经看到了信号()系统调用,现在是时候的sigaction()系统调用。让我们修改上面的程序中使用的sigaction()如下进行:
/* Filename: sigHandlerSigAction.c */#include#include #include void handleSignals(int signum);int main(void) { void (*sigHandlerReturn)(int); struct sigaction mysigaction; mysigaction.sa_handler = handleSignals; sigemptyset(&mysigaction.sa_mask); mysigaction.sa_flags = 0; sigaction(SIGINT, &mysigaction, NULL); if (mysigaction.sa_handler == SIG_ERR) { perror("signal error: "); return 1; } mysigaction.sa_handler = handleSignals; sigemptyset(&mysigaction.sa_mask); mysigaction.sa_flags = 0; sigaction(SIGQUIT, &mysigaction, NULL); if (mysigaction.sa_handler == SIG_ERR) { perror("signal error: "); return 1; } while (-1) { printf("\nTo terminate this program, perform either of the following: \n"); printf("1. Open another terminal and issue command: kill %d\n", getpid()); printf("2. Issue CTRL+C 2 times (second time it terminates)\n"); sleep(10); } return 0;}void handleSignals(int signum) { switch(signum) { case SIGINT: printf("\nYou have entered CTRL+C \n"); printf("Now reverting SIGINT signal to perform default action\n"); signal(SIGINT, SIG_DFL); break; case SIGQUIT: printf("\nYou have entered CTRL+\\ \n"); break; default: printf("\nReceived signal number %d\n", signum); break; } return;}
让我们看看编译和执行过程。 在执行过程中,让我们两次查看CTRL + C问题,其余检查/方式(如上所述)也可以尝试该程序。
Compilation and Execution Steps
To terminate this program, perform either of the following:1. Open another terminal and issue command: kill 31992. Issue CTRL+C 2 times (second time it terminates)^CYou have entered CTRL+CNow reverting SIGINT signal to perform default actionTo terminate this program, perform either of the following:1. Open another terminal and issue command: kill 31992. Issue CTRL+C 2 times (second time it terminates)