0% found this document useful (0 votes)
16 views

5 Signal Processing

The document discusses signals and signal handling in processes. Key points: - Signals are software interrupts that allow asynchronous events to be delivered to a process. - A process can ignore, catch, or use the default handler for each signal. - Signal handling functions should be reentrant to safely handle the same signal being delivered multiple times. - POSIX functions allow processes to manipulate blocked signal sets to safely handle signals while accessing shared resources.

Uploaded by

gayathri nath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

5 Signal Processing

The document discusses signals and signal handling in processes. Key points: - Signals are software interrupts that allow asynchronous events to be delivered to a process. - A process can ignore, catch, or use the default handler for each signal. - Signal handling functions should be reentrant to safely handle the same signal being delivered multiple times. - POSIX functions allow processes to manipulate blocked signal sets to safely handle signals while accessing shared resources.

Uploaded by

gayathri nath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

SIGNALS

 Signals are software interrupts.


 A robust program need to handle signals. This
is because signals are a way to deliver
asynchronous events to the application.
 A user hitting ctrl+c, a process sending a signal
to kill another process etc are all such cases
where a process needs to do signal handling.
Example :
A SIGINT signal that is generated when a user
presses ctrl+c. This is the way to terminate
programs from terminal.
SIGNAL PROCESSING
 Signals are the simplest form of inter process
communication
 allows a process to be asynchronously
interrupted by another process to handle
some event.
 Once the signal has been handled, the
interrupted process resumes from the point
of interruption.
Events
 An alarm set by the process has expired .
 The size of the terminal window has
changed.
When a process is signaled, the process can do
one of three things:
1. Ignore the signal - nothing will be done when
signal occurs
2. The signal can be caught. When this option is
chosen, then the process registers a function
with kernel. This function is called by kernel
when that signal occurs. kernel runs a special
part of the process before allowing the process
to continue (called catching a signal).
3. Let the kernel invoke its default action, which
depends on the particular signal being sent.
 two signals SIGKILL and SIGSTOP cannot be
ignored.
 This is because these two signals provide a
way for root user or the kernel to kill or stop
any process in any situation .
 The default action of these signals is to
terminate the process.
 Neither these signals can be caught nor can
be ignored.
WHY SIGNAL CATCHING FUNCTIONS SHOULD BE REENTRANT?

 A reentrant function is a function whose


execution can be stopped in between due to
any reason (like due to interrupt or signal)
and then can be reentered again safely
before its previous invocations complete the
execution.
SIGNAL CONCEPTS
 Signals have a well-defined life - created,
stored until the kernel can take an action
based on the signal.
 Creating a signal is variously called raising,
generating , or sending a signal.
 Process sends a signal to another process
 kernel generates a signal to send to a process.
 When a process sends itself a signal - raising the
signal.
 During the time between a signal being sent
and the signal causing an action to occur, the
signal is called pending .
 Once the signal is given to the target
process, the signal has been delivered .
SIMPLE SIGNALS
#include <signal.h>
void *signal(int signum, void * handler);
 signum - signal to handle
 handler defines the action to perform when the
signal is delivered.
 the handler is a pointer to a function that takes
no parameters and returns no value. When the
signal is delivered to the process, the kernel
executes the handler function as soon as
possible.
Signal Description Default Action

 SIGABRT Delivered by abort() Terminate,


core.
 SIGALRM alarm() has expired Terminate.
 SIGBUS Hardware-dependent Terminate,
error. core
 The handler can take on two special values,
SIG_IGN and SIG_DFL .
 If SIG_IGN is specified, the signal is ignored;
 SIG_DFL tells the kernel to perform the
default action for the signal - killing the
process or ignoring the signal.
 Two signals, SIGKILL and SIGSTOP , cannot be
caught. The kernel always performs the
default action for these two signals, killing
the process and stopping the process,
respectively.
REENTRANT
Process1
{
Signalalpha
{
handler
………….
<-------------Signalalpha
}
}
 Signalalpha is sent to a process (ie) already
running handler for signalalpha.
 Kernal interrupts the process and runs the
handler once again.
 Two problems:

1. Signal handler should run properly if it is


invoked while it is already running.
2. Global data structures or file
 Simple locking technique won’t allow
reentrancy.
 When the signal handler is called first time,
file is locked for writing.
 If the signal handler is invoked by another
signal the 2nd signal cannot hold the lock
because the 1st invocation signal wont
release the lock.
UNRELIABLE SIGNALS
 If two occurrences of the same signal
occurred quickly, Kernal handles the 2nd
signal in default fashion.
 2nd is ignored or the process was terminated.
POSIX SIGNALS
 Solution to multiple signals – wait to deliver
the 2nd signal until the process finishes
handling the first one.
 When the kernal is holding a signal for later
delivery the signal is said to be pending.
 But when the same signal is sent while it is
pending – only one will be delivered to the
process.
SIGNALS AND SYSTEM CALLS
 A signal is often delivered to a process that is
waiting for an external event to occur.
 Accept() waiting for another process to
connect to a socket.
 When the system administrator sends the
process a SIGTERM signal, the process could
handle it in a few ways:
1. No attempt to catch the signal and be
terminated by the kernel (the default
handling of SIGTERM).
 It could catch the signal, have the signal
handler clean up and then exit
 It could catch the signal, set a flag indicating
that the signal occurred, and somehow cause
the blocked system call (in this case,
accept() ) to exit with an error indicating
something unusual happened . The normal
execution pathway could then check for the
flag and handle it appropriately.
USING SIGSET_T

 The sigset_t data type is used to represent a


signal set.
 POSIX defines five functions for manipulating
signal sets:
#include <signal.h>
• int sigemptyset(sigset_t *set);
• int sigfillset(sigset_t *set);
• int sigaddset(sigset_t *set, int signum);
• int sigdelset(sigset_t *set, int signum);
• int sigismember(const sigset_t *set, int signum);
 int sigemptyset(sigset_t *set);
 Makes the signal set pointed to by set empty (no
signals are present in the set).
 int sigfillset(sigset_t *set);
 Includes all available signals in set .
 int sigaddset(sigset_t *set, int signum);
 Adds signal signum to set .
 int sigdelset(sigset_t *set, int signum);
 Removes signal signum from set .
 int sigismember(const sigset_t *set, int
signum);
 Returnsnon-0 if signal signum is in set , 0
otherwise .
 The only way any of these functions can
return an error is if their signum parameter
is an invalid signal.
 In that case, they return EINVAL .
CATCHING SIGNALS

 POSIX programs register signal handlers


through sigaction () .
• #include <signal.h>
• int sigaction(int signum, struct sigaction * act,
struct sigaction * oact);
 sets the handler for signal signum as defined by
act .
 If oact is not NULL , it is set to describe the
disposition of the signal before sigaction() was
called.
 If act is NULL , the current signal disposition is
left unchanged, allowing a program to discover
the current disposition for a signal without
modifying it.
 sigaction() returns 0 on success, non-0 on error.
 Errors occur only if one or more of the
parameters passed to sigaction() are invalid.
 The kernel's handling of a signal is fully
described by struct sigaction .
#include <signal.h>
struct sigaction
{
sighandler_t sa_handler;
sigset_t sa_mask;
int sa_flags;
};
 sa_handler is a pointer to a function with the
following prototype:
 void handler(int signum);
 signum is set to the signal number that
caused the function to be invoked.
 sa_handler can point to a function of this
type, or contain SIG_IGN or SIG_DFL .
 A program also specifies a set of signals that
should be blocked while the signal handler is
being run.
 If a signal handler is designed to handle
several different signals
 sa_mask is a signal set that includes all the
signals that should be blocked when the handler
for the signal is invoked.
 if you do not want it blocked, specify this
through the sa_flags member of struct sigaction .
SA_FLAGS
 SA_NOCLDSTOP
 SIGCHLD is generated when one of a process's
children has terminated or stopped
 If SA_NOCLDSTOP has been specified for the
SIGCHLD signal, the signal is generated only
when a child process has terminated;
 children stopped do not cause any signal.
SA_NOCLDSTOP has no effect on any other signal.
 SA_RESTART
 When the signal is sent to the process while it is
executing a slow system call, the system call is
restarted after the signal handler returns. If this
flag is not specified, the system call instead
returns an error and sets errno to EINTR .
 SA_ONESHOT
 Signal handler is reset to SIG_DFL.
MANIPULATING A PROCESS'S SIGNAL MASK
 It is common for a signal handler to
manipulate data structures that are used in
other parts of the program.
 Asynchronous nature of signals makes this
dangerous unless it is done carefully .
void handleHup(int signum)
{
free(someString);
someString = strdup("a different string");
}
Main program, (string copy)
src = someString;
while (*src)
*dest++ = *src++;
 When the main part of the program resumes
execution, src will be pointing to memory that
was freed by the signal handler.
 To solve this type of problem, the POSIX signal
API allows a process to block an arbitrary set of
signals from being delivered to the process.
 The signals are not thrown away—their delivery
is delayed until the process indicates it is willing
to handle those signals by unblocking them.
 To make the string copy shown earlier legal, the
program would have to block SIGHUP before the
string copy and unblock it afterward.
 The set of signals that a process is currently
blocking is often called the process's signal
mask .
 The signal mask for a process is a sigset_t
that contains the signals currently being
blocked.
 The sigprocmask () function allows a process
to control its current signal mask.
#include <signal.h>
int sigprocmask(int what, const sigset_t * set,
sigset_t * oldset);
 what - describes how the signal mask is to
be manipulated.
 If set is NULL, what is ignored.
 SIG_BLOCK - The signals in set are added to
the current signal mask.
 SIG_UNBLOCK - The signals in set are
removed from the current signal mask.
 SIG_SETMASK - Precisely the signals in set
are blocked—the rest are unblocked.
 The following finds the current signal mask
for the running process:
sigprocmask(SIG_BLOCK, NULL, &currentSet);
sigset_t hup;
sigemptyset(&hup);
sigaddset(&hup, SIGHUP);
sigprocmask(SIG_BLOCK, &hup, NULL);
src = someString;
while (*src)
*dest++ = *src++;
sigprocmask(SIG_UNBLOCK, &hup, NULL);
FINDING THE SET OF PENDING SIGNALS

#include <signal.h>
int sigpending(sigset_t * set);

 On return, the sigset_t pointed to by set


contains the signals currently pending.
WAITING FOR SIGNALS

#include <unistd.h>
int pause(void);
 pause() does not return until after a signal
has been delivered to the process.
 If a signal handler is present for that signal,
the signal handler is run before pause()
returns.
 pause() always returns -1 and sets errno to
EINTR .
#include <signal.h>
int sigsuspend(const sigset_t * mask);
 Like pause(), sigsuspend() suspends the process until a
signal has been received (and processed by a signal
handler, if one is available), returning -1 and setting
errno to EINTR .
 Unlike pause(), sigsuspend() temporarily sets the
process's signal mask to the value pointed to by the
mask parameter before waiting for a signal to occur.
 Once the signal occurs, the signal mask is restored to
the value it had before sigsuspend() was called.
 This allows a process to wait for a particular signal to
occur by blocking all other signals.

You might also like