ECF Exists at All Levels of a System
-
Exceptions
- Hardware and operating system kernel software
-
Process Context Switch
- Hardware timer and kernel software
-
Signals
- Kernel software and application software
-
Nonlocal jumps
- Application code
ECF to the Rescue
-
Solution: Exceptional control flow
- The kernel will interrupt regular processing to alert us when a background process completes
- In Unix, the alert mechanism is called a signed
Signals
-
A signal is a small message that notifies a process that an event of some type has occurred in the system
- Akin to exceptions and interrupts
- Sent from the kernel to a process
- Signal type is identified by small integer ID’s
- Only information in a signal is its ID and the fact that it arrived
Signal Concepts: Sending a Signal
-
Kernel sends a signal to a destination process by updating some state in the context of the destination process
-
Reasons
- Kernel has detected a system event such as divide-by-zero or the termination of a child process
- Another process has invoked the kill system call to explicitly request the kernel to send a signal to the destination process
Signal Concepts: Receiving a Signal
-
A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal
-
Some possible ways to react
- Ignore the signal
- Terminate the process
- Catch the signal by executing a user-level function call signal handler
Signal Concepts: Pending and Blocked Signals
-
A signal is pending if sent but not yet received
-
There can be at most one pending signal of any particular type
-
Signals are not queued
- If a process has a pending signal of type k, then subsequent signals of type k that are sent to that process are discarded
-
-
A process can block the receipt of certain signals
- Blocked signals can be delivered, but will not be received until the signal is unblocked
-
A pending signal is received at most once
Signal Concepts: Pending/ Blocked Bits
-
Kernel maintains pending and blocked bit vectors in the context of each process
-
pending: represents the set of pending signals
- Kernel sets bit k in pending when a signal of type k is delivered
- Kernel clears bit k in pending when a signal of type k is received
-
blocked: represents the set of blocked signals
- Can be set and cleared by using the sigprocmask function
- Also referred to as the signal mask
-
Receiving Signals
-
Suppose kernel is returning from an exception handler and is ready to pass control to process p
-
Kernel computes pnb = pending & ~blocked
- The set of pending nonblocked signals for process p
-
if (pnb == 0)
- Pass control to next instruction in the logical flow for p
-
Else
- Choose least nonzero bit k in pnb and force process p to receive signal k
- The receipt of the signal triggers some action by p
- Repeat for all nonzero k in pnb
- Pass control to next instruction in logical flow for p
Nested Signal Handlers
- Handlers can be interrupted by other handlers
Blocking and Unblocking Signals
-
Implicit blocking mechanism
- Kernel blocks any pending signals of type currently being handled
-
Explicit blocking and unblocking mechanism
- sigprocmask function
-
Supporting functions
- sigemptyset - create empty set
- sigfillset - add every signal number to set
- sigaddset - add signal number to set
- sigdelset - delete signal number from set
Guidelines for Writing Safe Handlers
- Keep your handlers as simple as possible
- Call only async-signal-safe functions in your handlers
- Save and restore errno on entry and exit
- Protect accesses to shared data structures by temporarily blocking all signals
- Declare global variables as volatile
- Declare global flags as volatile sig_atomic_t