Docsity

Exceptional Control Flow: Exceptions and Processes in Computer Systems, Slides of Computer System Design and Architecture

Partial preview of the text

Download Exceptional Control Flow: Exceptions and Processes in Computer Systems and more Slides Computer System Design and Architecture in PDF only on Docsity!

Excep&onal Control Flow:

Excep&ons and Processes

15-­‐213: Introduc0on to Computer Systems

12 th^ Lecture, Oct. 5, 2010

Instructors:

Randy Bryant and Dave O’Hallaron

Today

 Excep&onal Control Flow

 Processes

Altering the Control Flow

 Up to now: two mechanisms for changing control flow:

 Jumps and branches

 Call and return

Both react to changes in program state

 Insufficient for a useful system:

Difficult to react to changes in system state

 data arrives from a disk or a network adapter

 instruc0on divides by zero

 user hits Ctrl-­‐C at the keyboard

 System 0mer expires

 System needs mechanisms for “excep&onal control flow”

Excep&onal Control Flow

 Exists at all levels of a computer system

 Low level mechanisms

 Excep0ons

 change in control flow in response to a system event

(i.e., change in system state)

 Combina0on of hardware and OS soXware

 Higher level mechanisms

 Process context switch

 Signals

 Nonlocal jumps: setjmp()/longjmp()

 Implemented by either:

 OS soXware (context switch and signals)

 C language run0me library (nonlocal jumps)

n-

Interrupt Vectors

Each type of event has a unique excep&on number kk = index into excep&on table (a.k.a. interrupt vector)Handler k is called each &me excep&on k occurs

Excep&on

Table

code for

excep&on handler 0

code for

excep&on handler 1

code for

excep&on handler 2

code for

excep&on handler n-­‐

Excep&on

numbers

Asynchronous Excep&ons (Interrupts)

 Caused by events external to the processor

 Indicated by se]ng the processor’s interrupt pin

 Handler returns to “next” instruc0on

 Examples:

 I/O interrupts

 hi]ng Ctrl-­‐C at the keyboard

 arrival of a packet from a network

 arrival of data from a disk

 Hard reset interrupt

 hi]ng the reset bu`on

 SoX reset interrupt

 hi]ng Ctrl-­‐Alt-­‐Delete on a PC

Trap Example: Opening File

 User calls: open(filename, options)

 Func0on open executes system call instruc0on int

 OS must find or create file, get it ready for reading or wri0ng

 Returns integer file descriptor

0804d070 <__libc_open>:

804d082: cd 80 int $0x

804d084: 5b pop %ebx

User Process OS

excep.on open file returns int pop

Fault Example: Page Fault

 User writes to memory loca0on

 That por0on (page) of user’s memory

is currently on disk

 Page handler must load page into physical memory

 Returns to faul0ng instruc0on

 Successful on second try

int a[1000];

main ()

a[500] = 13;

80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d

User Process OS

excep.on: page fault Create page and returns^ load into memory movl

Excep&on Table IA32 (Excerpt)

Excep5on Number Descrip5on Excep5on Class 0 Divide error Fault 13 General protec0on fault Fault 14 Page fault Fault 18 Machine check Abort 32-­‐127 OS-­‐defined Interrupt or trap 128 (0x80) System call Trap 129-­‐255 OS-­‐defined Interrupt or trap Check Table 6-­‐1: h^p://download.intel.com/design/processor/manuals/253665.pdf

Today

 Excep&onal Control Flow

 Processes

Concurrent Processes

 Two processes run concurrently ( are concurrent) if their

flows overlap in &me

 Otherwise, they are sequen5al

 Examples (running on single core):

 Concurrent: A & B, A & C

 Sequen0al: B & C

Process A Process B Process C

Time

User View of Concurrent Processes

 Control flows for concurrent processes are physically

disjoint in &me

 However, we can think of concurrent processes are

running in parallel with each other

Time

Process A Process B Process C

fork : Crea&ng New Processes

 int fork(void)

 creates a new process (child process) that is iden0cal to the calling

process (parent process)

 returns 0 to the child process

 returns child’s pid to the parent process

 Fork is interes&ng (and oeen confusing) because

it is called once but returns twice

pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); }

Understanding fork

pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } Process n pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } Child Process m pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } pid = m pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } pid = 0 pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } hello from parent (^) Which one is first? hello from child