DOC PREVIEW
U of I CS 241 - Signal Handling

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Signal HandlingCS241 AdministrativeOutlineReview: How Signals WorkReview: Generating SignalsExamples: Programming SignalsProgramming SignalsSignal MasksSignal SetsSignal SetsSignal MasksSIGPROCMASKExample: Initialize Signal Set: Example: Add SIGINT to Set of Blocked Signals SummaryCS 241 Spring 2007System Programming1Signal HandlingLecture 15Klara Nahrstedt2CS241 Administrative Read Chapter 8.1-8.4 in R&R3Outline Learn fundamentals of signal handling Experiment with signals for control Explore POSIX signal facilities Use signal masks and handlers Signals and threads4Review: How Signals WorkSignal GeneratedProcessSignal HandlerSignal deliveredSignal not blockedSignal Caught by handlerReturn from Signal HandlerProcess ResumedSignalMaskSignalMaskSignalMask5Review: Generating Signals Signal has a symbolic name starting with SIG Signal names are defined in signal.hUsers can generate signals (e.g., SIGUSR1)OS generates signals when certain errors occur (e.g., SIGSEGV – invalid memory reference)Specific calls generate signals such as alarm (e.g., SIGALARM)6Examples: Programming SignalsFrom a program you can use the kill system call: #include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig);Example 8.4: send SIGUSR1 to process 3423: if (kill(3423, SIGUSR1) == -1) perror("Failed to send the SIGUSR1 signal"); Example 8.5: a child kills its parent: if (kill(getppid(), SIGTERM) == -1) perror ("Failed to kill parent");7Programming SignalsExample 8.5: a process sends a signal to itself: if (raise(SIGUSR1) != 0) perror("Failed to raise SIGUSR1"); Example 8.8: kill an infinite loop after 10 seconds: int main(void) { alarm(10);for ( ; ; ) ; }8Signal Masks Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains a set of signals currently blocked. Important! Blocking a signal is different from ignoring signal. Why?  When a process blocks a signal, the OS does not deliver signal until the process unblocks the signal A blocked signal is not delivered to a process until it is unblocked.  When a process ignores signal, signal is delivered and the process handles it by throwing it away.9Signal Sets Signal set is of type sigset_t Signal sets are manipulated by five functions:#include <signal.h> int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); int sigismember(const sigset_t *set, intsigno);10Signal Setssigemptyset initializes the set to contain no signalssigfillset puts all signals in the setsigaddset adds one signal to the setsigdelset removes one signal from the setsigismember tests to see if a signal is in the set11Signal MasksSigInt SigQuit SigKill … SigCont SigAbrt0 0 1 … 1 0Signal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20SigMaskA SIGSET is a collection of signals: #000003 is SIGHUP + SIGINT12SIGPROCMASK The function sigprocmask is used to modify the signal mask. #include <signal.h>int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oset)‘how’ specifies the manner in which the signal mask is to be modifiedSIG_BLOCK – add collection of signals to those currently blockedSIG_UNBLOCKED – delete collection of signals from the currently blockedSIG_SETMASK – set collection of signals being blocked to the specified set13Example: Initialize Signal Set: if ((sigemptyset(&twosigs) == -1) || (sigaddset(&twosigs, SIGINT) == -1) || (sigaddset(&twosigs, SIGQUIT) == -1)) perror("Failed to set up signal mask");14Example: Add SIGINT to Set of Blocked Signals sigset_t newsigset; if ((sigemptyset(&newsigset) == -1) || (sigaddset(&newsigset, SIGINT) == -1)) perror("Failed to initialize the signal set"); else if (sigprocmask(SIG_BLOCK, &newsigset, NULL) == -1) perror("Failed to block SIGINT");If SIGINT is already blocked, the call tosigprocmask has no effect.15Summary  Signals – asynchronous events Generating signals Programming signals Signal


View Full Document

U of I CS 241 - Signal Handling

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Signal Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Signal Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Signal Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?