kill

Header Files

#include <sys/types.h>
#include <signal.h>

Function Prototype

int kill(pid_t pid, int signo);

Function

kill send a signal to the specified process or process group

pid

pidbehavior
pid > 0send signal to the process
pid == 0send signal to all processes that is in the same process group as the caller
pid < 0send signal to process group with the id abs(pid)
pid == -1
broadcast signal to all processes the caller has permission to send

Permission to send signal

Only caller with right permission can send signal to the process

  • Superuser can send whatever it wants
  • Sender’s real UID or effective UID must equal to receiver’s real UID or saved set-UID

null signal (signo = 0)

When signo set to 0, then no signal will be send to specified pid. However, we’ll still do the check, like whether the process exist and do we have enough permission to send signal. If not, the return value will be -1 and errno will be set to ESRCH

Using this technique, we can detect whether a process exists

Side effect of kill

If there exist signals that are pending and unblocked, then one of them must be delivered before kill() return

只有用 kill 送 signal 給自己才會觸發 side effect (By Andromeda)


raise

raise(signo) equals kill(getpid(), signo)