1 / 68

Operating Systems CS3013 / CS502

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

calida
Download Presentation

Operating Systems CS3013 / CS502

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating SystemsCS3013 / CS502 WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS

  2. Agenda • Scheduling Policies • Synchronization • Deadlock

  3. Objectives • Identify Scheduling Criteria • Explain Different Scheduling Policies • Describe Measurements for Evaluation • Give Examples of Evaluation Methods

  4. CPU Burst Duration

  5. 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

  6. Scheduling Criteria • What should we consider in developing a scheduling policy?

  7. 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

  8. 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.

  9. 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

  10. 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)

  11. First-Come-First-Serve (FCFS) B A 0 5 12 15 C

  12. Shortest Process/Job First B A 0 3 8 15 C

  13. 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

  14. Priority Scheduling B A 0 7 10 15 C

  15. 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

  16. Round Robin A B C A B C A B C A B A B B B 0 9 12 15

  17. Round Robin • Play with Quantum • q = 1 • q = 2 • q = 5

  18. Round Robin • Avg. Turnaround Time • Quantum from 1 to 5 • How does it change?

  19. Exercise • Gantt Chart • FCFS • SJF • Priority • RR (q = 1) • Performance • Throughput • Waiting time • Turnaround time

  20. Exercise • Turnaround time • FCFS • SJF (Preemptive vs. Non-preemptive) • RR q= 1 • RR q = 0.5

  21. 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

  22. 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

  23. Objectives • Identify Scheduling Criteria • Explain Different Scheduling Policies • Describe Measurements for Evaluation • Give Examples of Evaluation Methods

  24. Agenda • Scheduling Policies • Synchronization • Deadlock

  25. Objectives • Explain reasons for synchronization • Describe busy waiting solutions • Describe a semaphore and its use • Describe a sonitor and its use

  26. Why synchronization?

  27. 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

  28. 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);

  29. 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 */

  30. 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; }

  31. 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; }

  32. Producer Consumer Trouble!

  33. 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)

  34. 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; … }

  35. 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; … }

  36. 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; … }

  37. 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; … }

  38. 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; … }

  39. 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.

  40. Synchronization Hardware • Test-and-Set: returns and modifies atomically int Test_and_Set(int &target) { int temp; temp = target; target = true; return temp; }

  41. Using Test_and_Set • All solutions so far requires “busy waiting”. while (1) { while (Test_and_Set(lock)) ; // critical section lock = false; // remainder section }

  42. Semaphore

  43. 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)

  44. Critical Section with Semaphore semaphore mutex; /* shared */ while (1) { wait(mutex); // critical section signal(mutex); // remainder section }

  45. Classical Synchronization Problems • Bounded Buffers • Readers/Writers • Dining Philosophers

  46. 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

  47. Readers/Writers • Shared • Writer semaphore mutex, wrt; int readcount; wait(wrt); // write stuff signal(wrt)

  48. 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);

  49. Dining Philosophers • Philosophers • Think • Eat • Need 2 chopsticks to eat

  50. Dining Philosophers • Philosopher i while (1) { // think wait(chopstick[i]); wait(chopstick[i+1 % 5]); // eat signal(chopstick[i]); signal(chopstick[i+1 % 5]); }

More Related