Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.

Similar presentations


Presentation on theme: "Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals."— Presentation transcript:

1 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals

2 Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Posix Signals Signals are an integral part of multitasking in the UNIX/POSIX environment. Signals are used for many purposes, including: 1. Exception handling (bad pointer accesses, divide by zero, etc.) 2. Process notification of asynchronous event occurrence (I/O completion, timer expiration, etc.) 3. Process termination in abnormal circumstances 4. Interprocess communication Signals are similar to the notion of hardware interrupts. However, they are managed and delivered by the Operating System.

3 Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Dealing with signals There are different ways in which you can deal with a signal: 1. You can block a signal for a while, and get to it (by unblocking it) later. Blocking signals is a temporary measure. 2. You can ignore the signal, in which case it is as if the signal never arrived. 3. You can handle the signal by executing a default action to deal with the signal (the default action often is to kill the process receiving the signal) 4. You can handle the signal by setting up a function to be called whenever a signal with a particular number arrives. There are two spare signals available to user applications: SIGUSR1 and SIGUSR2. Any application can use them as it wants.

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Basic Signal Concepts Signal is generated when the event that causes it occurs. Signal is delivered when a process receives it. The lifetime of a signal is the interval between its generation and delivery. Signal that is generated but not delivered is pending. Process catches signal if it executes a signal handler when the signal is delivered. Alternatively, a process can ignore a signal when it is delivered, that is to take no action. Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains the set of signals currently blocked.

5 Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Steps of Signal’s Delivery and Handling Event of sending a signal: 1. The OS updates the descriptor of the destination process to represent that a new signal has been sent. 2. Signals that have been sent but not yet received are called pending signals. At any time, only one pending signal of a given type may exist for a process; additional pending signals of the same type to the same process are not queued but simply discarded. Signal sent by the OS or by a process Process mask Signal received by the current executing process when switching from Kernel Mode to User Mode signal pending ignore signal default action user’s handler signal received

6 Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Steps of Signal’s Delivery and Handling Event of receiving a signal: 1. If the sent signal is blocked by the process mask, the process will not receive the signal until it removes the block: the signal remains pending. 2. When switching from Kernel Mode to User Mode, check whether a signal has been sent to the current executing process (this happens at every timer interrupt). Unblocked signals are received. 3. Determine whether ignore the signal, execute a default action or execute user’s signal handler. Signal sent by the OS or by a process Process mask Signal received by the current executing process when switching from Kernel Mode to User Mode signal pending ignore signal default action user’s handler signal received

7 Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 How Signals Work Signal Generated Process Signal Handler Signal delivered Signal not blocked Signal Caught by handler Return from Signal Handler Process Resumed Process Signal Mask Handler signal Mask Process Signal Mask A signal handler interacts with the regular execution flow of the corresponding process by simply sharing global variables: the regular execution flow and signal handler execute in the same memory space.

8 Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Examples of POSIX Required Signals SignalDescriptiondefault action SIGABRTprocess abortimplementation dependent SIGALRMalarm clockabnormal termination SIGBUSaccess undefined part of memory objectimplementation dependent SIGCHLDchild terminated, stopped or continuedignore SIGILLinvalid hardware instructionimplementation dependent SIGINTinteractive attention signal (usually ctrl-C) abnormal termination SIGKILLterminated (cannot be caught or ignored) abnormal termination

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 SignalDescriptiondefault action SIGSEGVInvalid memory referenceimplementation dependent SIGSTOPExecution stoppedstop SIGTERMterminationAbnormal termination SIGTTINBackground process attempting readstop SIGTTOUBackground process attempting writestop SIGURGHigh bandwidth data available on socket ignore SIGUSR1User-defined signal 1abnormal termination Examples of POSIX Required Signals

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Signal 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?

11 Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Signal 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 When a process ignores signal, signal is delivered and the process handles it by throwing it away.

12 Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Signal Sets Signal set is of type sigset_t Signal sets are manipulated by five functions: #include 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);

13 Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Signal Sets sigemptyset initializes the set to contain no signals sigfillset puts all signals in the set sigaddset adds one signal to the set sigdelset removes one signal from the set sigismember tests to see if a signal is in the set

14 Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Example: Initialize Signal Set: if ((sigemptyset(&twosigs) == -1) || (sigaddset(&twosigs, SIGINT) == -1) || (sigaddset(&twosigs, SIGQUIT) == -1)) perror("Failed to set up signal mask");

15 Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Setting the Signal Mask of a process The collection of signals that are currently blocked is called the signal mask. Each process has its own signal mask. When you create a new process, it inherits its parent's mask. You can block or unblock signals with total flexibility by modifying the signal mask

16 Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Function: int sigprocmask (int how, const sigset_t *set, sigset_t *oldset) The sigprocmask function is used to examine or change the calling process's signal mask. The how argument determines how the signal mask is changed, and must be one of the following values: SIG_BLOCK Block the signals in set---add them to the existing mask. In other words, the new mask is the union of the existing mask and set. SIG_UNBLOCK Unblock the signals in set---remove them from the existing mask. SIG_SETMASK Use set for the mask; ignore the previous value of the mask. The last argument, oldset, is used to return information about the old process signal mask Setting the Signal Mask of a process

17 Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Example: 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 to sigprocmask has no effect.

18 Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Generating Signals Signals have a symbolic name starting with SIG Signal names are defined in signal.h Users 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)

19 Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Command Line Generates Signals You can send a signal to a process from the command line using kill kill -l will list the signals the system understands kill [-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.

20 Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Examples: Programming Signals From a program you can use the kill system call: #include 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");

21 Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Command Line Generates Signals CTRL-C is SIGINT (interactive attention signal CTRL-Z is SIGSTOP (execution stopped – cannot be ignored) CTRL-Y is SIGCONT (execution continued if stopped)

22 Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Timers Generate SIGALRM Signals #include unsigned alarm (unsigned seconds); alarm(20) creates SIGALRM to calling process after 20 real time seconds. Alarm requests are not stacked; only one SIGALRM generation can be scheduled in this manner. If the SIGALRM signal has not yet been generated, calling alarm() again will reschedule the time at which the SIGALRM signal is generated. alarm(0) cancels alarm


Download ppt "Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals."

Similar presentations


Ads by Google