简体   繁体   English

C语言中的自定义信号处理程序

[英]Custom Signal Handler in C

Consider the following chunk of C code: 考虑以下C代码块:

void TERMHandler(int sig){
 signal (sig, SIG_DFL);
}

main() { 
 pid_t pid;
 pid = fork()

if (pid == 0) { 
     signal(SIGTERM,TERMHandler);
     while(1); 
  }
else
  {  
   sleep(3);
   kill(pid,SIGTERM);
   sleep(3);
   kill(pid,SIGTERM);
  }
}

We create a new process and distinguish between child (pid = 0) and parent. 我们创建一个新进程,并在子进程(pid = 0)和父进程之间进行区分。

Can a custom handler be used for every type of signals? 可以将自定义处理程序用于每种信号类型吗? If so, assuming we create a custom handler, is it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function? 如果是这样,假设我们创建了一个自定义处理程序,那么如果我只使用一次信号(或者从不重置信号处理程序),那么所有信号之间就不会有任何区别,因为它只会执行我的处理程序而不考虑功能中的信号?

What I'm trying to say is, is it right that: 我想说的是:

 signal(SIGTERM,CustomHandler);
 signal(SIGTSTP,CustomHandler);
 signal(SIGHUP,CustomHandler);
 ...

will execute the same code when the parent runs kill(pid, SomeSignal) ? 当父母运行kill(pid, SomeSignal)时将执行相同的代码?

Can a custom handler be used for every type of signals? 可以将自定义处理程序用于每种信号类型吗?

Yes, the same custom signal-handler function can be registered to handle different types of signals, up to and including all the signals that can be caught on the system in question. 是的,可以注册相同的自定义信号处理程序功能来处理不同类型的信号,最多可以包括所讨论系统上可以捕获的所有信号。 Note, however, that there may be defined signals that cannot be caught. 但是请注意,可能存在无法捕获的已定义信号。 On POSIX-conforming systems, for example, SIGKILL and SIGSTOP have this property. 例如,在符合POSIX的系统上, SIGKILLSIGSTOP具有此属性。

If so, assuming we create a custom handler, is it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function? 如果是这样,假设我们创建了一个自定义处理程序,那么如果我只使用一次信号(或者从不重置信号处理程序),那么所有信号之间就不会有任何区别,因为它只会执行我的处理程序而不考虑功能中的信号?

The signal handler function is not obligated to consider the signal number in determining what to do. 信号处理程序功能在确定操作时不必考虑信号编号。 It can perform the same action no matter what, or, as in your example function, it can simply pass the signal number on to some other function. 无论执行什么操作,它都可以执行相同的操作,或者像在示例功能中一样,它可以简单地将信号编号传递给其他功能。 You may or may not consider the latter to be a special case of the former. 您可能会也可能不会认为后者是前者的特例。

Do note, however, that on a POSIX system, the sigaction() function is preferable to signal() for modifying signal dispositions. 但是要注意,在POSIX系统上, sigaction()函数比signal()函数更可用于修改信号的位置。 Its behavior is both more flexible and more consistent than signal() 's over various operating systems. 在各种操作系统上,它的行为比signal()更加灵活和一致。

Can a custom handler be used for every type of signals? 可以将自定义处理程序用于每种信号类型吗?

Yes. 是。 You can install a custom "signal-catching" function for all signals which can be caught. 您可以为所有可以捕获的信号安装自定义的“信号捕获”功能 (For example, SIGKILL and SIGSTOP may not be caught.) (例如,可能不会捕获SIGKILL和SIGSTOP。)

[I]s it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function? [I]是对的,如果我只使用一次信号(或从不重置信号处理程序),所有信号之间就不会有任何区别,因为它只会执行我的处理程序,而无需考虑函数中的信号?

That depends on how you code your signal catching function. 这取决于您如何编码信号捕获功能。 The system will pass the caught signal to the function, so the same function could do something different upon catching a SIGTERM rather than a SIGHUP, for instance. 系统会将捕获到的信号传递给函数,因此,例如,在捕获SIGTERM而不是SIGHUP时,同一函数可能会做一些不同的事情。 If your handler ignores its sig argument and ignores the signal environment generally (masks, stacks, dispositions), then, yes, each invocation would be like any other. 如果您的处理程序忽略其sig参数,并且通常忽略信号环境(掩码,堆栈,处置),则可以,每次调用都将与其他调用相同。

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

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