Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both | POSIX(ON) |
#define _POSIX_SOURCE
#include <signal.h>
int kill(pid_t pid, int sig);
Sends a signal to a process or process group. A process has permission to send a signal if the real or effective user ID of the sender is the same as the real or effective user ID of the intended recipient. A process can also send signals if it has appropriate privileges. If _POSIX_SAVED_IDS is defined in the unistd.h header file, the saved set user ID of the intended recipient is checked instead of its effective user ID.
Regardless of user ID, a process can always send a SIGCONT signal to a process that is a member of the same session (same session ID) as the sender.
You can use either signal() or sigaction() to specify how a signal will be handled when kill() is invoked.
A process can use kill() to send a signal to itself. If the signal is not blocked or ignored, at least one pending unblocked signal is delivered to the sender before kill() returns. If there are no other pending unblocked signals, the delivered signal is sig.
This function is supported only in a POSIX program. You can use it to pass SIGIOERR.
The use of the SIGTHSTOP and SIGTHCONT signal is not supported with this function.
kill() returns 0 if it has permission to send sig to any of the processes specified by pid.
⁄* CELEBK01 *⁄
#define _POSIX_SOURCE
#include <signal.h>
#include <stdio.h>
#include <sys⁄types.h>
#include <unistd.h>
#include <sys⁄wait.h> ⁄*FIX: used to be <wait.h>*⁄
main() {
sigset_t sigset;
int p[2], status;
char c='z';
pid_t pid;
if (pipe(p) != 0)
perror("pipe() error");
else {
if ((pid = fork()) == 0) {
sigemptyset(&sigset);
puts("child is letting parent know he's ready for signal");
write(p[1], &c, 1);
puts("child is waiting for signal");
sigsuspend(&sigset);
exit(0);
}
puts("parent is waiting for child to say he's ready for signal");
read(p[0], &c, 1);
puts("child has told parent he's ready for signal");
kill(pid, SIGTERM);
wait(&status);
if (WIFSIGNALED(status))
if (WTERMSIG(status) == SIGTERM)
puts("child was ended with a SIGTERM");
else
printf("child was ended with a %d signal\n", WTERMSIG(status));
else puts("child was not ended with a signal");
close(p[0]);
close(p[1]);
}
}
parent is waiting for child to say he's ready for signal
child is letting parent know he's ready for signal
child is waiting for signal
child has told parent he's ready for signal
child was ended with a SIGTERM