1 / 62

Exceptions and Processes

Exceptions and Processes. Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University. Control Flow. Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a time

dobry
Download Presentation

Exceptions and Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University

  2. Control Flow • Processors 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 CPU’s control flow (or flow of control) Physical control flow <startup> inst1 inst2 inst3 … instn <shutdown> Time

  3. 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 • instruction divides by zero • user hits Ctrl-C at the keyboard • System timer expires • System needs mechanisms for “exceptional control flow”

  4. Exceptional Control Flow • Exists at all levels of a computer system • Low level mechanisms • 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)

  5. Exceptions • An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) • Examples: div by 0, arithmetic overflow, page fault, I/O request completes, Ctrl-C User Process OS exception event I_current exception processing by exception handler I_next • return to I_current • return to I_next • abort

  6. Interrupt Vectors • Each type of event has a unique exception number k • k = index into exception table (a.k.a. interrupt vector) • Handler k is called each time exception k occurs Exception numbers code for exception handler 0 Exception Table code for exception handler 1 0 1 code for exception handler 2 2 ... ... n-1 code for exception handler n-1

  7. 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 Ctrl-C at the keyboard • arrival of a packet from a network • arrival of data from a disk • Hard reset interrupt • hitting the reset button • Soft reset interrupt • hitting Ctrl-Alt-Delete on a PC

  8. Synchronous Exceptions • Caused by events that occur as a result of executing an instruction: • Traps • Intentional • Examples: system calls, breakpoint traps, special instructions • Returns control to “next” instruction • Faults • Unintentional but possibly recoverable • Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions • Either re-executes faulting (“current”) instruction or aborts • Aborts • unintentional and unrecoverable • Examples: parity error, machine check • Aborts current program

  9. Trap Example: Opening File • User calls: open(filename, options) • Function open executes system call instruction int • OS must find or create file, get it ready for reading or writing • Returns integer file descriptor 0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . . User Process OS exception int pop open file returns

  10. Fault Example: Page Fault int a[1000]; main () { a[500] = 13; } • 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 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 User Process OS exception: page fault movl Create page and load into memory returns

  11. Fault Example: Invalid Memory Reference int a[1000]; main () { a[5000] = 13; } • Page handler detects invalid address • Sends SIGSEGV signal to user process • User process exits with “segmentation fault” 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 User Process OS exception: page fault movl detect invalid address signal process

  12. User mode vs. Kernel mode • Priviliged instructions

  13. Exception handlers • Return adress • Depends on the class of exception • Current instruction (page fault) • Next instruction • Push some additional processor state • Necessary to restart the interrupted program when the handler returns • If the control is being transferred from user to kernel • All these items are pushed on the kernel stack • Exception handlers run in kernel mode

  14. Exception Table IA32 (Excerpt) Check pp. 183: http://download.intel.com/design/processor/manuals/253665.pdf

  15. int main() { write(1, “hello, world\n”, 13); exit(0); } # write our string to stdout movl $len,%edx # third argument: message length movl $msg,%ecx # second argument: message to write movl $1,%ebx # first argument: file handle (stdout) movl $4,%eax # system call number (sys_write) int $0x80 # call kernel # and exit movl $0,%ebx # first argument: exit code movl $1,%eax # system call number (sys_exit) int $0x80 # call kernel .data # section declaration msg: .ascii "Hello, world!\n" # our dear string len = . - msg # length of our dear string

  16. Today • Exceptional Control Flow • Processes

  17. Processes • Definition: 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 virtual 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 • we’ll talk about this in a couple of weeks

  18. What is a process? • A process is the OS's abstraction for execution • A process represents a single running application on the system • Process has three main components: • 1) Address space • The memory that the process can access • Consists of various pieces: the program code, static variables, heap, stack, etc. • 2) Processor state • The CPU registers associated with the running process • Includes general purpose registers, program counter, stack pointer, etc. • 3) OS resources • Various OS state associated with the process • Examples: open files, network sockets, etc.

  19. Process Address Space • The range of virtual memory addresses that the process can access • Includes the code of the running program • The data of the running program (static variables and heap)‏ • An execution stack • Local variables and saved registers for each procedure call 0xFFFFFFFF (Reserved for OS)‏ Stack Stack pointer Address space Heap Uninitialized vars (BSS segment)‏ Initialized vars (data segment)‏ Code (text segment)‏ Program counter 0x00000000

  20. Process Address Space • Note!!! This is the process's own view of the address space --- physical memory may not be laid out this way at all. • In fact, on systems that support multiple running processes, it's pretty muchguaranteed to look different. • The virtual memory system provides this illusion to each process. 0xFFFFFFFF (Reserved for OS)‏ Stack Stack pointer Address space Heap Uninitialized vars (BSS segment)‏ Initialized vars (data segment)‏ Code (text segment)‏ Program counter 0x00000000

  21. Execution State of a Process • Each process has an execution state • Indicates what the process is currently doing • Running: • Process is currently using the CPU • Ready: • Currently waiting to be assigned to a CPU • That is, the process could be running, but another process is using the CPU • Waiting (or sleeping): • Process is waiting for an event • Such as completion of an I/O, a timer to go off, etc. • Why is this different than “ready” ? • As the process executes, it moves between these states • What state is the process in most of the time?

  22. New Ready Waiting Terminated Running Process State Transitions • What causes schedule and unschedule transitions? create I/O done unschedule schedule kill or exit I/O, page fault, etc.

  23. Process Control Block • OS maintains a Process Control Block (PCB) for each process • The PCB is a big data structure with many fields: • Process ID • User ID • Execution state • ready, running, or waiting • Saved CPU state • CPU registers saved the last time the process was suspended. • OS resources • Open files, network sockets, etc. • Memory management info • Scheduling priority • Give some processes higher priority than others • Accounting information • Total CPU time, memory usage, etc.

  24. 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 Process B user code context switch kernel code Time user code context switch kernel code user code

  25. PID 1342 State: Running PID 4277 State: Ready PID 8109 State: Ready PC PC PC Registers Registers Registers Save current CPU state PC Registers Context Switching • The act of swapping a process state on or off the CPU is a context switch Currently running process

  26. PID 1342 State: Ready PID 4277 State: Ready PID 8109 State: Ready PC PC PC Registers Registers Registers PC Registers Context Switching • The act of swapping a process state on or off the CPU is a context switch Suspend process

  27. PID 1342 State: Ready PID 4277 State: Running PID 8109 State: Ready PC PC PC Registers Registers Registers Restore CPU state of new process PC PC Registers Registers Context Switching • The act of swapping a process state on or off the CPU is a context switch Pick next process

  28. Context Switch Overhead • Context switches are not cheap • Generally have a lot of CPU state to save and restore • Also must update various flags in the PCB • Picking the next process to run – scheduling – is also expensive • Context switch overhead in Linux 2.4.21 • About 5.4 usec on a 2.4 GHz Pentium 4 • This is equivalent to about 13,200 CPU cycles! • Not quite that many instructions since CPI > 1

  29. Context Switching in Linux Process A Process A is happily running along... time

  30. User Kernel Timer interrupt handler Context Switching in Linux Process A 1) Timer interrupt fires 2) PC saved on stack time

  31. User Kernel 4) Call schedule() routine Scheduler Context Switching in Linux Process A 1) Timer interrupt fires 2) PC saved on stack Timer interrupt handler 3) Rest of CPU statesaved in PCB time

  32. Process B 7) Return from interrupt handler – process CPU state restored User Kernel Timer interrupthandler 6) Resume Process B (suspended within timer interrupt handler!)‏ Context Switching in Linux Process A 1) Timer interrupt fires 2) PC saved on stack Timer interrupt handler 3) Rest of CPU statesaved in PCB 4) Call schedule() routine Scheduler 5) Decide next process to run time

  33. PID 4277 State: Ready PID 4391 State: Ready PC PC Registers Registers PID 4110 State: Waiting PID 4002 State: Waiting PID 4923 State: Waiting PC PC PC Registers Registers Registers State Queues • The OS maintains a set of state queues for each process state • Separate queues for ready and waiting states • Generally separate queues for each kind of waiting process • e.g., One queue for processes waiting for disk I/O • Another queue for processes waiting for network I/O, etc. Ready queue Disk I/O queue

  34. PID 4277 State: Ready PID 4391 State: Ready PID 4923 State: Ready PC PC PC Registers Registers Registers PID 4923 State: Waiting PID 4110 State: Waiting PID 4002 State: Waiting PC PC PC Registers Registers Registers State Queue Transitions • PCBs move between these queues as their state changes • When scheduling a process, pop the head off of the ready queue • When I/O has completed, move PCB from waiting queue to ready queue Ready queue Disk I/O queue Disk I/O completes

  35. Process Creation • One process can create, or fork, another process • The original process is the parent • The new process is the child • What creates the first process in the system, and when? • Parent process defines resources and access rights of children • Just like real life ... • e.g., child process inherits parent's user ID % pstree -p init(1)-+-apmd(687)‏ |-atd(847)‏ |-crond(793)‏ |-rxvt(2700)---bash(2702)---ooffice(2853)‏ `-rxvt(2752)---bash(2754)‏

  36. UNIX fork mechanism In UNIX, use the fork() system call to create a new process This creates an exact duplicate of the parent process!! Creates and initializes a new PCB Creates a new address space Copies entire contents of parent's address space into the child Initializes CPU and OS resources to a copy of the parent's Places new PCB on ready queue PID 4109 State: Running PID 4110 State: Ready PID 4110 State: Ready PID 4110 State: Ready Copy state PC PC PC PC Registers Registers Registers Registers PID 4277 State: Ready PID 4391 State: Ready Add to end of ready queue PC PC Registers Registers Process calls fork()‏ Ready queue

  37. 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 • Fork is interesting (and often confusing) because it is called oncebut returns twice pid_tpid = fork(); if (pid== 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); }

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

  39. Fork Example #1 • 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 void fork1() { int x = 1; pid_tpid = 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); }

  40. Bye L1 Bye Bye L0 L1 Bye Fork Example #2 • Both parent and child can continue forking void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); }

  41. Bye L2 Bye Bye L1 L2 Bye Bye L2 Bye Bye L0 L1 L2 Bye Fork Example #3 • Both parent and child can continue forking void fork3() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("L2\n"); fork(); printf("Bye\n"); }

  42. Bye Bye Bye L0 L1 L2 Bye Fork Example #4 • Both parent and child can continue forking void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } } printf("Bye\n"); }

  43. Bye L2 Bye L1 Bye L0 Bye Fork Example #4 • Both parent and child can continue forking void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } } printf("Bye\n"); }

  44. Why have fork() at all? • Why make a copy of the parent process? • Don't you usually want to start a new program instead? • Where might “cloning” the parent be useful?

  45. Why have fork() at all? • Why make a copy of the parent process? • Don't you usually want to start a new program instead? • Where might “cloning” the parent be useful? • Web server – make a copy for each incoming connection • Parallel processing – set up initial state, fork off multiple copies to do work • UNIX philosophy: System calls should be minimal. • Don't overload system calls with extra functionality if it is not always needed. • Better to provide a flexible set of simple primitives and let programmerscombine them in useful ways.

  46. Memory concerns So fork makes a copy of a process. What about memory usage? Child #5 Child #2 Child #10 Parent (Reserved for OS)‏ (Reserved for OS)‏ (Reserved for OS)‏ Child #1 (Reserved for OS)‏ Child #6 Stack Stack Stack Child #4 (Reserved for OS)‏ Stack (Reserved for OS)‏ Stack (Reserved for OS)‏ Child #7 Heap Child #3 Heap Heap Stack Heap Uninitialized vars (BSS segment)‏ Stack Uninitialized vars (BSS segment)‏ Uninitialized vars (BSS segment)‏ (Reserved for OS)‏ (Reserved for OS)‏ Child #9 Uninitialized vars (BSS segment)‏ Heap Child #8 Initialized vars (data segment)‏ Initialized vars (data segment)‏ Initialized vars (data segment)‏ Heap Stack Uninitialized vars (BSS segment)‏ Stack Initialized vars (data segment)‏ Code (text segment)‏ (Reserved for OS)‏ Heap Code (text segment)‏ Code (text segment)‏ Uninitialized vars (BSS segment)‏ (Reserved for OS)‏ Initialized vars (data segment)‏ Code (text segment)‏ Uninitialized vars (BSS segment)‏ Stack Initialized vars (data segment)‏ Stack Heap Code (text segment)‏ Heap Initialized vars (data segment)‏ Code (text segment)‏ Uninitialized vars (BSS segment)‏ Uninitialized vars (BSS segment)‏ Code (text segment)‏ Heap Initialized vars (data segment)‏ Heap Initialized vars (data segment)‏ Uninitialized vars (BSS segment)‏ Uninitialized vars (BSS segment)‏ Code (text segment)‏ Code (text segment)‏ Initialized vars (data segment)‏ Initialized vars (data segment)‏ Code (text segment)‏ Code (text segment)‏

  47. Problematic?

  48. Memory concerns OS aggressively tries to share memory between processes. Especially processes that are fork()'d copies of each other Copies of a parent process do not actually get a private copyof the address space... ... Though that is the illusion that each process gets. Instead, they share the same physical memory, until one of them makes a change. The virtual memory system is behind these shenanigans. We will discuss this in much detail later in the course

  49. exit: Ending a 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); }

  50. 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 • So, only need explicit reaping in long-running processes • e.g., shells and servers

More Related