Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 10.9 killandraiseFunctions 337


#include <signal.h>
int kill(pid_tpid,intsigno);
int raise(intsigno);
Both return: 0 if OK,−1 on error
The call
raise(signo);
is equivalent to the call
kill(getpid(), signo);
Thereare four different conditions for thepidargument tokill.

pid>0The signal is sent to the process whose process ID ispid.

pid==0The 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<0The signal is sent to all processes whose process group ID equals the
absolute value ofpidand for which the sender has permission to send the
signal. Again, the set of all processes excludes certain system processes,
as described earlier.

pid==−1The 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.

As we’ve mentioned, a process needs permission to send a signal to another
process. The superuser can send a signal to any process. For other users, the basic rule
is that the real or effective user ID of the sender has to equal the real or effective user ID
of the receiver.Ifthe implementation supports_POSIX_SAVED_IDS(as POSIX.1 now
requires), the saved set-user-ID of the receiver is checked instead of its effective user ID.
One special case for the permission testing also exists: if the signal being sent is
SIGCONT,aprocess can send it to any other process in the same session.
POSIX.1 defines signal number 0 as the null signal. If thesignoargument is 0, then
the normal error checking is performed bykill,but no signal is sent. This technique is
often used to determine if a specific process still exists. If we send the process the null
signal and it doesn’t exist,killreturns−1anderrnois set toESRCH.Beaware,
however,that UNIX systems recycle process IDs after some amount of time, so the
existence of a process with a given process ID does not necessarily mean that it’s the
process that you think it is.
Free download pdf