CMU Computer Systems: Exceptional Control Flow (Signals and Nonlocal Jumps)

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值