[CS:APP-ch8] Exceptional Control Flow

CS:APP ch-8 Exceptional Control Flow

control flow

  • sequence of control transfers ( from the address of instruction to the next )

Exceptional Control Flow ( ECF )

  • Abrupt changes in control flow
  • Occurs all levels
    1. Hardware: Transfers to exceptional handlers
    2. Operating System: Context switch
    3. Application: Signal
    4. Individual Program: Nonlocal jump

8.1 Exceptions


  • Abrupt change in control flow in response to some change in the processor’s state


  • change in state

Exception Handling

  • Hardware
    1. processor detect an event
    2. determine corresponding exception number k
    3. trigger exception by calling the handler through entry k of the exception table
  • Software ( handler )
    1. do some processing
    2. return control to the interrupted control flow
  • Difference with procedure call
    1. return either current or next instruction
    2. push additional processor state to restart ( on kernel stack )
    3. handlers run in kernel mode

Classes of Exceptions

  • Interrupt: I/O devices
  • Trap: System call ( fork, read, execve, exit )
  • Fault: error conditions that might be able to correct or just abort ( page fault, segmentation fault, divide zero )
  • Abort: unrecoverable ( machine check )

8.2 Processes

DEF: An instance of program in execution

program run in context of process

Logical Control Flow

  • provide an illusion that program has exclusive use of the processor
  • The sequence of PC values is know as logical control flow
  • Each process executes a portion of its flow and then is preempted (temporarily suspended) while other processes take their turns.

Concurrent Flow

  • A logic flow whose execution overlaps in time with another flow


  • The general phenomenon of multiple flows executing concurrently

Parallel Flow

  • subset of concurrent flow that run on different cores

Private Address Space

  • provide an illusion that program has exclusive use of the system address’s space

User and kernel mode: mode bit


  • the state that kernel needs to restart the preempted process

  • ( general-purpose registers, floating point registers, program counter, user’s stack, status register, kernel’s stack, page table, process table, file table )


  • the kernel decide to preempt the current process and restart a previously preempted process

Context Switch

  1. Saves context of the current process
  2. Restores the saved context of previous process
  3. Passes control to newly restored process

Example when execute read system call ( disk )

Kernel switch process until disk sends an interrupt signal

8.4 Process Control

Obtaining Process ID

pid_t getpid ( void );

Create and Terminating process

Three states

  1. Running
  2. Stopped
  3. Terminated
void exit ( int status );
//return 0 to child, PID of chile to parent
pid_t fork ( void );
  • Call Once Return Twice

  • Concurrent execution

  • Duplicate but separate address space

  • Shared files

Reaping Child Process

The process kept around in a terminated state until it is reaped by its parent

父进程进行回收的函数,也是一个等待子进程执行结束的函数就是waitpid。这在 APUE(advanced programming in unix enviroment)中很早就提过这个函数

// return PID of terminated child and reap it
pid_t waitpid(pid_t pid, int *statud, int options)

Putting Process to Sleep

unsigned int sleep(unsigned int secs);
int pause(void); // 一直休眠,直到收到一个信号

Loading and Running Programs

int execve(const char *filename, const char *argv[], const char *envp[]);

8.5 Signals

The transfer of a signal to a destination process occurs in two distinct steps

Sending a signal

  • The kernel has detected a system event such as a divide-by-zero error or the termination of a child process.
  • A process has invoked the kill function to explicitly request the kernel to send a signal to the destination process. A process can send a signal to itself.

Receiving a signal

  • The process can either ignore the signal, terminate, or catch the signal by executing a user-level function called a signal handler.

Pending signal

  • A signal that has been sent but not yet received
  • At any point in time, there can be at most one pending signal of a particular type.
  • Received at most once: The kernel sets bit k in pending whenever a signal of type k is delivered and clears bit k in pending whenever a signal of type k is received.

8.6 Nonlocal Jumps