Signals
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
void ( *signal(int num, void (*handler)(int)) ) (int);
num
|
The signal number |
handler
|
The signal handler |
Return | The previous disposition on success; SIG_ERR on error |
Understanding the Signature:
handler
is a pointer to a function that returns nothing and is
passed an int
.
signal()
itself returns a pointer to a function that
returns nothing and is passed an int
.
signal()
typedef void (*signalhandler_t)(int);
signal()
:
sighandler_t signal(int num, sighandler_t handler);
SIG_DFL
:
SIG_IGN
:
signal()
:
read()
, wait()
) that are
interrupted by a signal do not resume when the handler
returns (instead they return immediately and
set errno
to EINTR
)
sigaction()
allows the caller to specify
all behaviors in a standard way (but it is complicated
to use)int sigaction(int num, const struct sigaction *act, struct sigaction *oact)
num
|
The signal number |
act
|
The action to be assoicated with the signal (or NULL) |
oact
|
The previous action assoicated with the signal (or NULL) |
Return | 0 on success; -1 on error |
Note: struct sigaction
contains a pointer to the signal handler,
the additional signals to be blocked, flags, etc...
kill -9 22807
sends signal 9 (i.e.,
SIGKILL
) to process 22807kill -19 -11975
sends signal 19 (i.e.,
SIGSTOP
) to process group 11975SIGINT
to every process in the foreground process group
(which, unless the default action has been modified, terminates
the process)SIGTSTP
to every process in the foreground process group
(which, unless the default action has been modified, suspends
the process)int kill(pid_t pid, int num)
pid
|
The destination of the signal, specifically the process ID (if > 0), all processes in the group of the sender (if 0), all processes (if -1), all processes in the absolute value of the given group ID (if < -1) |
num
|
Is the signal number |
Return | 0 on success; -1 on error |
Note: A signal will only be sent to processes for which the calling process has the appropriate permission
should
have in the two processes?
Why won't giving should
file scope fix it?
wait()
for the child to terminatewait()
The Goal: Sequence Task 1 Before Task 2
unixexamples/signals/sequence.cThe Race Condition: The child process might call kill()
before the parent process calls pause()
.
pause()
and pause()
will never return.
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
how
|
The change to perform |
set
|
The set of signals |
oldset
|
Will hold the old set of signals (if non-NULL) |
Return | 0 on success; -1 on error |
how
can be SIG_BLOCK
to make
blocked = blocked | set
, SIG_UNBLOCK
to make
blocked = blocked & ~set
, or SIG_SETMASK
to
make blocked = set
.
pause()
is invoked, in which
case pause()
will never return.
sigprocmask()
but before the call to
pause()
volatile
:
sig_atomic_t
: