f1f520c1cb3e3efdfe6224d85944616d.ppt
- Количество слайдов: 68
Operating Systems CS 3013 / CS 502 WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS
Agenda 2 Scheduling Policies Synchronization Deadlock
Objectives 3 Identify Scheduling Criteria Explain Different Scheduling Policies Describe Measurements for Evaluation Give Examples of Evaluation Methods
CPU Burst Duration 4
Scheduler 5 Invoked whenever the operating system must select a user-level process to execute After process creation/termination A process blocks on I/O interrupt occurs Clock interrupt occurs Type of processes Interactive jobs CPU bound jobs Somewhere inbetween
Scheduling Criteria 6 What should we consider in developing a scheduling policy?
Scheduling Criteria 7 CPU Utilization – keep the CPU as busy as possible Throughput - # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process Waiting time – amount of time process has been waiting in the ready queue Response time – amount of time from request submission until first response is produced
Scheduling Issues 8 Fairness – don’t starve process Priorities – most important first Deadlines – task X must be done by time t Optimization – throughput, response time, etc.
Scheduling Evaluation 9 Simplicity – easy to implement Job latency – time from start to completion Interactive latency – time from action start to expected system response Throughput – number of jobs completed Utilization – keep processor and/or subset of I/O devices busy Determinism – insure that jobs get done before some time or event Fairness – every job makes progress
Policies 10 First-Come-First-Serve (FCFS) Round Robin Shorted Process/Job First (SPF/SJF) Pre-emptive Shorted Process/Job First (PSPF/PSJF) Multiple-level Feedback (FB)
First-Come-First-Serve (FCFS) 11 Process Burst Time A 5 B 7 C 3 A 0 B 5 C 12 15
Shortest Process/Job First 12 Process Burst Time A 5 B 7 C 3 C 0 A 3 B 8 15
Exponential Average 13 b(t+1) = a b(t) + (1 -a) b(t) 14 12 a=0 Current value has no effect 10 8 a=1 6 Only current value matters 4 b(0) = system average, a=0. 5 2 0 0 1 a = 0. 25 3 2 a = 0. 50 5 4 a = 0. 75 7 6 8
Priority Scheduling 14 Process Burst Time Priority A 5 3 B 7 1 C 3 2 B 0 C 7 A 10 15
Priority Scheduling Issues 15 Starvation – low priority processes may never execute Use of aging Priority Inversion – some processes may never run to release resources required by other processes Increase the priority to match level of resource of level of waiting process
Round Robin 16 Process A 0 7 C B 5 B A Burst Time 3 C A B C A 9 B A B 12 B B 15
Round Robin 17 Process Burst Time A 5 B 7 C 3 Play with Quantum q = 1 q = 2 q = 5
Round Robin 18 Process Burst Time A 5 B 3 C 1 D 7 12. 0 Avg. Turnaround Time Quantum from 1 to 5 How does it change? 11. 5 11. 0 10. 5 10. 0 9. 5 9. 0 8. 5 8. 0 0 1 2 3 4 5 6
Exercise 19 Process Burst Time Priority A 10 2 B 1 1 C 2 3 Gantt Chart FCFS SJF Priority RR (q = 1) Performance Throughput Waiting time Turnaround time
Exercise 20 Process Arrival Time Burst Time A 0. 0 8 B 0. 4 4 C 1. 0 1 Turnaround time FCFS SJF (Preemptive vs. Non-preemptive) RR q= 1 RR q = 0. 5
Multilevel Queues 21 Multilevel Ready Queue Foreground (interactive) Background (batch) Each queue has its own scheduling algorithm Foreground – RR Background – FCFS Scheduling between queues is required Fixed priority scheduling Time slice
Multilevel Feedback Queue 22 Process can move between queues Parameters # of queues Scheduling algorithm for each queue Method to determine when to upgrade a process Method to determine when to demote a process Method to determine which queue a process will enter when that process needs service
Objectives 23 Identify Scheduling Criteria Explain Different Scheduling Policies Describe Measurements for Evaluation Give Examples of Evaluation Methods
Agenda 24 Scheduling Policies Synchronization Deadlock
Objectives 25 Explain reasons for synchronization Describe busy waiting solutions Describe a semaphore and its use Describe a sonitor and its use
Why synchronization? 26 Time John 3: 00 Look in fridge. Oh no, no pizza! 3: 05 Leave for store. Look in fridge. Oh no, no pizza! 3: 10 Arrive at store. Leave for store. 3: 15 Buy pizza. Arrive at store. 3: 20 Arrive home. Buy pizza. 3: 25 Put away pizza. Arrive home. 3: 30 Jane Put away pizza. Oh no!
Cooperating Processes 27 Consider a printer spooler Enter a file name into the spooler queue Printer daemon checks the queue and prints the file. Free 9 A letter proj 1 5 hw 1 6 7 “Race Conditions” B lecture 1 8 9
Race Condition Demo 28 Shared variable One program increments the shared variable. The other decrements the shared variable. // reads the value of shared memory sscanf(counter, "%d", &num); // incrememt/decrement the number num = num + incr; // writes the new value to the shared memory sprintf(counter, "%dn", num);
Producer / Consumer 29 Model for cooperating processes Producer “produces” an item that consumer “consumes” Bounded buffer (shared memory) item buffer[MAX]; /* queue */ int counter; /* number of items */
Producer 30 item i; /* item produced */ int in; /* put next item */ while (1) { produce an item; while (counter == MAX) {/* no-op */} buffer[in] = item; in = (in + 1) % MAX; counter = counter + 1; }
Consumer 31 item i; /* item consumed */ int out; /* take next item */ while (1) { while (counter == 0) {/* no-op */} item = buffer[out] out = (out + 1) % MAX; counter = counter - 1; }
Producer Consumer Trouble! 32 P R 1 = counter R 1 = 5 P R 1 = R 1 + 1 R 1 = 6 C R 2 = counter R 2 = 5 C R 2 = R 2 – 1 R 2 = 4 C counter = R 2 counter = 4 P counter = R 1 counter = 6
Critical Section 33 Mutual Exclusion Only one process inside critical region Progress No process outside critical region may block other processes wanting in Bounded Waiting No process should have to wait forever (starvation)
First Try 34 Process A Process B … While (TRUE) { … while (turn == B) ; // critical section turn = B; … } … While (TRUE) { … while (turn == A) ; // critical section turn = A; … } Shared variable turn is initialized to either A or B and both processes start execution concurrently.
Second Try 35 Process A Process B … While (TRUE) { … while (p. Binside) ; p. Ainside = TRUE; // critical section p. Ainside = FALSE; … } … While (TRUE) { … while (p. Ainside) ; p. Binside = TRUE; // critical section p. Binside = FALSE; … } Shared variables p. Ainside and p. Binside are initialized to FALSE.
Third Try 36 Process A Process B … While (TRUE) { … p. Atrying = TRUE; while (p. Btrying) ; // critical section p. Atrying = FALSE; … } … While (TRUE) { … p. Btrying = TRUE; while (p. Atrying) ; // critical section p. Btrying = FALSE; … } Shared variables p. Atrying and p. Btrying are initialized to FALSE.
Dekker’s Algorithm 37 Process A … While (TRUE) { … p. Atrying = TRUE; while (p. Btrying) if (turn == B) { p. Atrying = FALSE; while (turn == B) ; p. Atrying = TRUE; } // critical section turn = B; p. Atrying = FALSE; … } Process B … While (TRUE) { … p. Btrying = TRUE; while (p. Atrying) if (turn == A) { p. Btrying = FALSE; while (turn == A) ; p. Btrying = TRUE; } // critical section turn = A; p. Btrying = FALSE; … } turn needs to be initialized to A or B.
Peterson’s Algorithm 38 Process A Process B … While (TRUE) { … p. Atrying = TRUE; turn = B; while (p. Btrying && turn == B); // critical section p. Atrying = FALSE; … } … While (TRUE) { … p. Btrying = TRUE; turn = A; while (p. Atrying && turn == A); // critical section p. Btrying = FALSE; … } turn does not need any initialization.
Dekker’s and Peterson’s Algorithm 39 Both of them are correct. Limited to 2 processes Can be generalized to N processes N must be known and fixed. Algorithm becomes too complicated and expensive.
Synchronization Hardware 40 Test-and-Set: returns and modifies atomically int Test_and_Set(int &target) { int temp; temp = target; target = true; return temp; }
Using Test_and_Set 41 while (1) { while (Test_and_Set(lock)) ; // critical section lock = false; // remainder section } All solutions so far requires “busy waiting”.
Semaphore 42
Semaphore 43 Do not require “busy waiting” Semaphore S (shared, often initially = 1) Integer variable Accessed via two (indivisible) atomic operations wait(S) : if (S signal(S) if (S S = S – 1 < 0) then block(S) : S = S + 1; <= 0) then wakeup(S)
Critical Section with Semaphore 44 semaphore mutex; /* shared */ while (1) { wait(mutex); // critical section signal(mutex); // remainder section }
Classical Synchronization Problems 45 Bounded Buffers Readers/Writers Dining Philosophers
Readers/Writers 46 Readers can only read the content Writers read and write the object Critical Region One or more readers (no writers) One writer (nothing else) Solutions favor Reader or Writer
Readers/Writers 47 Shared semaphore mutex, wrt; int readcount; Writer wait(wrt); // write stuff signal(wrt)
Readers-Writers 48 Reader wait(mutex); readcount = readcount + 1; if (readcount == 1) wait(wrt); signal(mutex); // read stuff wait(mutex); readcount = readcount – 1; if (readcount == 0) signal(wrt); signal(mutex);
Dining Philosophers 49 Philosophers Think Eat Need 2 chopsticks to eat
Dining Philosophers 50 Philosopher i while (1) { // think wait(chopstick[i]); wait(chopstick[i+1 % 5]); // eat signal(chopstick[i]); signal(chopstick[i+1 % 5]); }
Dining Philosophers 51 Other solutions Allow at most N-1 to go hungry at a time Allow to pick up chopsticks only if both are available Asymmetric solution
Monitors 52 High-level construct Collection of Variables Data structures Functions Like C++ class One process active inside “Condition” variable Not like semaphore
Producer-Consumer Monitor 53 monitor Producer. Consumer { condition full, empty; integer count; // functions void enter(item i); item remove(); } void producer(); void consumer();
Producer-Consumer Monitor 54 void producer() { item i; while (1) { // produce item i Producer. Consumer. enter(i); } } void consumer() { item i; while (1) { i = Producer. Consumer. remove(); // consume item i } }
Producer-Consumer Monitor 55 void enter(item i) { if (count == N) sleep(full); // add item i count = count + 1; if (count == 1) then wakeup(empty); } void remove() { if (count == 0) then sleep(empty); // remove item into i count = count – 1; if (count == N-1) then wakeup(full); return i; }
Objectives 56 Explain reasons for synchronization Describe busy waiting solutions Describe a semaphore and its use Describe a sonitor and its use
Agenda 57 Scheduling Policies Synchronization Deadlock
Objectives 58 Identify deadlocks Describe ways to prevent deadlocks Describe ways to detect deadlocks Describe ways to recover from deadlocks
Deadlock 59 Examples “It takes money to make money” You can’t get a job without experience; you can’t get experience without a job. Background The cause of deadlocks: Each process needing what another process has. This results from sharing resources such as memory, devices, links. Under normal operation, a resource allocation proceeds like this: Request a resource (suspend until available if necessary) Use the resource Release the resource
Conditions for Deadlock 60 Mutual Exclusion Each resource is assigned to a process or is available Hold and Wait Processes holding resources can request new resources No Preemption Resources cannot be taken from a process Circular Wait A set of processes are in a circular wait
Resource Allocation Graph 61 G = (V, E) V : Nodes consist of processes and resource types E : Edges are pairs of processes and resources Pi Pi Pi Rj Rj
Resource Allocation Graph 62 No cycle No deadlock Cycle If resource types have multiple instances, then deadlock MAY exist If each resource type has 1 instance, then deadlock has occurred R 3 Assigned to P 3 P 2 Requests P 3
Resource Allocation Graph 63
Handling Deadlocks 64 Ignore Deadlocks Ensure deadlock never occurs Prevention Avoidance Allow deadlock to happen Detection Recovery
Deadlock Prevention 65 Mutual Exclusion Not really possible Hold and Wait Collect all resources before execution Only request resource when no others are being held No Preemption Release any resource being held if the process can’t get additional resource Allow preemption Circular Wait Number resources and only request in ascending order
Deadlock Avoidance 66 Possible States Deadlock: No forward progress can be made Unsafe: A state that may cause deadlock Safe: A state is safe if a sequence of processes exist such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish. If a request allocation would cause an unsafe state, do not honor that request.
Deadlock Detection 67 Resource Allocation Graph Wait-for Graph
Deadlock Recovery 68 PROCESS TERMINATION: Could delete all the processes in the deadlock -- this is expensive. Delete one at a time until deadlock is broken ( time consuming ). Select who to terminate based on priority, time executed, time to completion, needs for completion, or depth of rollback In general, it's easier to preempt the resource, than to terminate the process. RESOURCE PREEMPTION: Select a victim - which process and which resource to preempt. Rollback to previously defined "safe" state. Prevent one process from always being the one preempted ( starvation ).


