Presentation is loading. Please wait.

Presentation is loading. Please wait.

Signals.

Similar presentations


Presentation on theme: "Signals."— Presentation transcript:

1 Signals

2 Introduction to Signals
What 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 events Examples of asynchronous events: message arrives on my machine – mailing agent (user) process should retrieve Invalid memory access happens – OS should inform scheduler to remove process from the processor Alarm clock goes on – process which sets the alarm should catch it

3 Basic Signal Concept (Definitions)
Signal is generated when the event that causes the signal occurs. Signal is delivered when the process takes action based on the signal. The lifetime of a signal is the interval between its generation and delivery. Signal that has been generated but not yet delivered is pending. Process catches signal if it executes 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 a set of signals currently blocked.

4 How Signals Work Process Signal Generated Signal Mask Signal delivered
Signal Handler Signal delivered Signal not blocked Signal Caught by handler Return from Signal Handler Process Resumed Signal Mask

5 Examples of POSIX Required Signals
Description default action SIGABRT process abort (can be caught but not ignored) implementation dependent SIGALRM alarm clock abnormal termination SIGBUS Invalid address alignment Non-existent physical address SIGCHLD child terminated, stopped or continued ignore SIGILL invalid hardware instruction SIGINT interactive attention signal (usually ctrl-C) SIGKILL terminated (cannot be caught or ignored)

6 Signal Description default action SIGSEGV Invalid memory reference implementation dependent SIGSTOP Execution stopped stop SIGTERM Termination: it can be caught and interpreted (or ignored) Abnormal termination SIGTSTP Terminal stop SIGTTIN Background process attempting read SIGTTOU Background process attempting write SIGURG High bandwidth data available on socket ignore SIGUSR1 User-defined signal 1 abnormal termination

7 Generating Signals Signal has 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)

8 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. The default is SIGTERM. To unconditionally kill a process, use: kill -9 pid which is kill -SIGKILL pid.

9 Command Line Generates Signals
stty –a CTRL-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)

10 Timers 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 stacked alarm(0) cancels alarm

11 Examples: Programming Signals
From 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");

12 Programming Signals if (raise(SIGUSR1) != 0)
Example 8.5: a process sends a signal to itself: if (raise(SIGUSR1) != 0) perror("Failed to raise SIGUSR1"); //It is same as kill(getpid(), SIGUSR1); Example 8.8: kill an infinite loop after 10 seconds: int main(void) { alarm(10); for ( ; ; ) ; }

13 Signal Masks Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains a set of signals currently blocked. 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.

14 Signal Sets #include <signal.h> int sigemptyset(sigset_t *set);
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, int signo);

15 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

16 Signal Masks SigMask Signal SigInt Bit 2, Signal Sigkill Bit 9,
SigQuit SigKill SigCont SigAbrt 1 1 Signal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20 A SIGSET is a collection of signals: # is SIGHUP + SIGINT

17 SIGPROCMASK 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) If oset is non-null, the previous value of the signal mask is stored in oset. ‘how’ specifies the manner in which the signal mask is to be modified SIG_BLOCK The set of blocked signals is the union of the current set and the set argument. SIG_UNBLOCKED The signals in set are removed from the current set of blocked signals. SIG_SETMASK The set of blocked signals is set to the argument set.

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

19 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.


Download ppt "Signals."

Similar presentations


Ads by Google