490 likes | 705 Views
Principles of Operating Systems: Design & Applications. Chapter 5 Principles of Process Management. Objectives. understand what a process is and how it differs from a program understand how the process table and process state are used to represent and manage processes
E N D
Principles of Operating Systems: Design & Applications Chapter 5 Principles of Process Management
Objectives • understand what a process is and how it differs from a program • understand how the process table and process state are used to represent and manage processes • be able to describe a number of process-related OS services • understand the blocked, ready, and running states
Objectives (cont.) • be able to list some items stored in the process table • understand each of the scheduling algorithms: FCFS, SJF, RR, priority scheduling, and multilevel feedback queues • understand why FCFS and SJF are not well-suited for time-sharing • understand two-level scheduling
Objectives (cont.) • understand real-time scheduling (if covered) • be able to trace the steps in context switching • be able to describe what happens when processes are created and destroyed • understand race conditions • understand how mutual exclusion locking can deal with race conditions
Objectives (cont.) • understand how interrupt control, atomic test and set instructions, Peterson's algorithm, semaphores, monitors, and message passing can be used to provide mutual exclusion • be able to identify the correct mutex mechanism(s) needed in a given situation • understand how counted semaphores can be used beyond simple mutex locks
Objectives (cont.) • understand how mutual exclusion can lead to deadlock • be able to list the four necessary and sufficient conditions for deadlock • understand how breaking one of the conditions will eliminate deadlock • understand the four basic approaches to handling deadlock • understand the Banker's Algorithm and be able to run through examples by hand
What is a process? • An instance of a running program • A memory space and a locus of control • Sequence of states during the execution of a program Principles of Operating Systems: Design & Applications 7
Process Operations • Create/terminate process • Change program • Set/get process parameters • Block process • Awaken process • Switch process • Schedule process Principles of Operating Systems: Design & Applications 8
Process State • Running: currently being executed on the CPU • Ready: not waiting on any event, but not currently using the CPU • Blocked: waiting on an event, often I/O Principles of Operating Systems: Design & Applications 9
Process State (cont.) Principles of Operating Systems: Design & Applications 10
Process Table • One entry for each process • Some typical data: • Saved registers • Process state • Process ID • Owner/group ID • Priority • Memory usage • Open files Principles of Operating Systems: Design & Applications 11
Threads • Parent process elements: • Memory • Open files • Name space • Environment • Child process can: • Inherit by sharing • Inherit by copying • Replace Principles of Operating Systems: Design & Applications 12
Scheduling • Select the next process to run • Consider processes in the ready state • Approaches different for batch systems and for time-sharing systems Principles of Operating Systems: Design & Applications 13
First Come, First Served • Batch scheduling technique • Jobs processed in the order they arrived • Implemented with a queue Principles of Operating Systems: Design & Applications 14
Shortest Job First • Batch scheduling technique • Process jobs in increasing order of running time • Optimal: minimizes average turnaround time • Requires knowledge of running time Principles of Operating Systems: Design & Applications 15
Round-Robin • Time-sharing analog of FCFS • Processes taken in turn • Processes given a time slice, then preempted • Implemented with a circular queue Principles of Operating Systems: Design & Applications 16
Priority Scheduling • Each process has priority • Process with the highest priority is selected • Static and dynamic priorities • Upper and lower limits common on dynamic priorities • Often give preference to interactive processes over compute-bound processes Principles of Operating Systems: Design & Applications 17
Multilevel Feedback Queue • A technique for implementing dynamic priorities • One queue per priority level • Processes can be boosted to higher priority • Processes move to lower priority queues at the end of time slices Principles of Operating Systems: Design & Applications 18
Multilevel Feedback Queue (cont.) Principles of Operating Systems: Design & Applications 19
Scheduling Parameters • Parameters affecting running time give priority • Quantum is the length of a time slice • Longer quanta give more run time: higher priority • In CTSS, lower priority levels had longer quanta • In Windows, longer quanta for “foreground” process Principles of Operating Systems: Design & Applications 20
Two-Level Scheduling • More ready processes than fit in memory can lead to thrashing • Solution: two levels of scheduling • Long term: decide which processes are in memory • Short term: decide next process to run among only those in memory Principles of Operating Systems: Design & Applications 21
Real-Time Scheduling • Time critical systems • Event Driven • Processes run in response to events • Usually triggered by interrupts • Often not preempted • Earliest Deadline First • Like shortest job first • Deadlines determine the latest something can happen Principles of Operating Systems: Design & Applications 22
Real-Time Scheduling (cont.) • Slotted • Major scheduling cycle • Each process has one or more slots in cycle • Processes are given the slot whether or not they have work to do • Guarantees exactly when processes run Principles of Operating Systems: Design & Applications 23
Context Switching • Give CPU to another process: • Transfer control to kernel (usually interrupt) • Save old process's configuration • Schedule new process • Restore new process's configuration • Return to new process Principles of Operating Systems: Design & Applications 24
Context Switching (cont.) Principles of Operating Systems: Design & Applications 25
Process Creation • Fork creates copy of parent • Spawn creates new process running new program • Mechanism: • Create new process table entry • Copy parent memory (some versions of fork) • Load new program (for spawn) • Set up initial saved configuration (for spawn) Principles of Operating Systems: Design & Applications 26
Process Termination • Initiated by ending process or another process • Mechanism: • Close open files • Reduce usage count on shared resources • Free nonshared memory • Send exit status to parent (or other waiting process) Principles of Operating Systems: Design & Applications 27
Critical Sections • Code containing a race condition • Correct outcome depends on relative ordering of events • Example: if( head == NULL ) head = new_elem ; else tail->next = new_elem ; tail = new_elem ; Principles of Operating Systems: Design & Applications 28
Critical Sections (cont.) • If interrupted between test and assignments, elements can get lost • Need to make test and assignments atomic • Use mutual exclusion locks • Lock resources by putting lock/unlock operations around critical sections Principles of Operating Systems: Design & Applications 29
Mutual Exclusion Techniques • Interrupt control • Disable interrupts prior to entering critical section • Enable on leaving • Alternatively, change interrupt priority level • No other thread of control can execute this code while interrupts are disabled on a single processor Principles of Operating Systems: Design & Applications 30
Mutual Exclusion Techniques • Test and set instruction • Atomically tests a variable and sets it • Example: mutex_lock: tas _lock blt mutex_lock ret • Works on multiple processors • Busy waiting Principles of Operating Systems: Design & Applications 31
Mutual Exclusion Techniques • Peterson's Algorithm • Does not need special hardware features • Busy waiting void mutex_lock( int who ) { other = 1 – who ; want[who] = 1 ; turn = other ; while( want[other] && turn != who ) ; } Principles of Operating Systems: Design & Applications 32
Mutual Exclusion Techniques • Semaphores • Higher level technique, suitable for applications • Blocks: no busy waiting • Defined by two operations on semaphore s: • up(): increase the value of s by 1 • down(): if s = 0 block until s ≠ 0, decrease s by 1 • Binary semaphores restrict s to be only 0 or 1 Principles of Operating Systems: Design & Applications 33
Mutual Exclusion Techniques • Monitors • Language feature • Blocks • Only one process at a time can be in any function in a monitor • Term sometimes used for other forms of mutex locks Principles of Operating Systems: Design & Applications 34
Mutual Exclusion Techniques • Message passing • Can be used for synchronization • Based on send and receive operations • Rendezvous principle: senders block until messages are delivered Principles of Operating Systems: Design & Applications 35
Deadlock • Also called deadly embrace • Example: • A waiting on a mutex lock held by B, and • B waiting on a mutex lock held by A • No escape: neither process continues Principles of Operating Systems: Design & Applications 36
Deadlock (cont.) • Necessary and sufficient conditions • Mutual exclusion: only one process holds a resource • Hold and wait: processes don't voluntarily give up resources before they are finished • No preemption: resources aren't taken away from processes • Circular wait: cycle in the dependency graph Principles of Operating Systems: Design & Applications 37
Deadlock (cont.) • Basic approaches • Ignoring • Might be rare • Might not affect system operation • Applications should deal with application issues Principles of Operating Systems: Design & Applications 38
Deadlock (cont.) • Basic approaches (cont.) • Detecting • Look for processes in deadlock • Ones blocked for a long time or build dependency graph and look for cycles • Terminate one process in the cycle Principles of Operating Systems: Design & Applications 39
Deadlock (cont.) • Basic approaches (cont.) • Preventing • Make sure one of the conditions is broken • Resource ordering prevent cyclic dependency • No resource is allocated after one later in the ordering • Often hard to establish order on all resources Principles of Operating Systems: Design & Applications 40
Deadlock (cont.) • Basic approaches (cont.) • Avoiding • Test each request • Allow only requests where we know we won't have future deadlock • Banker's Algorithm is one approach • Requires we know future resource needs Principles of Operating Systems: Design & Applications 41
Banker's Algorithm • Temporarily grant assignment • If no process exists that could complete with the remaining resources, defer the request • Temporarily simulate that one process completes • Repeat 2-3 until all processes have complete • If all processes can complete, the request can be granted Principles of Operating Systems: Design & Applications 42
Summary • Processes are, loosely, instances of running programs • Processes are represented and managed by the process table and the process state • Key services: creation, termination, block, awaken, schedule • Classic processes and threads are two sets of choices for sharing, inheriting, or replacing components of the parent Principles of Operating Systems: Design & Applications 43
Summary (cont.) • Variety of scheduling techniques • Batch • FCFS, SJF • Time-sharing • Round robin, priority, multilevel feedback queue, changing scheduling parameters • Real-time • EDF, slotted scheduling Principles of Operating Systems: Design & Applications 44
Summary (cont.) • Race conditions are tricky • Mutual exclusion protects critical sections • Mutual exclusion techniques: • Interrupt control • Test-and-set instructions • Peterson's Algorithm • Semaphores • Monitors • Message Passing Principles of Operating Systems: Design & Applications 45
Summary (cont.) • Deadlock techniques: • Ignore it • Detect it and terminate a process • Prevent it with resource ordering • Avoid it with the Banker's Algorithm Principles of Operating Systems: Design & Applications 46