













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These lecture notes cover the topic of signal handling and threads in the context of system programming for cs 241 at the university of illinois at urbana-champaign, spring 2007. Information on using signal masks and handlers, signals and threads, and the sigaction function. It also covers waiting for signals and the use of sigsuspend and sigwait.
Typology: Study notes
1 / 21
This page cannot be seen from the preview
Don't miss anything!














CS 241 Spring 2007System Programming
CS241 Administrative ^ Read Chapter 8 in R&R and Chapter 13.5 inR&R ^ SMP4 due on Monday, 2/26 ^ Homework 1 will be posted this evening, 2/23 ^ Deadline for Homework 1 is Friday, March2, at 5pm^ ^
Homework 1 is a preparation for Midterm Homework 1 is an individual effort – NOWORKING IN GROUPS/PAIRS!! ^ MIDTERM – MONDAY, March 5, 11am
Catching and Ignoring Signals -SIGACTION ^ sigaction
function allows the caller to examine or specify action associated witha specific signal Program installs
signal handler
by calling
sigaction
with the name of a user-written function. The function
sigaction
is used to specify
what is to happen to a signal when it isdelivered.
SIGACTION
Process
Default Signal Handler
SignalMask^ Process Resumed
SignalMask
SignalMask
New Signal Handler
SignalMask
System Booking Keeping
SIGACTION changes Signal handler (and returns old handler)
Example: Set up Signal Handler for SIGINT struct sigaction newact; newact.sa_handler = mysighand
; /set new handler/
newact.sa_flags = 0;
/* no special options */
if (( sigemptyset(&newact,sa_mask)
== -1) ||
( sigaction(SIGINT, &newact, NULL)
== -1))
perror(“Failed to install SIGINT signal handler”);
Set up Signal Handler that CatchesSIGINT Generated by Ctrl-C void^
catchctrlc(int signo)
char handmsg[] = "I found Ctrl-C\n"; int msglen = sizeof(handmsg); write(STDERR_FILENO, handmsg, msglen); } … struct sigaction act; act.sa_handler = catchctrlc; act.sa_flags = 0; if (( sigemptyset(&act.sa_mask)
( sigaction(SIGINT, &act, NULL)
perror("Failed to set SIGINT to handle Ctrl-C"); Note: write is async-signal safe – meaning it can be called inside a signalhandler.^ Not so for
printf^
or strlen.
sigsuspend sigsuspend
function sets signal mask and suspends process until signal is caught by theprocess sigsuspend
returns when signal handler of the caught signal returns #include <signal.h> int sigsupend(const sigset_t *sigmask);
Example: sigsuspend What’s wrong? sigfillset(&sigmost); sigdelset(&sigmost, signum); sigsuspend(&sigmost); signum
is the only signal unblocked that can cause
sigsuspend
to return
If the signal
signum
is delivered before the start of
the code segment, the process still suspendsitself and deadlocks if another
signum
is not
generated
Use sigwait This function is much cleaner and avoids races and errors !!!! 1. First block all signals 2. Put the signals you want to wait for in
sigset_t
sigwait
4.^ sigwait
blocks the process until at least one of these signals is pending. 5. It removes one of the pending signals and gives you thecorresponding signal number in the second parameter.. 6. Do what you want: no signal handler needed. 7. It returns 0 on success and -1 on error with
errno
set.
#include <signal.h> int sigwait(const sigset_t restrict sigmask, intrestrict signo);
Signal Handling & Threads (Ch13.5 p473-475)^ type
Delivery action
asynchronous
Delivered to some thread that has itunblocked synchronous
Delivered to thread that caused it
directed
Delivered to the identified thread
Masking Signals for Threads ^ Signal handlers are process-wide ^ Each thread has its own signal mask forprocess wide signals – only use sigprocmaskfor initialization before creating threads ^ Thread can^ ^
Examine its signal mask Set its signal mask ^ sigprocmask
function should not be used
when the process has multiple threads
Masking Signals for Threads #include <pthread.h> #include <signal.h> int pthread_sigmask(int how,
**const sigset_t restrict set, sigset_t restrict pset);
If ‘how
’ is^ SIG_SETMASK
, then thread’s signal mask is
replaced by ‘
set’
If ‘how
’ is^ SIG_BLOCK
, then additional signals in ‘
set’ are
blocked by the thread If ‘how
’ is^ SIG_UNBLOCK
, then any of blocked signals in ‘
set’
are removed from the thread’s current signal mask
Dedicating Threads for Signal Handling ^ Signal handlers are process-wide and installed with^ sigaction
as in single-threaded processes ^ Distinction between process-wide signal handlers andthread-specific signal masks is important!!! ^ Remember:^ ^
When a signal is caught, the signal that caused the event isautomatically blocked on entry to the signal handler With multi-threaded application, nothing prevents anothersignal of the same type from being delivered to another threadthat has the signal unblocked ^ A recommended strategy for dealing with signals inmulti-threaded processes is to dedicate particularthreads to signal handling
Dedicating Threads for Signal Handling ^ Dedicate particular threads to handlesignals ^ Main thread blocks all signals beforecreating any child threads ^ Signal mask is inherited by children ^ Thread dedicated to signal handlingexecutes
sigwait
on specific signals to
be handled or uses
pthread_sigmask
to
unblock signal