Advanced Operating Systems - Lecture 4: Process. This lecture will cover the following: process definition; what are address spaces; components of an address space; methods of altering control flow of a CPU; interrupts, traps and faults; how does a process enter into the operating system; context switching;...
Overview of today’s lecture Process definition What are Address spaces Components of an address space Methods of altering control flow of a CPU Interrupts, Traps and faults How does a process enter into the operating system Context switching Introduction to process management models and state machines Re-cap of the lecture Process A program in execution An instance of a program running on a computer The entity that can be assigned to and executed on a processor A unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system instructions Private Address Spaces Each process has its own private address space 0xffffffff 0xc0000000 0x40000000 0x08048000 kernel virtual memory (code, data, heap, stack) user stack (created at runtime) memory invisible to user code %esp (stack pointer) memory mapped region for shared libraries run-time heap (managed by malloc) read/write segment (.data, bss) read-only segment (.init, text, rodata) unused brk loaded from the executable file Implementing the Process Abstraction Pj CPU PPi Executable i Executable Memory Memory PPj Executable j Executable Memory Memory Pk CPU … PPExecutable Executable k k Memory Memory OS interface CPU ALU Control Unit OS OS Address Address Space Space PPi Address i Address Space Space PPk Address k Address Space Space … P Address Pj j Address Space Space Machine Executable Memory Pi CPU The Address Space Address Space Process Process Address Binding Executable Memory Files Other objects Execution of the Operating System Non-process Kernel Execute kernel outside of any process Operating system code is executed as a separate entity that operates in privileged mode Execution Within User Processes Operating system software within context of a user process Process executes in privileged mode when executing operating system code Execution of the Operating System Process-Based Operating System Implement operating system as a collection of system processes Useful in multi-processor or multi-computer environment Control Flow Computers 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 Time inst1 inst2 inst3 … instn Altering the Control Flow Two basic mechanisms available to the programmer for changing control flow: Jumps and branches Function 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” 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 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) Either re-executes faulting (“current”) instruction or aborts Aborts unintentional and unrecoverable Examples: parity error, machine check Aborts current program Trap Example Opening a File User calls open(filename, options) 0804d070 < libc_open>: 804d082: cd 80 804d084: 5b int pop $0x80 %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 int pop OS exception return Open file Fault Example # 1 Memory Reference User writes to memory location That portion (page) of user’s memory is currently on disk 80483b7: c7 05 10 9d 04 08 0d $0xd,0x8049d10 Page handler must load page into physical memory Returns to faulting instruction Successful on second try User Process event movl int a[1000]; main () { a[500] = 13; } movl OS page fault return Create page and load into memory Fault Example # 2 int a[1000]; main () { a[5000] = 13; } Memory Reference 80483b7: User writes to memory location Address is not valid c7 05 60 e3 04 08 0d $0xd,0x804e360 Page handler detects invalid address Sends SIGSEG signal to user process User process exits with “segmentation fault” User Process event movl movl OS page fault Detect invalid address Signal process The Abstract Machine Interface Application Program Abstract Machine Instructions Trap Instruction User Mode Instructions fork() open() OS User Mode User Mode Instructions Instructions Supervisor Mode Supervisor Mode Instructions Instructions create() Context Switching Executable Memory Initialization Interrupt Process Manager Interrupt Handler P1 P2 Pn When to Switch a Process Clock interrupt process has executed for the maximum allowable time slice I/O interrupt Memory fault memory address is in virtual memory so it must be brought into main memory When to Switch a Process Trap error or exception occurred may cause process to be moved to Exit state Supervisor call such as file open Process Creation Assign a unique process identifier Allocate space for the process Initialize process control block Set up appropriate linkages Ex: add new process to linked list used for scheduling queue Create or expand other data structures Ex: maintain an accounting file Change of Process State Save context of processor including program counter and other registers Update the process control block of the process that is currently in the Running state Move process control block to appropriate queue – ready; blocked; ready/suspend Select another process for execution Change of Process State Update the process control block of the process selected Update memory-management data structures Restore context of the selected process TwoState Process Model Process may be in one of two states Running Not-running NotRunning Process in a Queue Fivestate Model Processes may be waiting for I/O Use additional states: Running: currently being run Ready: ready to run Blocked: waiting for an event (I/O) New: just created, not yet admitted to set of runnable processes Exit: completed/error exit Fivestate Model May have separate waiting queues for each event Transitions: Null New – Process is created New Ready – O.S is ready to handle another process (Memory, CPU) Ready Running – Select another process to run Running Exit – Process has terminated Running Ready – End of time slice or higher-priority process is ready Running Blocked – Process is waiting for an event (I/O, Synchronization) Blocked Ready – The event a process is waiting for has occurred, can continue Ready Exit – Process terminated by O.S or parent Blocked Exit – Same reasons ... Operating system software within context of a user process Process executes in privileged mode when executing operating system code Execution of the? ?Operating? ?System Process-Based Operating. .. program Trap Example Opening a File User calls open(filename, options) 0804d070 < libc_open>: 804d082: cd 80 804d0 84: 5b int pop $0x80 %ebx Function open executes system call instruction... Execution of the? ?Operating? ?System Process-Based Operating System Implement operating system as a collection of system processes Useful in multi-processor or multi-computer environment Control Flow Computers Only