680 likes | 772 Views
Operating Systems CS3013 / CS502. WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS. Agenda. Scheduling Policies Synchronization Deadlock. Objectives. Identify Scheduling Criteria Explain Different Scheduling Policies Describe Measurements for Evaluation
E N D
Operating SystemsCS3013 / CS502 WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS
Agenda • Scheduling Policies • Synchronization • Deadlock
Objectives • Identify Scheduling Criteria • Explain Different Scheduling Policies • Describe Measurements for Evaluation • Give Examples of Evaluation Methods
Scheduler • Invoked whenever the operating system must select a user-level process to execute • After process creation/termination • A process blocks on I/O • I/O interrupt occurs • Clock interrupt occurs • Type of processes • Interactive jobs • CPU bound jobs • Somewhere inbetween
Scheduling Criteria • What should we consider in developing a scheduling policy?
Scheduling Criteria • 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 • 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 • 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 • 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) B A 0 5 12 15 C
Shortest Process/Job First B A 0 3 8 15 C
Exponential Average • b(t+1) = a b(t) + (1-a) b(t) • a=0 • Current value has no effect • a=1 • Only current value matters • b(0) = system average, a=0.5
Priority Scheduling B A 0 7 10 15 C
Priority Scheduling Issues • 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 A B C A B C A B C A B A B B B 0 9 12 15
Round Robin • Play with Quantum • q = 1 • q = 2 • q = 5
Round Robin • Avg. Turnaround Time • Quantum from 1 to 5 • How does it change?
Exercise • Gantt Chart • FCFS • SJF • Priority • RR (q = 1) • Performance • Throughput • Waiting time • Turnaround time
Exercise • Turnaround time • FCFS • SJF (Preemptive vs. Non-preemptive) • RR q= 1 • RR q = 0.5
Multilevel Queues • 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 • 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 • Identify Scheduling Criteria • Explain Different Scheduling Policies • Describe Measurements for Evaluation • Give Examples of Evaluation Methods
Agenda • Scheduling Policies • Synchronization • Deadlock
Objectives • Explain reasons for synchronization • Describe busy waiting solutions • Describe a semaphore and its use • Describe a sonitor and its use
Cooperating Processes • Consider a printer spooler • Enter a file name into the spooler queue • Printer daemon checks the queue and prints the file. • “Race Conditions” A B 9 Free letter hw1 proj1 lecture1 5 6 7 8 9
Race Condition Demo • 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, "%d\n", num);
Producer / Consumer • 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 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 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; }
Critical Section • 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 • Shared variable turn is initialized to either A or B and both processes start execution concurrently. Process A Process B … While (TRUE) { … while (turn == B) ; // critical section turn = B; … } … While (TRUE) { … while (turn == A) ; // critical section turn = A; … }
Second Try • Shared variables pAinside and pBinside are initialized to FALSE. Process A Process B … While (TRUE) { … while (pBinside) ; pAinside = TRUE; // critical section pAinside = FALSE; … } … While (TRUE) { … while (pAinside) ; pBinside = TRUE; // critical section pBinside = FALSE; … }
Third Try • Shared variables pAtrying and pBtrying are initialized to FALSE. Process A Process B … While (TRUE) { … pAtrying = TRUE; while (pBtrying) ; // critical section pAtrying = FALSE; … } … While (TRUE) { … pBtrying = TRUE; while (pAtrying) ; // critical section pBtrying = FALSE; … }
Dekker’s Algorithm Process A Process B • turn needs to be initialized to A or B. … While (TRUE) { … pAtrying = TRUE; while (pBtrying) if (turn == B) { pAtrying = FALSE; while (turn == B) ; pAtrying = TRUE; } // critical section turn = B; pAtrying = FALSE; … } … While (TRUE) { … pBtrying = TRUE; while (pAtrying) if (turn == A) { pBtrying = FALSE; while (turn == A) ; pBtrying = TRUE; } // critical section turn = A; pBtrying = FALSE; … }
Peterson’s Algorithm Process A Process B • turn does not need any initialization. … While (TRUE) { … pAtrying = TRUE; turn = B; while (pBtrying && turn == B); // critical section pAtrying = FALSE; … } … While (TRUE) { … pBtrying = TRUE; turn = A; while (pAtrying && turn == A); // critical section pBtrying = FALSE; … }
Dekker’s and Peterson’s Algorithm • 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 • 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 • All solutions so far requires “busy waiting”. while (1) { while (Test_and_Set(lock)) ; // critical section lock = false; // remainder section }
Semaphore • Do not require “busy waiting” • Semaphore S (shared, often initially = 1) • Integer variable • Accessed via two (indivisible) atomic operations wait(S) : S = S – 1 if (S < 0) then block(S) signal(S) : S = S + 1; if (S <= 0) then wakeup(S)
Critical Section with Semaphore semaphore mutex; /* shared */ while (1) { wait(mutex); // critical section signal(mutex); // remainder section }
Classical Synchronization Problems • Bounded Buffers • Readers/Writers • Dining Philosophers
Readers/Writers • 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 • Shared • Writer semaphore mutex, wrt; int readcount; wait(wrt); // write stuff signal(wrt)
Readers-Writers • 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 • Philosophers • Think • Eat • Need 2 chopsticks to eat
Dining Philosophers • Philosopher i while (1) { // think wait(chopstick[i]); wait(chopstick[i+1 % 5]); // eat signal(chopstick[i]); signal(chopstick[i+1 % 5]); }