kill - send signal to a process
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
//return: 0 if OK, −1 on error
raise - send a signal to the caller
#include <signal.h>
int raise(int sig);
//return: 0 if OK, −1 on error
raise(signo)
等价于kill(getpid(), signo)
4 conditions pid in kill
different conditions | 解释 |
---|---|
pid > 0 | The signal is sent to the process whose process ID is pid. |
pid == 0 | The signal is sent to all processes whose process group ID equals the process group ID of the sender and for which the sender has permission to send the signal. Note that the term all processes excludes an implementation-defined set of system processes. For most UNIX systems, this set of system processes includes the kernel processes and init (pid 1). |
pid < 0 | The signal is sent to all processes whose process group ID equals the absolute value of pid and for which the sender has permission to send the signal. Again, the set of all processes excludes certain system processes, as described earlier. |
pid == −1 | The signal is sent to all processes on the system for which the sender has permission to send the signal. As before, the set of processes excludes certain system processes. |
signo参数
signo为0时,kill会执行一般的错误检查,但不发送信号。
用途: 用来检查特定的进程是否仍然存在。
如果不存在,kill
返回-1,errno
被设置为ESRCH
进程的存在不是atomic
原子性的。