Exceptions and Processes

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Chapter 3 Process Description and Control
1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
1 Processes Professor Jennifer Rexford
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
1 Operating Systems and Protection Professor Jennifer Rexford CS 217.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Introduction to Interrupts
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
OS Spring’04 Introduction Operating Systems Spring 2004.
Exceptions, Interrupts & Traps
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Chapter 3 Process Description and Control
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
1 Exceptions and Processes Jennifer Rexford Much of the material for this lecture is drawn from Computer Systems: A Programmer’s Perspective (Bryant &
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
What is a Process ? A program in execution.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Exceptional Control Flow
CS 3214 Computer Systems Lecture 9 Godmar Back.
Processes and threads.
Process concept.
Operating Systems CMPSC 473
Exceptional Control Flow
Process Management Process Concept Why only the global variables?
Microprocessor Systems Design I
Exceptional Control Flow
Exceptional Control Flow
Chapter 3 – Process Concepts
Overview of today’s lecture
Exceptional Control Flow
Intro to Processes CSSE 332 Operating Systems
Exceptional Control Flow: System Calls, Page Faults etc.
Structure of Processes
Process & its States Lecture 5.
Exceptions Control Flow
Architectural Support for OS
CSCE 313 – Introduction to UNIx process
CS201 - Lecture 17 Exceptions
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Operating Systems: A Modern Perspective, Chapter 6
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CS510 Operating System Foundations
Architectural Support for OS
CSE 153 Design of Operating Systems Winter 2019
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Exceptions and Processes Jennifer Rexford The material for this lecture is drawn from Computer Systems: A Programmer’s Perspective (Bryant & O’Hallaron) Chapter 8

Goals of this Lecture Help you learn about: Exceptions The process concept … and thereby… How operating systems work How applications interact with OS and hardware The process concept is one of the most important concepts in systems programming

Context of this Lecture Second half of the course Previously Starting Now C Language Application Program service levels tour language levels tour Assembly Language Operating System Machine Language Hardware Application programs, OS, and hardware interact via exceptions

Motivation Question: Answers: Exceptions… How does a program get input from the keyboard? How does a program get data from a (slow) disk? Executing program thinks it has exclusive control of CPU But multiple programs share one CPU (or a few CPUs) How is that illusion implemented? Executing program thinks it has exclusive use of memory But multiple programs must share one memory Answers: Exceptions…

Exceptions Exception Examples: Synchronous Asynchronous An abrupt change in control flow in response to a change in processor state Examples: Application program: Requests I/O Requests more heap memory Attempts integer division by 0 Attempts to access privileged memory Accesses variable that is not in real memory (see upcoming “Virtual Memory” lecture) User presses key on keyboard Disk controller finishes reading data Synchronous Asynchronous

Exceptions Note Note: Exceptions in OS ≠ exceptions in Java Implemented using try/catch and throw statements

Exceptional Control Flow Application program Exception handler in operating system exception exception processing exception return (optional)

Exceptions vs. Function Calls Exceptions are similar to function calls Control transfers from original code to other code Other code executes Control returns to original code Exceptions are different from function calls Processor pushes additional state onto stack E.g. values of all registers (including EFLAGS) Processor pushes data onto OS’s stack, not application’s stack Handler runs in privileged mode, not in user mode Handler can execute all instructions and access all memory Control might return to next instruction Control sometimes returns to current instruction Control sometimes does not return at all!

Classes of Exceptions There are four classes of exceptions… Interrupts Traps Faults Aborts

(1) Interrupts An alternative to wasteful polling! Application program Exception handler (2) After current instr finishes, control passes to handler (1) CPU interrupt pin goes high (3) Handler runs (4) Handler returns control to next instr Instead of polling hardware devices to wait for their response, each device is responsible for notifying the processor about its current state. When a hardware device needs the processor's attention, it simply sends an electrical signal (hardware interrupt) through a dedicated pin in the interrupt controller chip (located on the computer's motherboard). The interrupt controller serves as an intermediate between the hardware devices and the processor. Its responsibility is to alert the processor when one of the hardware devices needs its immediate attention. In this case, the processor stops its current activity and jumps to execute a function (interrupt handler) which was previously associated with the calling device (or more accurately, associated with the interrupt vector of the device). Cause: Signal from I/O device Examples: User presses key Disk controller finishes reading/writing data Timer to trigger another application to run An alternative to wasteful polling!

(2) Traps Application program Exception handler (2) Control passes to handler (1) Application pgm traps (3) Handler runs (4) Handler returns control to next instr Cause: Intentional (application program requests OS service) Examples: Application program requests more heap memory Application program requests I/O Traps provide a function-call-like interface between application and OS

(3) Faults Application program Exception handler (2) Control passes to handler (1) Current instr causes a fault (3) Handler runs (4) Handler returns control to current instr, or aborts Cause: Application program causes (possibly) recoverable error Examples: Application program accesses privileged memory (segmentation fault) Application program accesses data that is not in real memory (page fault)

(4) Aborts Application program Exception handler (2) Control passes to handler (1) Fatal hardware error occurs (3) Handler runs (4) Handler aborts execution Cause: Non-recoverable error Example: Parity check indicates corruption of memory bit (overheating, cosmic ray!, etc.)

Summary of Exception Classes Cause Asynch/Synch Return Behavior Interrupt Signal from I/O device Asynch Return to next instr Trap Intentional Sync Fault (Maybe) recoverable error (Maybe) return to current instr Abort Non-recoverable error Do not return

Exceptions in Intel Processors Each exception has a number Some exceptions in Intel processors: Exception # Exception Fault: Divide error 13 Fault: Segmentation fault 14 Fault: Page fault (see “Virtual Memory” lecture) 18 Abort: Machine check 32-127 Interrupt or trap (OS-defined) 128 Trap 129-255

Traps in Intel Processors To execute a trap, application program should: Place number in EAX register indicating desired functionality Place parameters in EBX, ECX, EDX registers Execute assembly language instruction “int 128” Example: To request more heap memory… In Linux, 45 indicates request for more heap memory movl $45, %eax movl $1024, %ebx int $128 Request is for 1024 bytes Causes trap

System-Level Functions For convenience, traps are wrapped in system-level functions Example: To request more heap memory… sbrk() is a system-level function /* unistd.h */ void *sbrk(intptr_t increment); … /* unistd.s */ Defines sbrk() in assembly lang Executes int instruction … /* client.c */ … sbrk(1024); A call of a system-level function, that is, a system call See Appendix for list of some Linux system-level functions

Processes Program Process Executable code Process An instance of a program in execution Each program runs in the context of some process Context consists of: Process ID Address space TEXT, RODATA, DATA, BSS, HEAP, and STACK Processor state EIP, EFLAGS, EAX, EBX, etc. registers Etc.

Significance of Processes Process is a profound abstraction in computer science The process abstraction provides application pgms with two key illusions: Private control flow Private address space

Private Control Flow: Illusion Process 1 Process 2 Time Hardware and OS give each application process the illusion that it is the only process running on the CPU

Private Control Flow: Reality Process 1 OS Process 2 Exception Return from exception Time Exception Return from exception Exception Return from exception All application processes -- and the OS process -- share the same CPU(s)

Context Switches Context switch Exceptions can be caused: The activity whereby the OS assigns the CPU to a different process Occurs during exception handling, at discretion of OS Exceptions can be caused: Synchronously, by application pgm (trap, fault, abort) Asynchronously, by external event (interrupt) Asynchronously, by hardware timer So no process can dominate the CPUs Exceptions are the mechanism that enables the illusion of private control flow

Context Switch Details State the OS needs to restart a preempted process Context switch Save the context of current process Restore the saved context of some previously preempted process Pass control to this newly restored process Process 1 Process 2 Waiting Running Save context . Load context Waiting Running Save context . Load context Running Waiting

When Should OS Do Context Switch? When a process is stalled waiting for I/O Better utilize the CPU, e.g., while waiting for disk access When a process has been running for a while Sharing on a fine time scale to give each process the illusion of running on its own machine Trade-off efficiency for a finer granularity of fairness CPU 1: I/O CPU I/O CPU I/O CPU 2: I/O CPU I/O CPU I/O

Life Cycle of a Process Running: instructions are being executed Waiting: waiting for some event (e.g., I/O finish) Ready: ready to be assigned to a processor Create Ready Running Termination Waiting

Context Switch: What Context to Save? Process state New, ready, waiting, terminated CPU registers EIP, EFLAGS, EAX, EBX, … I/O status information Open files, I/O requests, … Memory management information Page tables (see “Virtual Memory” lecture) Accounting information Time limits, group ID, ... CPU scheduling information Priority, queues

Private Address Space: Illusion Process 1 Process 2 00000000 00000000 Memory for Process 1 Memory for Process 2 FFFFFFFF FFFFFFFF Hardware and OS give each application process the illusion that it is the only process using memory

Private Address Space: Reality Process 1 VM Real Memory Process 2 VM 00000000 00000000 unused unused FFFFFFFF FFFFFFFF Memory is divided into pages Disk All processes use the same real memory Hardware and OS provide application pgms with a virtual view of memory, i.e. virtual memory (VM)

Private Address Space Details Exceptions (specifically, page faults) are the mechanism that enables the illusion of private address spaces See the Virtual Memory lecture for details

Summary Exception: an abrupt change in control flow Interrupts: asynchronous; e.g. I/O completion, hardware timer Traps: synchronous; e.g. app pgm requests more heap memory, I/O Faults: synchronous; e.g. seg fault Aborts: synchronous; e.g. parity error Process: An instance of a program in execution Hardware and OS use exceptions to give each process the illusion of: Private control flow (reality: context switches) Private address space (reality: virtual memory)

Appendix: System-Level Functions Linux system-level functions for I/O management Number Function Description 3 read() Read data from file descriptor Called by getchar(), scanf(), etc. 4 write() Write data to file descriptor Called by putchar(), printf(), etc. 5 open() Open file or device Called by fopen() 6 close() Close file descriptor Called by fclose() 8 creat() Open file or device for writing Called by fopen(…, “w”) Described in I/O Management lecture

Appendix: System-Level Functions Linux system-level functions for process management Number Function Description 1 exit() Terminate the process 2 fork() Create a child process 7 waitpid() Wait for process termination wait() (Variant of previous) 11 exec() Execute a program in current process 20 getpid() Get process id Described in Process Management lecture

Appendix: System-Level Functions Linux system-level functions for I/O redirection and inter-process communication Number Function Description 41 dup() Duplicate an open file descriptor 42 pipe() Create a channel of communication between processes 63 dup2() Close an open file descriptor, and duplicate an open file descriptor Described in Process Management lecture

Appendix: System-Level Functions Linux system-level functions for dynamic memory management Number Function Description 45 brk() Move the program break, thus changing the amount of memory allocated to the HEAP sbrk() (Variant of previous) 90 mmap() Map a virtual memory page 91 munmap() Unmap a virtual memory page Described in Dynamic Memory Management lectures

Appendix: System-Level Functions Linux system-level functions for signal handling Number Function Description 27 alarm() Deliver a signal to a process after a specified amount of wall-clock time 37 kill() Send signal to a process 67 sigaction() Install a signal handler 104 setitimer() Deliver a signal to a process after a specified amount of CPU time 126 sigprocmask() Block/unblock signals Described in Signals lecture