exceptional control flow part i
play

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


  1. 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

  2. Control flow 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 <startup> inst 1 inst 2 Time inst 3 … inst n <shutdown> 2 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  3. Altering the control flow Up to now: two mechanisms for changing control flow – Jumps and branches – Call and return using the stack discipline. – Both react to changes in program state. Insufficient for a useful system – Difficult for the CPU to react to changes in system state. • Data arrives from a disk or a network adapter. • Instruction divides by zero • User hits ctl-c at the keyboard • System timer expires System needs mechanisms for “exceptional control flow” 3 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  4. Exceptional control flow Mechanisms for exceptional control flow exists at all levels of a computer system Low level mechanism – Exceptions • change in control flow in response to a system event (i.e., change in system state) – Combination of hardware and OS software Higher level mechanisms – Process context switch – Signals – Nonlocal jumps (setjmp/longjmp) – Implemented by either: • OS software (context switch and signals). • C language runtime library: nonlocal jumps. 4 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  5. System context for exceptions Keyboard Mouse Modem Printer Interrupt Keyboard Serial port Parallel port Processor controller controller controller controller Local/IO Bus Video Network IDE disk SCSI Memory adapter adapter controller controller SCSI bus Network disk Display disk CDROM 5 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  6. Exceptions Exception – a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS exception event current exception processing next by exception handler exception return (optional) 6 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  7. Interrupt vectors Each type of event has a unique Exception numbers exception number k Index into jump table code for exception handler 0 (a.k.a., interrupt interrupt vector) code for vector exception handler 1 Jump table entry k 0 1 code for points to a function 2 exception handler 2 ... (exception handler). ... n-1 Handler k is called each time exception k code for exception handler n-1 occurs. 7 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  8. Exceptions Exception numbers created by – processor designers – OS kernel designers Exception handling – like procedure call – return address pushed on stack – might be current instruction or next, depending on type of exception – additional processor state pushed, e.g., condition flags – data be pushed on either user stack or kernel stack – handler run in kernel mode Monday, November 21, 2011

  9. 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 9 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  10. Synchronous exceptions Caused by events that occur as a result of executing an instruction: – Traps • Intentional • Examples: system calls, breakpoint traps, special instructions • Like procedure call but in kernel mode • Returns control to “next” instruction – Faults • Unintentional but possibly recoverable • Examples: page faults (recoverable), protection faults (unrecoverable). • Either re-executes faulting (“current”) instruction or aborts. – Aborts • unintentional and unrecoverable • Examples: parity error, machine check. • Aborts current program 10 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  11. Trap example Opening a File – User calls open(filename, options) 0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . . • Function open executes system call instruction int – OS must find or create file, get it ready for reading or writing – Returns integer file descriptor User Process OS exception int pop Open file return 11 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  12. Fault example #1 Memory reference int a[1000]; main () { – User writes to memory location a[500] = 13; } – That portion (page) of user’s memory is currently on disk 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 – Page handler must load page into physical memory – Returns to faulting instruction – Successful on second try User Process OS page fault event movl Create page and load into memory return 12 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  13. Fault example #2 Memory reference int a[1000]; main () – User writes to memory location { a[5000] = 13; – Address is not valid } 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 – Page handler detects invalid address – Sends SIGSEG signal to user process – User process exits with “segmentation fault” User Process OS page fault event movl Detect invalid address Signal process 13 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  14. 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 14 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  15. Logical control flows Each process has its own logical control flow Process A Process B Process C Time 15 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  16. Concurrent processes Two processes run concurrently ( are concurrent) if their flows overlap in time. Otherwise, they are sequential. Examples: – Concurrent: A & B, A & C – Sequential: B & C Process A Process B Process C Time 16 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  17. User view of concurrent processes Control flows for concurrent processes are physically disjoint in time. However, we can think of concurrent processes are running in parallel with each other. Process A Process B Process C Time 17 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  18. Checkpoint Monday, November 21, 2011

  19. Context switching Processes are managed by a shared chunk of OS code called the kernel – Not a separate process, but runs as part of user process Control flow passes from one process to another via a context switch. A context is all the data needed to restart a process, e.g., register values, stack values, page table, … Process A Process B code code user code context switch kernel code Time user code context switch kernel code user code 18 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  20. Private address spaces Each process has its own private address space. 0xffffffff kernel virtual memory memory (code, data, heap, stack) invisible to 0xc0000000 user code user stack (created at runtime) %esp (stack pointer) memory mapped region for shared libraries 0x40000000 brk run-time heap (managed by malloc) read/write segment (.data, .bss) loaded from the read-only segment executable file (.init, .text, .rodata) 0x08048000 unused 0 19 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

  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"); Fork is interesting } else { (and often confusing) printf("hello from parent\n"); because it is called } once but returns twice 20 EECS 213 Introduction to Computer Systems Northwestern University Monday, November 21, 2011

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend