Process Control
Process Scheduling Queues Job queue set of all processes in the system. Ready queue set of all processes residing in main memory, ready and waiting to execute. Device queues set of processes waiting for an I/O device. Process migration between the various queues. Q1 2
Ready Queue, I/O Device Queues 3
Process Scheduling 4
Schedulers Long term scheduler: job scheduler selects which processes should be brought into the ready queue. invoked infrequently (seconds, minutes) controls the degree of multiprogramming Medium term scheduler allocates memory for process. invoked periodically or as needed. Short term scheduler: CPU scheduler selects which process should be executed next and allocates CPU. invoked frequently (ms) Q2 5
Process Switching Definition The activity that occurs when the OS kernel switches between processes in an effort to share the CPU among competing, runable processes Actions Save contents of hardware registers (PC, SP,...) Load PC with location of resume point Load hardware registers with new context if full context switch 6
Process Switching Process switch time is overhead CPU utilization affected by quantum and process switch time Quantum: Time slice (max CPU interval) given to a process Time dependent on hardware support Types Voluntary: Process blocks Involuntary: End of time quantum or higher priority, runable process gets control of CPU 7
Processes and the kernel processes in private virtual address spaces data data The kernel sets up process execution contexts to virtualize the machine. shared kernel code and data in shared address space system call traps...and upcalls (e.g., signals) Threads or processes enter the kernel for services. CPU and devices force entry to the kernel to handle exceptional events. 8
Virtual address space (VAS) 0 2 n-1 text data BSS user stack args/env kernel text and kernel data 0x0 A typical process VAS space includes: user regions in the lower half Virtual to physical address mappings specific to each process accessible to user or kernel code kernel regions in upper half shared by all processes accessible only to kernel code 2 n -1 0xffffffff 9
Modes of processor execution User mode: Some parts of virtual address space can not be accessed Some instructions (e.g., memory management) can not be executed Kernel mode: Can access kernel address space Is a fixed part of the virtual address space of every process System call puts user into kernel mode 10
Entering the kernel The machine defines a small set of exceptional event types what conditions raise each event The kernel installs handlers for each event at boot time e.g., a table in kernel memory read by the machine The machine transitions to kernel mode only on an exceptional event The kernel defines the event handlers Therefore the kernel chooses what code will execute in kernel mode, and when 11
CPU Events An unnatural change in control flow An interrupt is caused by an external event device requests attention, timer expires, etc. An exception is caused by an executing instruction CPU requires software intervention to handle a fault or trap the kernel defines a handler routine for each event type Q3 12
CPU Process Switch 13
State of a processor At any time, the state of a processor is defined by: Mode: given by the mode bit Is the CPU executing in the protected kernel or a user program? Space: defined by virtual to physical address translations currently in effect What address space is the CPU running in? Once the system is booted, it always runs in some virtual address space. Context: given by register state and execution stream Is the CPU executing a thread/process, or an interrupt handler? Where is the stack? 14
Mode, Space and Context User code executes in a process/thread context in a process address space, in user mode. Can address only user code/data defined for the process, with no access to privileged instructions. System services execute in a process/thread context in a process address space, in kernel mode. Can address kernel memory or user process code/data, with access to protected operations: may sleep in the kernel. 15
Mode, Space and Context Interrupts execute in a system interrupt context in the address space of the interrupted process, in kernel mode. Can access kernel memory and use protected operations. no sleeping! 16
Mode, Space and Context Process Context Application (user space) System calls Exceptions (user + system space) Kernel Context X not allowed Interrupts, System tasks (user space) System call Interrupt occurs User Mode Kernel Mode More privileges 17
Execution of the OS 18
Execution within user processes 19
The UNIX Kernel loaded at boot time and initializes system, creates initial system processes remains in memory and manages the system Resource manager/mediator process is key abstraction Time share (time slice) the CPU, coordinate access to peripherals, manage virtual memory Performs privileged operations 20
The UNIX Kernel Provides synchronization primitives. Well defined entry points syscall, Exceptions, or interrupts. 21
Entry into the Kernel Synchronous kernel performs work on behalf of the process: System call interface (UNIX API): central component of the UNIX API Hardware exceptions: unusual action of process Asynchronous kernel performs tasks that are possibly unrelated to current process. Hardware interrupts: devices assert hardware interrupt mechanism to notify kernel of events which require attention (I/O completion, status change, real time clock etc) System processes scheduled by OS swapper and page daemon. 22
Trap or Interrupt Processing Hardware switches to kernel mode, using the per/process kernel stack HW saves PC, Status word and possibly other state on kernel stack Assembly routine saves any other information necessary and dispatches event On completion a return from interrupt (RFI) instruction is performed 23
Interrupt handling Asynchronous event Name three? Must be serviced in system context, What does this mean? Must not block, Why? What happens to the currently running process? 24
Exception handling Synchronous to the currently running process What is an example? Must run in the current processes context Why? An Interrupt may occur during the processing of an exception or trap Could this be a problem? 25
Software Interrupts Interrupts typically have the highest priority in a system Software interrupts are assigned priorities above that of user processes but below that of interrupts Software interrupts are typically implemented in software Examples: Callout queue processing Network packet processing 26
System Call Interface System call API Why do we need one? Implemented as a set of assembly language stubs Trap into kernel dispatch routine which invokes a high level system call User privileges are verified and any data is copied into kernel (copyin()) On return, copy out any user data (copyout()), check for an Asynchronous System Trap (signal, preemption, etc) 27
Mode switch Revisited How to set and reset the mode? Hardware level support required For example, when an interrupt is detected and the decision to service it is made, at the hardware level the mode bit (part of the Program Status Word) is set appropriately When the ISR is complete it resets the bit 28
Process switching Revisited When can a process switch occur? Anytime the kernel executes for a process Examples: Interrupt: external to the currently running process clock INT, I/O interrupt, memory fault (page or segment) Trap: error exception internal to the program may have a recovery procedure or simply notify and quit Supervisor call: explicit request from the program (system call) Q4 29
Timer interrupt 30
Mode switch vs Process switch When a mode switch occurs, the PCB of the process that was running is still marked Running and is not placed in the Blocked or Ready queue However, depending on what caused the mode switch, a process switch could follow 31