原文:https://github.com/angrave/SystemProgramming/wiki/Signals%2C-Part-3%3A-Raising-signals
您已经知道发送SIG_INT
的一种方法只需键入CTRL-C
从 shell 中可以使用kill
(如果您知道进程 ID)和killall
(如果您知道进程名称)
# First let's use ps and grep to find the process we want to send a signal to
$ ps au | grep myprogram
angrave 4409 0.0 0.0 2434892 512 s004 R+ 2:42PM 0:00.00 myprogram 1 2 3
#Send SIGINT signal to process 4409 (equivalent of `CTRL-C`)
$ kill -SIGINT 4409
#Send SIGKILL (terminate the process)
$ kill -SIGKILL 4409
$ kill -9 4409
killall
类似,只是它与程序名称匹配。接下来的两个例子,发送SIGINT
然后发送SIGKILL
来终止正在运行的进程myprogram
# Send SIGINT (SIGINT can be ignored)
$ killall -SIGINT myprogram
# SIGKILL (-9) cannot be ignored!
$ killall -9 myprogram
使用raise
或kill
int raise(int sig); // Send a signal to myself!
int kill(pid_t pid, int sig); // Send a signal to another process
对于非 root 进程,信号只能发送给同一用户的进程,即你不能只是 SIGKILL 我的进程!有关详细信息,请参阅 kill(2),即 man -s2。
使用pthread_kill
int pthread_kill(pthread_t thread, int sig)
在下面的示例中,执行func
的新创建的线程将被SIGINT
中断
pthread_create(&tid, NULL, func, args);
pthread_kill(tid, SIGINT);
pthread_kill(pthread_self(), SIGKILL); // send SIGKILL to myself
它会杀死整个过程。虽然各个线程可以设置信号掩码,但信号配置(每个信号执行的处理程序/动作表)是 _ 每个进程 _ 而不是 _ 每个线程 _。这意味着可以从任何线程调用sigaction
,因为您将为进程中的所有线程设置信号处理程序。
您可以异步或同步选择句柄待处理信号。
安装信号处理程序以异步处理信号使用sigaction
(或者,对于简单的例子,signal
)。
要同步捕获待处理信号,请使用sigwait
(阻塞直到发送信号)或signalfd
(它还会阻塞并提供可以read()
检索待处理信号的文件描述符)。
有关使用sigwait
的示例,请参阅Signals, Part 4