Chris Riesbeck, Fall 2011 Original: Fabian Bustamante
Exceptional Control Flow Part I
Today Exceptions Process context switches Creating and destroying processes Next time Signals, non-local jumps, …
Monday, November 21, 2011
Exceptional Control Flow Part I Today Exceptions Process context - - PowerPoint PPT Presentation
Exceptional Control Flow Part I Today Exceptions Process context switches Creating and destroying processes Next time Signals, non-local jumps, Chris Riesbeck, Fall 2011 Original: Fabian Bustamante Monday, November 21, 2011 Control flow
Chris Riesbeck, Fall 2011 Original: Fabian Bustamante
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
2
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
3
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
4
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
5
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 Printer Modem disk disk CDROM
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
6
current next
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
7
interrupt vector
1 2
n-1
code for exception handler 0 code for exception handler 1 code for exception handler 2 code for exception handler n-1
Exception numbers
Monday, November 21, 2011
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
9
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
10
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
11
int pop
0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . .
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
12
movl
int a[1000]; main () { a[500] = 13; } 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
13
int a[1000]; main () { a[5000] = 13; } 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360
movl
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
14
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
15
Time Process A Process B Process C
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
16
Time Process A Process B Process C
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
17
Time Process A Process B Process C
Monday, November 21, 2011
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
18
user code kernel code user code kernel code user code
context switch context switch
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
19
kernel virtual memory (code, data, heap, stack) memory mapped region for shared libraries run-time heap (managed by malloc) user stack (created at runtime) unused %esp (stack pointer) memory invisible to user code brk 0xc0000000 0x08048000 0x40000000 read/write segment (.data, .bss) read-only segment (.init, .text, .rodata) loaded from the executable file 0xffffffff
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
20
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
21
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); }
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
22
L0 L1 L1 Bye Bye Bye Bye
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
23
L1 L2 L2 Bye Bye Bye Bye L1 L2 L2 Bye Bye Bye Bye L0
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
24
L0 L1 Bye L2 Bye Bye Bye
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
25
L0 Bye L1 Bye Bye Bye L2
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
26
Monday, November 21, 2011
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
27
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
28 linux> ./forks 7 & [1] 6639 Running Parent, PID = 6639 Terminating Child, PID = 6640 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00 forks <defunct> 6641 ttyp9 00:00:00 ps linux> kill 6639 [1] Terminated linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6642 ttyp9 00:00:00 ps
void fork7() { if (fork() == 0) { /* Child */ printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } } Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
29 linux> ./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> kill 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6678 ttyp9 00:00:00 ps
void fork8() { if (fork() == 0) { /* Child */ printf("Running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } else { printf("Terminating Parent, PID = %d\n", getpid()); exit(0); } } Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
30
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
31
HP HC Bye CT Bye
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
32
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++) { /* whichever ends first */ 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); } }
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
33
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); }
Monday, November 21, 2011
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
35
– typically arg0 is either identical to path, or else it contains only the executable filename from path
main() { if (fork() == 0) { execl("/usr/bin/cp", "cp", "foo", "bar", 0); } wait(NULL); printf("copy completed\n"); exit(); }
Monday, November 21, 2011
EECS 213 Introduction to Computer Systems Northwestern University
36
Monday, November 21, 2011