Download Exceptional Control Flow - Lecture Slides | CSCE 230 and more Study notes Computer Architecture and Organization in PDF only on Docsity!
Exceptional Control Flow
Part I
CSCE 230J
Computer Organization
Dr. Steve Goddard
http://cse.unl.edu/~goddard/Courses/CSCE230J
2
Giving credit where credit is due
Most of slides for this lecture are based on
slides created by Drs. Bryant and
O’Hallaron, Carnegie Mellon University.
I have modified them and added new
slides.
3
Topics
Exceptions
Process context switches
Creating and destroying processes
4
Control Flow
inst 1
inst 2
inst 3
instn
Computers do Only One Thing
From startup to shutdown, a CPU simply reads and executes
(interprets) a sequence of instructions, one at a time.
This sequence is the system’s physical control flow (or flow
of control ).
Physical control flow
Time
7
System context for exceptions
Local/IO Bus
Memory Network adapter
IDE disk controller
Video adapter
Display Network
Processor Interrupt controller
SCSI
controller
SCSI bus
Serial port controller
Parallel port controller
Keyboard controller
Keyboard Mouse Modem Printer
disk disk CDROM
8
Exceptions
An exception is a transfer of control to the OS in response
to some event (i.e., change in processor state)
User Process OS
exception
exception processing
by exception handler
exception
return (optional)
event current
next
9
Interrupt Vectors
Each type of event has a
unique exception number k
Index into jump table (a.k.a.,
interrupt vector)
Jump table entry k points to a
function (exception handler).
Handler k is called each time
exception k occurs.
interrupt vector 0 1 2 ... n-
code for exception handler 0
code for exception handler 1
code for exception handler 2
code for exception handler n-
...
Exception numbers
10
Asynchronous Exceptions (Interrupts)
Caused by events external to the processor
Indicated by setting the processor’s interrupt pin
handler returns to “next” instruction.
Examples:
I/O interrupts
hitting ctl-c at the keyboard
arrival of a packet from a network
arrival of a data sector from a disk
Hard reset interrupt
hitting the reset button
Soft reset interrupt
hitting ctl-alt-delete on a PC
13
Fault Example #
User Process OS
page fault
Create page and load
return into memory
event movl
Memory Reference
User writes to memory location
That portion (page) of user’s memory
is currently on disk
Page handler must load page into
physical memory
Returns to faulting instruction
Successful on second try
int a[1000]; main () { a[500] = 13; }
80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d
14
Fault Example #
User Process OS
page fault
Detect invalid address
event movl
Memory Reference
User writes to memory location
Address is not valid
Page handler detects invalid address
Sends SIGSEG signal to user process
User process exits with “segmentation fault”
int a[1000]; main () { a[5000] = 13; }
80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e
Signal process
15
Processes
Def: 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.
Private address space
Each program seems to have exclusive use of main memory.
How are these Illusions maintained?
Process executions interleaved (multitasking)
Address spaces managed by virtual memory system
16
Logical Control Flows
Time
Process A Process B Process C
Each process has its own logical control flow
19
Context Switching
Processes are managed by a shared chunk of OS code
called the kernel
Important: the kernel is not a separate process, but rather
runs as part of some user process
Control flow passes from one process to another via a
context switch.
Process A
code
Process B
code
user code
kernel code
user code
kernel code
user code
Time
context switch
context switch
20
Private Address Spaces
Each process has its own private address space.
kernel virtual memory (code, data, heap, stack)
memory mapped region for shared libraries
run-time heap (managed by malloc)
user stack (created at runtime)
0 unused
%esp (stack pointer)
memory invisible to user code
brk
0xc
0x
0x
read/write segment (.data, .bss) read-only segment (.init, .text, .rodata)
loaded from the executable file
0xffffffff
21
fork : Creating new processes
int fork(void)
creates a new process (child process) that is identical to the
calling process (parent process)
returns 0 to the child process
returns child’s pid to the parent process
if (fork() == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
Fork is interesting
(and often confusing)
because it is called
once but returns twice
22
Fork Example #
void fork1()
int x = 1;
pid_t pid = fork();
if (pid == 0) {
printf("Child has x = %d\n", ++x);
} else {
printf("Parent has x = %d\n", --x);
printf("Bye from process %d with x = %d\n", getpid(), x);
Key Points
Parent and child both run same code
Distinguish parent from child by return value from fork
Start with same state, but each has private copy
Including shared output file descriptor
Relative ordering of their print statements undefined
25
Fork Example #
void fork4()
printf("L0\n");
if (fork() != 0) {
printf("L1\n");
if (fork() != 0) {
printf("L2\n");
fork();
printf("Bye\n");
Key Points
Both parent and child can continue forking
L0 L
Bye
L
Bye
Bye
Bye
26
Fork Example #
void fork5()
printf("L0\n");
if (fork() == 0) {
printf("L1\n");
if (fork() == 0) {
printf("L2\n");
fork();
printf("Bye\n");
Key Points
Both parent and child can continue forking
L0 Bye
L
Bye
Bye
Bye
L
27
exit : Destroying Process
void exit(int status)
exits a process
Normally return with status 0
atexit() registers functions to be executed upon exit
void cleanup(void) {
printf("cleaning up\n");
void fork6() {
atexit(cleanup);
fork();
exit(0);
28
Zombies
Idea
When process terminates, still consumes system resources
Various tables maintained by OS
Called a “zombie”
Living corpse, half alive and half dead
Reaping
Performed by parent on terminated child
Parent is given exit status information
Kernel discards process
What if Parent Doesn’t Reap?
If any parent terminates without reaping a child, then child
will be reaped by init process
Only need explicit reaping for long-running processes
E.g., shells and servers
31
wait : Synchronizing with children
int wait(int *child_status)
suspends current process until one of its children
terminates
return value is the pid of the child process that terminated
if child_status != NULL , then the object it points to will
be set to a status indicating why the child process
terminated
32
wait : Synchronizing with children
void fork9() {
int child_status;
if (fork() == 0) {
printf("HC: hello from child\n");
else {
printf("HP: hello from parent\n");
wait(&child_status);
printf("CT: child has terminated\n");
printf("Bye\n");
exit();
HP
HC Bye
CT Bye
33
Wait Example
If multiple children completed, will take in arbitrary order
Can use macros WIFEXITED and WEXITSTATUS to get
information about exit status
void fork10()
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = wait (&child_status);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminate abnormally\n", wpid);
34
Waitpid
waitpid(pid, &status, options)
Can wait for specific process
Various options
void fork11()
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = waitpid (pid[i], &child_status, 0);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminated abnormally\n", wpid);
37
Summarizing
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, though
Each process appears to have total control of processor + private
memory space
38
Summarizing (cont.)
Spawning Processes
Call to fork
One call, two returns
Terminating Processes
Call exit
One call, no return
Reaping Processes
Call wait or waitpid
Replacing Program Executed by Process
Call execl (or variant)
One call, (normally) no return