

#include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig);
kill sends a signal to an OpenEdition process. pid is the
process ID of the recipient. If pid is greater than 0, kill
sends a signal to a process whose ID equals pid. If pid is 0,
kill sends the signal to all processes whose process group ID is
equal to that of the sender, for which the sender has the necessary privileges
to send a signal. If pid is - 1, kill returns a
- 1. If pid is less than - 1, kill sends a
signal to all processes whose process group ID equals the absolute value of
pid, for which the sender has the necessary privileges.
sig is the signal. sig must be 0, or a signal number defined
in <signal.h>. The signal number must be one recognized by
OpenEdition, not a signal defined by SAS/C. If sig is 0, kill
performs error checking only, and does not send a signal.
A process can also send a kill signal to itself. In this case, at
least one pending unblocked signal is delivered to the sender if the
signal is not blocked or ignored.
You can only use the kill function to send a signal supported by
OpenEdition; however, the signal need not have been assigned to
OpenEdition by oesigsetup.
kill returns a 0 if it has permission to send a signal.
kill returns a - 1 if it is not successful.
fork to create a new process, and
it uses kill to terminate the new process if it fails to
terminate in ten seconds. This example uses oesigsetup to
force the signals SIGALRM and SIGTERM to be managed by OpenEdition:
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <lcsignal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
static void dospin(void); /* child process code */
static void alrmhdlr(int); /* SIGALRM handler */
static void termhdlr(int); /* SIGTERM handler */
static pid_t child;
static volatile int counter; /* counter for child process */
main() {
int status;
pid_t ended;
sigset_t oesigs, sascsigs;
sigemptyset(&oesigs);
sigemptyset(&sascsigs);
sigaddset(&oesigs, SIGALRM);
sigaddset(&oesigs, SIGTERM);
oesigsetup(&oesigs, &sascsigs); /* Define SIGALRM and SIGTERM */
/* as OE-managed signals. */
child = fork(); /* Create child process. */
if (child == -1) {
perror("fork error");
exit(EXIT_FAILURE);
}
if (child == 0) dospin(); /* This runs in the child. */
else { /* This runs in the parent. */
signal(SIGALRM, &alrmhdlr);
alarm(10); /* Set alarm for 10 seconds. */
ended = wait(&status);
if (ended == -1 && errno != EINTR) {
/* Check for non-signal failure. */
perror("wait error");
abort();
}
exit(0);
}
}
void alrmhdlr(int signum) { /* parent SIGALRM handler */
pid_t ended;
int rc;
int status;
rc = kill(child, SIGTERM); /* Send SIGTERM to child. */
if (rc != 0) {
perror("kill");
abort();
}
ended = wait(&status); /* Wait for it to quit. */
if (ended == -1 && errno != EINTR) {
/* Check for non-signal failure. */
perror("wait error");
abort();
}
return; /* to point of signal */
}
void dospin(void) {
signal(SIGTERM, &termhdlr); /* Define SIGTERM handler. */
for (;;) { /* busy loop, waiting for SIGTERM signal */
++counter;
if (counter < 0) { /* Avoid looping absolutely forever. */
fputs("Counter overflowed, child terminating!n", stderr);
exit(EXIT_FAILURE);
}
sigchk(); /* Make sure signal is discovered. */
}
}
void termhdlr(int signum) { /* handler for termination request */
fprintf(stderr, "Termination signal received, counter = %dn",
counter);
exit(EXIT_SUCCESS);
}
abort, raise, siggen
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.