CMU Computer Systems: Exceptional Control Flow (Exceptions and Processes)

Control Flow
  • Processors do only one thing
    • Simply read and execute a sequence of instructions, one at a time
    • The sequence is the CPU’s control flow
Altering the Control Flow
  • Mechanism
    • Jumps and branches
    • Call and return
  • Difficulty
    • Data arrives from a disk or a network adapter
    • Instruction divides by zero
    • User hits Ctrl-C at the keyboard
    • System timer expires
Exceptional Control Flow
  • Low level mechanisms
    • Exceptions
      • Change in control flow in response to a system event
      • Implemented using combination of hardware and OS software
  • Higher level mechanisms
    • Process context switch
      • Implemented by OS software and hardware timer
    • Signals
      • Implemented by OS software
    • Nonlocal jumps
      • Implemented by C runtime library
Exceptions
  • An exception is a transfer of control to the OS kernel in response to some event (i.e., change in processor state)
    • Kernel is the memory-resident part of the OS
    • Examples of events: Divide by 0, arithmetic overflow, page fault, I/O, request completes, typing Ctrl-C
Exception Tables
  • Each type of event has a unique exception number k
  • k = index into exception table
  • Handler k is called each time exception k occurs
Asynchronous Exceptions (Interrupts)
  • Caused by events external to the processor
    • Indicated by setting the processor’s interrupt pin
    • Handler returns to “next” instruction
  • Caused by events that occur as a result of executing an instruction
    • Traps
      • Intentional
      • Returns control to “next” instruction
    • Faults
      • Unintentional but possibly recoverable
      • Either re-executes faulting (“current”) instruction or aborts
    • Aborts
      • Unintentional and unrecoverable
      • Aborts current program
Processes
  • A process is an instance of a running program
    • One of the most profound ideas in computer science
    • Not the same as “program” or “processor”
  • Process provides each program with two key abstractions
    • Logical control flow
      • Each program seems to have exclusive use of the CPU
      • Provided by kernel mechanism called context switching
    • Private address space
      • Each program seems to have exclusive use of main memory
      • Provided by kernel mechanism called virtual memory
Multiprocessing: The Traditional Reality
  • Single processor executes multiple processes concurrently
  • Save current registers in memory
  • Load saved registers and switch address space (context)
Concurrent Processes
  • Each process is a logical control flow
  • Two processes run concurrently if their flows overlap in time
  • Otherwise, they are sequential
Context Switching
  • Processes are managed by a shared chunk of memory-resident OS code called the kernel
    • Important: the kernel is not a separate process, but rather runs as part of some existing process
  • Control flow passes from one process to another via a context switch
Creating and Terminating Processes
  • Running
    • Process is either executing, or waiting to be executed and will eventually be scheduled by the kernel
  • Stopped
    • Process execution is suspended and will not be scheduled until further notice
  • Terminated
    • Process is stopped permanently
Terminating Processes
  • Process becomes terminated for one of three reasons
    • Receiving a signal whose default actions is to terminate
    • Returning from the main routine
    • Calling the exit function
Reaping Child Processes
  • Idea
    • When process terminates, it still consumes system resources
    • Called a “zombie”
      • Living corpse, half alive and half dead
  • Reaping
    • Performed by parent on terminated child
    • Parent is given exit status information
    • Kernel then deletes zombie child process
  • What if parent doesn’t reap
    • If any parent terminates without reaping a child, then the orphaned child will be reaped by init process
    • So, only need explicit reaping in long-running processes
Summary
  • Exceptions
    • Events that require nonstandard control flow
    • Generated externally (interrupts) or internally (traps and faults)
  • Processes
    • At any given time, system has multiple active processes
    • Only one can execute at a time on a single core, though
    • Each process appears to have total control of processor + private memory space
  • Spawning processes
    • Call fork
    • One call, two returns
  • Process completion
    • Call exit
    • One call, no return
  • Reaping and waiting for processes
    • Call wait or waitpid
  • Loading and running programs
    • Call execve (or variant)
    • One call, (normally) no return
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值