SignalsIntroduction to SignalsHow Signals WorkExamples of POSIX Required SignalsSlide 5Generating SignalsCommand Line Generates SignalsCommand Line Generates SignalsTimers Generate SIGALRM SignalsExamples: Programming SignalsProgramming SignalsSignal MasksSignal SetsSlide 14Slide 15SIGPROCMASKExample: Initialize Signal Set:Example: Add SIGINT to Set of Blocked SignalsCatching and Ignoring Signals - SIGACTIONSlide 20Example: Set up Signal Handler for SIGINTSet up Signal Handler that Catches SIGINT Generated by Ctrl-CWaiting for SignalssigsuspendExample: sigsuspendExample: sigsuspend (Correct Way to Wait for Single Signal)Use sigwaitCopyright ©: Nahrstedt, Angrave, Abdelzaher 1SignalsCopyright ©: Nahrstedt, Angrave, Abdelzaher2Introduction to SignalsWhat is Signal? A signal is a software notification to a process of an event. Why do we need Signals? In systems we need to enable asynchronous eventsExamples of asynchronous events: Email message arrives on my machine – mailing agent (user) process should retrieveInvalid memory access happens – OS should inform scheduler to remove process from the processorAlarm clock goes on – process which sets the alarm should catch itCopyright ©: Nahrstedt, Angrave, Abdelzaher3How Signals WorkSignal GeneratedProcessSignal HandlerSignal deliveredSignal not blockedSignal Caught by handlerReturn from Signal HandlerProcess ResumedSignalMaskSignalMaskSignalMaskCopyright ©: Nahrstedt, Angrave, Abdelzaher4Examples of POSIX Required SignalsSignal Description default action SIGABRT process abort implementation dependent SIGALRM alarm clock abnormal termination SIGBUS access undefined part of memory object implementation dependent SIGCHLD child terminated, stopped or continued ignore SIGILL invalid hardware instruction implementation dependent SIGINT interactive attention signal (usually ctrl-C) abnormal termination SIGKILL terminated (cannot be caught or ignored) abnormal terminationCopyright ©: Nahrstedt, Angrave, Abdelzaher5Signal Description default action SIGSEGV Invalid memory reference implementation dependent SIGSTOP Execution stopped stopSIGTERM termination Abnormal terminationSIGTSTP Terminal stop stop SIGTTIN Background process attempting read stop SIGTTOU Background process attempting writestopSIGURG High bandwidth data available on socketignore SIGUSR1 User-defined signal 1 abnormal terminationExamples of POSIX Required SignalsCopyright ©: Nahrstedt, Angrave, Abdelzaher6Generating SignalsSignal has a symbolic name starting with SIGSignal 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., SIGALRM)Copyright ©: Nahrstedt, Angrave, Abdelzaher7Command Line Generates Signals You can send a signal to a process from the command line using killkill -l will list the signals the system understandskill [-signal] pid will send a signal to a process. The optional argument may be a name or a number (default is SIGTERM).To unconditionally kill a process, use:kill -9 pid which is kill -SIGKILL pid.Copyright ©: Nahrstedt, Angrave, Abdelzaher8Command Line Generates SignalsCTRL-C is SIGINT (interactive attention signal CTRL-Z is SIGSTOP (execution stopped – cannot be ignored)CTRL-Y is SIGCONT (execution continued if stopped)CTRL-D is SIGQUIT (interactive termination: core dump)Copyright ©: Nahrstedt, Angrave, Abdelzaher9Timers Generate SIGALRM Signals#include <unistd.h>unsigned alarm (unsigned seconds);alarm(20) creates SIGALRM to calling process after 20 real time seconds.Calls are not stackedalarm(0) cancels alarmCopyright ©: Nahrstedt, Angrave, Abdelzaher10Examples: 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");Copyright ©: Nahrstedt, Angrave, Abdelzaher11Programming 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 ( ; ; ) ; }Copyright ©: Nahrstedt, Angrave, Abdelzaher12Signal MasksProcess 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 signalA 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.Copyright ©: Nahrstedt, Angrave, Abdelzaher13Signal SetsSignal set is of type sigset_tSignal 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, int signo);Copyright ©: Nahrstedt, Angrave, Abdelzaher14Signal 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 setCopyright ©: Nahrstedt, Angrave, Abdelzaher15Signal MasksSigInt SigQuit SigKill … SigCont SigAbrt0 0 1 … 1 0SigMaskSignal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20A SIGSET is a collection of signals: #000003 is SIGHUP + SIGINTCopyright ©: Nahrstedt, Angrave, Abdelzaher16SIGPROCMASKThe 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 setCopyright ©: Nahrstedt, Angrave, Abdelzaher17Example:
View Full Document