Sigaction: by Prithiviraj.M
Sigaction: by Prithiviraj.M
By
Prithiviraj.M
SIGACTION
The sigaction function can be used to set a
signal disposition in a more controlled way
at the cost of complexity
Function Format
int sigaction ( int signum, const struct
sigaction *act, struct sigaction *oldact)
Description
The first argument, signum, is a
specified signal; the second
argument, sigaction, is used to set
the new action of the signal signum;
and the third argument is used to
store the previous action, usually
NULL
STRUCTURE
SIGACTION
struct sigaction
{
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *,
void*);
sigset_t sa_mask; int sa_flags;
}
FACTORS
In most cases, this consists simply of
recording the fact that a signal
occurred.
The main program then checks
periodically whether a signal has
occurred and reacts accordingly.
SIGACTION
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
sig_atomic_t sigusr1_count = 0;
void handler (int signal_number)
{
++sigusr1_count;
}
int main ()
{
struct sigaction sa;
sa.sa_handler = &handler;
sigaction (SIGINT, &sa, NULL);
printf (SIGUSR1 was raised %d times\n,
sigusr1_count);
return 0;
}
Process Termination
A process may also terminate abnormally, in
response to a signal. For instance, the SIGBUS,
SIGSEGV, and SIGFPE signals mentioned
previously cause the process to terminate.
system calls
A process can wait for one of its
child process to finish by executing
the wait system call.
int wait(int *status)