Lecture 016

Exceptional Control Flow

Low level mechanisms

Exception: Change in control flow in response to a system event (change system state)

Asynchronous Exception Control Flow

Asynchronous Exception Control Flow: external events

Synchronous Exception Control Flow

Synchronous Exception Control Flow: Caused by events that occur as a result of executing an instruction

Syscall

syscall: calling kernel with different set of privileges

Number Name Description
0 read read file
1 write write file
2 open open file
3 closc close file
4 stat get info about file
12 brk allocate memory
57 fork create process
59 execve execute program
60 exit terminate process
62 kill send signal to process
Page Fault

Recoverable Page Fault

Recoverable Page Fault

Unrecoverable Page Fault

Unrecoverable Page Fault

Higher level mechanisms

Process Context Switch (interrupt)

Signals

Nonlocal Jumps

Processes

process: A process is an instance of a running program. abstraction

Context Switching

Context Switching Single Core

Context Switching Single Core

Context Switching (about 2000 cycles)

Context Switching Multi Core

Context Switching Multi Core
Multi-core: multiple CPUs on single chip

Concurrency

Kernel Code doing Context Switch

Kernel Code doing Context Switch

concurrent: if any portion of the code from process B runs during the runtime of process A, process A is in concurrent with process B. sequential: not concurrent

Process Control

On Error, Linux system-level functions typically return -1 and set global variable errno to indicate cause. (not termination, therefore you should check return value of syscall)

Simplifying Error Handling

Simplifying Error Handling

pid_t getpid(void): Returns PID of current process pid_t getppid(void): Returns PID of parent process

Processes' Status: status of a process in programmer's perspective

exit()

Terminating Process

fork()

Creating Process: parent create running child process by calling int fork(void)

Concurrency

Concurrency in Creating Process:

Feasible Ordering Example 1

Feasible Ordering Example 1

Feasible Ordering Example 2

Feasible Ordering Example 2

Feasible Ordering Example 3

Feasible Ordering Example 3

Feasible Ordering Example 4

Feasible Ordering Example 4

Reaping Child Process with wait()

wait(...): wait for the first finished child process before running following code

waitpid(...): wait for a specific child process before running following code

Zombie Process: finished child process with only exit status, various OS tables waiting for the parent to check. Zombie Process only killed after parent check their children's status by calling wait.

Orphan Process: a process that is still executing, but whose parent has died. When the parent dies, the orphaned child process is adopted by init. When orphan processes die, they do not remain as zombie processes; instead, they are waited on by init (PID: 0, parent of all process)

Wait Example

Wait Example

Loading and Running Programs with execve()

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

Arguments in execve

Arguments in execve

Arguments in stack

Arguments in stack

execve command operating virtual memory

execve command operating virtual memory

More variation of execve: Youtube

Shell

Linux Process Hierarchy

Linux Process Hierarchy

Shell

Process Group: one process belongs to exactly one process group

Process Group

Process Group

kill: send a signal to a process or process group

Signals

signal: notify a process and start run signal handler (can be sync or async)

ID Name Default Event
2 SIGINT Terminate Ctrl-C
9 SIGKILL Terminate Kill Program (not overridable)
11 SIGSEGV Terminate Segmentation (page) violation
14 SIGALRM Terminate Timer
17 SIGCHLD Terminate Child stopped or terminated

Kernel and Signal

Send: kernel store signal in memory

Delivers: kernel read stored signal and notify program by executing handler

Pending: a signal is sent but not delivered

Block: a process can block the receipt of signals

Receive: when a process react to the delivery of the signal

Signal handler

Async-Safety

Installing Signal Handlers

int sigaction(int signum, const struct sigaction *sa, struct sigaction *old_sa)

Blocking Signals

sigprocmask: manipulate signal mask in process control block of kernel sigemptyset: create empty set that blocks no signals sigfillset: create filled set that blocks all signals sigaddset: add signal to blocking set sigdelset: delete signal from blocking set (unblock)

Synchronous Signal

pause(): stop(suspend) process until any signal arrives waitpid(): wait for child process with pid to terminate / stop / continue wait(): wait for any children to terminate

Asynchronous Signal

using handlers

Table of Content