简体   繁体   English

sigkill不会终止C程序

[英]sigkill doesn't terminate C program

I'm under the impression that sigkill always works? 我觉得sigkill总是有效的吗? I have a signal handler in my program that displays an exit prompt when you press Ctrl-C. 我的程序中有一个信号处理程序,当您按Ctrl-C时会显示退出提示。 That works fine but how do I also make it work when I kill through the command line? 那很好,但是当我通过命令行杀死它时,我又如何使它工作呢? Right now the kill command does nothing. 现在,kill命令什么也不做。 Is there something I'm supposed to implement in my code to make kill work? 我应该在我的代码中实现某些东西以使kill工作吗?

You cannot set a signal handler for SIGKILL . 您不能为SIGKILL设置信号处理程序。

Your process does not get a chance to respond to SIGKILL . 您的过程没有机会响应SIGKILL It is unilaterally terminated immediately; 立即单方面终止; the process cannot do any clean up. 该过程无法进行任何清理。 The O/S releases the resources allocated to it, just as it does for any other dead process. O / S释放分配给它的资源,就像处理其他任何死进程一样。

See POSIX Signal Concepts , and sigaction() , and (most particularly) <signal.h> — which says that SIGKILL and SIGSTOP cannot be caught or ignored. 请参阅POSIX 信号概念sigaction()和(尤其是<signal.h> ,它表示不能捕获或忽略SIGKILLSIGSTOP


OTOH, you say "how do I also make it so when I kill through the command line it also works". OTOH,您说“我也要做到这一点,所以当我通过命令行杀死它时,它也可以工作”。 How do you plan to kill it? 您打算如何杀死它? Assuming you have the PID in $pid , then: 假设您的PID在$pid ,则:

kill $pid

sends a SIGTERM signal, and you can control which signal is sent by specifying it. 发送SIGTERM信号,您可以通过指定信号来控制发送哪个信号。 The Control-C key normally sends SIGINT , usually signal number 2. The other signal sent from the keyboard is SIGQUIT ( Control-\\ ) or the various job control signals. Control-C键通常发送SIGINT ,通常是信号编号2。从键盘发送的其他信号是SIGQUITControl- \\ )或各种作业控制信号。

If you use: 如果您使用:

kill -INT $pid

then that sends an interrupt ( SIGINT ) to the process (as would kill -2 $pid ). 然后向该进程发送一个中断( SIGINT )(将kill -2 $pid )。 The default is equivalent to kill -TERM $pid . 默认值等同于kill -TERM $pid You'd send SIGKILL with one of: 您会使用以下其中一种发送SIGKILL

kill -KILL $pid
kill -9 $pid

When you press control-C, that sends SIGINT, which your program can catch. 当您按Control-C时,将发送SIGINT,您的程序可以捕获该信息。
When you use the kill command, that sends SIGTERM by default, which your program can also catch. 使用kill命令时,默认情况下会发送SIGTERM,您的程序也可以捕获该命令。
But your program cannot catch or ignore SIGKILL. 但是您的程序无法捕获或忽略SIGKILL。 That's the one that's always guaranteed to be able to kill even a misbehaving program. 这是始终保证能够杀死行为不端程序的程序。

Read signal(7) & sigaction(2) and kill(1) (for the command) and kill(2) (for the system call). 读取signal(7)sigaction(2)kill(1) (对于命令)和kill(2) (对于系统调用)。

By default, SIGTERM is sent by the kill command (eg kill 1234 is sending SIGTERM , using kill(2) , to process of pid 1234). 默认情况下, SIGTERM由发送kill命令(例如kill 1234正在发送SIGTERM ,使用杀(2) ,以的PID 1234处理)。 Of course you can give an explicit signal in your kill command, eg kill -QUIT 1234 . 当然,您可以在kill命令中给出明确的信号,例如kill -QUIT 1234 You can catch it (eg SIGTERM or SIGQUIT ) by installing an appropriate signal handler. 您可以通过安装适当的信号处理程序来捕获它(例如SIGTERMSIGQUIT )。

Of course, you cannot catch SIGKILL (witch you could send with kill -KILL 1234 command), so you cannot set a signal handler for it (ie sigaction would give EINVAL error). 当然,你不能赶上SIGKILL (女巫你可以用发送kill -KILL 1234命令),所以你不能设置信号处理程序,它(即sigaction会给EINVAL错误)。 Sending a SIGKILL always kills -ie terminates- the receiving targeted process (and should be used on rogue processes). 发送SIGKILL 总是会杀死(即终止)接收目标进程(并且应在恶意进程上使用)。

Regarding Ctrl C it is a complex thing related to tty-s (generally it is sending a SIGINT signal). 关于Ctrl C,这是与tty-s有关的复杂事情(通常它正在发送SIGINT信号)。 Read the tty demystified ; 阅读揭秘的tty ; etc... See also termios(3) & stty(1) 等...另请参阅termios(3)stty(1)

In practice, take the habit to kill your processes first with SIGTERM , then with SIGQUIT and at last with SIGKILL . 在实践中,采取习惯kill与第一您的流程SIGTERM ,然后用SIGQUIT ,最后用SIGKILL Some processes (eg database servers) need some time to save a useful state to disk. 一些进程(例如数据库服务器)需要一些时间才能将有用的状态保存到磁盘。 So don't use kill -KILL at first (some processes might have an inconsistent state for their internal files, give them a chance to manage that with a "gentle" signal) 因此,不要一开始使用kill -KILL (某些进程的内部文件状态可能不一致,请给他们机会用“温和”的信号来管理它)

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

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