kill function can be used to send a signal to another process.
In spite of its name, it can be used for a lot of things other than
causing a process to terminate. Some examples of situations where you
might want to send signals between processes are:
This section assumes that you know a little bit about how processes work. For more information on this subject, see Processes.
kill function is declared in signal.h.
killfunction sends the signal signum to the process or process group specified by pid. Besides the signals listed in Standard Signals, signum can also have a value of zero to check the validity of the pid.
The pid specifies the process or process group to receive the signal:
- The process whose identifier is pid.
- All processes in the same process group as the sender.
- The process group whose identifier is −pid.
- If the process is privileged, send the signal to all processes except for some special system processes. Otherwise, send the signal to all processes with the same effective user ID.
A process can send a signal to itself with a call like
killis used by a process to send a signal to itself, and the signal is not blocked, then
killdelivers at least one signal (which might be some other pending unblocked signal instead of the signal signum) to that process before it returns.
The return value from
killis zero if the signal can be sent successfully. Otherwise, no signal is sent, and a value of
-1is returned. If pid specifies sending a signal to several processes,
killsucceeds if it can send the signal to at least one of them. There's no way you can tell which of the processes got the signal or whether all of them did.
errnoerror conditions are defined for this function:
- The signum argument is an invalid or unsupported number.
- You do not have the privilege to send a signal to the process or any of the processes in the process group named by pid.
- The pid argument does not refer to an existing process or group.
This is similar to
kill, but sends signal signum to the process group pgid. This function is provided for compatibility with BSD; using
killto do this is more portable.
As a simple example of
kill, the call
kill (getpid (), sig
) has the same effect as