Operating Systems Lecture 4 CPU Scheduling Deadlock 1

  • Slides: 43
Download presentation
Operating Systems Lecture 4 CPU Scheduling & Deadlock 1

Operating Systems Lecture 4 CPU Scheduling & Deadlock 1

Process Management n n n Concept of a Process Context-Change Process Life Cycle Process

Process Management n n n Concept of a Process Context-Change Process Life Cycle Process Creation Process Spawning OS 1 - Lecture 2 – Scheduling – Paul Flynn 2

Process State Diagrams n Three State Model u Ready u Running u Blocked n

Process State Diagrams n Three State Model u Ready u Running u Blocked n Five State Model u Ready u Running u Blocked u Ready Suspended u Blocked Suspended OS 1 - Lecture 2 – Scheduling – Paul Flynn 3

CPU Scheduling n n Scheduling the processor among all ready processes The goal is

CPU Scheduling n n Scheduling the processor among all ready processes The goal is to achieve: u High processor utilization u High throughput F number u Low of processes completed per of unit time response time F time elapsed from the submission of a request until the first response is produced OS 1 - Lecture 2 – Scheduling – Paul Flynn 4

Classification of Scheduling Activity n n n Long-term: which process to admit? Medium-term: which

Classification of Scheduling Activity n n n Long-term: which process to admit? Medium-term: which process to swap in or out? Short-term: which ready process to execute next? OS 1 - Lecture 2 – Scheduling – Paul Flynn 5

Queuing Diagram for Scheduling OS 1 - Lecture 2 – Scheduling – Paul Flynn

Queuing Diagram for Scheduling OS 1 - Lecture 2 – Scheduling – Paul Flynn 6

Long-Term Scheduling n n n Determines which programs are admitted to the system for

Long-Term Scheduling n n n Determines which programs are admitted to the system for processing Controls the degree of multiprogramming Attempts to keep a balanced mix of processor-bound and I/O-bound processes u CPU usage u System performance OS 1 - Lecture 2 – Scheduling – Paul Flynn 7

Medium-Term Scheduling n Makes swapping decisions based on the current degree of multiprogramming u

Medium-Term Scheduling n Makes swapping decisions based on the current degree of multiprogramming u Controls which remains resident in memory and which jobs must be swapped out to reduce degree of multiprogramming OS 1 - Lecture 2 – Scheduling – Paul Flynn 8

Short-Term Scheduling n Selects from among ready processes in memory which one is to

Short-Term Scheduling n Selects from among ready processes in memory which one is to execute next u The n selected process is allocated the CPU It is invoked on events that may lead to choose another process for execution: u. Clock interrupts u. I/O interrupts u. Operating system calls and traps u. Signals OS 1 - Lecture 2 – Scheduling – Paul Flynn 9

Characterization of Scheduling Policies The selection function determines which ready process is selected next

Characterization of Scheduling Policies The selection function determines which ready process is selected next for execution n The decision mode specifies the instants in time the selection function is exercised u Nonpreemptive F Once a process is in the running state, it will continue until it terminates or blocks for an I/O u Preemptive F Currently running process may be interrupted and moved to the Ready state by the OS F Prevents one process from monopolizing the processor OS 1 - Lecture 2 – Scheduling – Paul Flynn n 10

Short-Term Scheduler Dispatcher n n The dispatcher is the module that gives control of

Short-Term Scheduler Dispatcher n n The dispatcher is the module that gives control of the CPU to the process selected by the short-term scheduler The functions of the dispatcher include: u Switching context u Switching to user mode u Jumping to the location in the user program to restart execution n The dispatch latency must be minimal OS 2 - Lecture 2 – Scheduling – Paul Flynn 11

The CPU-I/O Cycle n n Processes require alternate use of processor and I/O in

The CPU-I/O Cycle n n Processes require alternate use of processor and I/O in a repetitive fashion Each cycle consist of a CPU burst followed by an I/O burst u. A n process terminates on a CPU burst CPU-bound processes have longer CPU bursts than I/O-bound processes OS 1 - Lecture 2 – Scheduling – Paul Flynn 12

Short-Term Scheduling Criteria n n User-oriented criteria u Response Time: Elapsed time between the

Short-Term Scheduling Criteria n n User-oriented criteria u Response Time: Elapsed time between the submission of a request and the receipt of a response u Turnaround Time: Elapsed time between the submission of a process to its completion System-oriented criteria u Processor utilization u Throughput: number of process completed per unit time u fairness OS 1 - Lecture 2 – Scheduling – Paul Flynn 13

Scheduling Algorithms n n First-Come, First-Served Scheduling Shortest-Job-First Scheduling u Also n n referred

Scheduling Algorithms n n First-Come, First-Served Scheduling Shortest-Job-First Scheduling u Also n n referred to as Shortest Job Next Highest Response Ratio Next (HRN) Shortest Remaining Time (SRT) Round-Robin Scheduling Multilevel Feedback Queue Scheduling OS 1 - Lecture 2 – Scheduling – Paul Flynn 14

Process Mix Example Process Arrival Time Service Time 1 0 3 2 2 6

Process Mix Example Process Arrival Time Service Time 1 0 3 2 2 6 3 4 4 4 6 5 5 8 2 Service time = total processor time needed in one (CPU-I/O) cycle Jobs with long service time are CPU-bound jobs and are referred to as “long jobs” OS 1 - Lecture 2 – Scheduling – Paul Flynn 15

First Come First Served (FCFS) n n Selection function: the process that has been

First Come First Served (FCFS) n n Selection function: the process that has been waiting the longest in the ready queue (hence, FCFS) Decision mode: non-preemptive ua process runs until it blocks for an I/O OS 1 - Lecture 2 – Scheduling – Paul Flynn 16

FCFS drawbacks n Favors CPU-bound processes u A CPU-bound process monopolizes the processor u

FCFS drawbacks n Favors CPU-bound processes u A CPU-bound process monopolizes the processor u I/O-bound processes have to wait until completion of CPU-bound process F I/O-bound processes may have to wait even after their I/Os are completed (poor device utilization) u Better I/O device utilization could be achieved if I/O bound processes had higher priority OS 1 - Lecture 2 – Scheduling – Paul Flynn 17

Shortest Job First (Shortest Process Next) n n n Selection function: the process with

Shortest Job First (Shortest Process Next) n n n Selection function: the process with the shortest expected CPU burst time u I/O-bound processes will be selected first Decision mode: non-preemptive The required processing time, i. e. , the CPU burst time, must be estimated for each process OS 1 - Lecture 2 – Scheduling – Paul Flynn 18

SJF / SPN Critique n n n Possibility of starvation for longer processes Lack

SJF / SPN Critique n n n Possibility of starvation for longer processes Lack of preemption is not suitable in a time sharing environment SJF/SPN implicitly incorporates priorities u Shortest jobs are given preferences u CPU bound process have lower priority, but a process doing no I/O could still monopolize the CPU if it is the first to enter the system OS 1 - Lecture 2 – Scheduling – Paul Flynn 19

Highest Response Ratio Next (HRN) n n n Based on SJF with formula introduced

Highest Response Ratio Next (HRN) n n n Based on SJF with formula introduced Priority Based - P Time Waiting + Run Time / Run Time = P The process with the HIGHEST Priority will be selected for running Non-Preemptive Reduces SJF bias against Short Jobs OS 1 - Lecture 2 – Scheduling – Paul Flynn 20

Priorities n n Implemented by having multiple ready queues to represent each level of

Priorities n n Implemented by having multiple ready queues to represent each level of priority Scheduler the process of a higher priority over one of lower priority Lower-priority may suffer starvation To alleviate starvation allow dynamic priorities u The priority of a process changes based on its age or execution history OS 1 - Lecture 2 – Scheduling – Paul Flynn 21

Round-Robin n n Selection function: same as FCFS Decision mode: preemptive a process is

Round-Robin n n Selection function: same as FCFS Decision mode: preemptive a process is allowed to run until the time slice period (quantum, typically from 10 to 100 ms) has expired u a clock interrupt occurs and the running process is put on the ready queue u OS 1 - Lecture 2 – Scheduling – Paul Flynn 22

RR Time Quantum n n Quantum must be substantially larger than the time required

RR Time Quantum n n Quantum must be substantially larger than the time required to handle the clock interrupt and dispatching Quantum should be larger then the typical interaction u but not much larger, to avoid penalizing I/O bound processes OS 1 - Lecture 2 – Scheduling – Paul Flynn 23

Round Robin: critique n Still favors CPU-bound processes u An I/O bound process uses

Round Robin: critique n Still favors CPU-bound processes u An I/O bound process uses the CPU for a time less than the time quantum before it is blocked waiting for an I/O u A CPU-bound process runs for all its time slice and is put back into the ready queue F May unfairly get in front of blocked processes OS 1 - Lecture 2 – Scheduling – Paul Flynn 24

Multilevel Feedback Scheduling n n n Preemptive scheduling with dynamic priorities N ready to

Multilevel Feedback Scheduling n n n Preemptive scheduling with dynamic priorities N ready to execute queues with decreasing priorities: Dispatcher selects a process for execution from RQi only if RQi-1 to RQ 0 are empty OS 1 - Lecture 2 – Scheduling – Paul Flynn 25

Multilevel Feedback Scheduling n n n New process are placed in RQ 0 After

Multilevel Feedback Scheduling n n n New process are placed in RQ 0 After the first quantum, they are moved to RQ 1 after the first quantum, and to RQ 2 after the second quantum, … and to RQN after the Nth quantum I/O-bound processes remain in higher priority queues. u CPU-bound jobs drift downward. u Hence, long jobs may starve OS 1 - Lecture 2 – Scheduling – Paul Flynn 26

Multiple Feedback Queues Different RQs may have different quantum values OS 1 - Lecture

Multiple Feedback Queues Different RQs may have different quantum values OS 1 - Lecture 2 – Scheduling – Paul Flynn 27

Algorithm Comparison n n Which one is the best? The answer depends on many

Algorithm Comparison n n Which one is the best? The answer depends on many factors: u the system workload (extremely variable) u hardware support for the dispatcher u relative importance of performance criteria (response time, CPU utilization, throughput. . . ) u The evaluation method used (each has its limitations. . . ) OS 1 - Lecture 2 – Scheduling – Paul Flynn 28

Deadlocks 3. 1. Resource 3. 2. Introduction to deadlocks 3. 3. The ostrich algorithm

Deadlocks 3. 1. Resource 3. 2. Introduction to deadlocks 3. 3. The ostrich algorithm 3. 4. Deadlock detection and recovery 3. 5. Deadlock avoidance 3. 6. Deadlock prevention 3. 7. Other issues 29

Deadlock Recap n n Process is deadlocked if it is waiting for an event

Deadlock Recap n n Process is deadlocked if it is waiting for an event that will never occur Most common situation is where two processes are involved on is holding resource required by the other and also looking for resource held by the other process. 30

Deadlock Example n Airline booking example. Alan wants to books seat on flight AB

Deadlock Example n Airline booking example. Alan wants to books seat on flight AB 123 so locks this file. Same time Brian wants to book flight on AB 456 and locks this file. Alan wants to book return flight on AB 456 and Brian wants to book return flight on AB 123. No each user as a file locked and is requesting a file locked by the other. 31

Deadlock Example cont n n Alan locks AB 123 Brian lock AB 456 Alan

Deadlock Example cont n n Alan locks AB 123 Brian lock AB 456 Alan requests AB 456 Brian requests AB 123 32

Conditions for deadlock n There are 4 conditions necessary for deadlock u Mutual exclusion

Conditions for deadlock n There are 4 conditions necessary for deadlock u Mutual exclusion u Resource holding u No premption u Circular wait 33

Conditions explained n n Mutual Exclusion only one process can use a resource at

Conditions explained n n Mutual Exclusion only one process can use a resource at a time Resource holding process can hold a resource while requesting another No premption resources cannot be forcibly removed from a process Circular wait closed circle exists where each process is holding a resource required by another. 34

Dealing with deadlock n Three ways of dealing with deadlocks u Deadlock prevention u

Dealing with deadlock n Three ways of dealing with deadlocks u Deadlock prevention u Deadlock avoidance u Deadlock detection 35

Deadlock prevention n Prevent any one of the 4 conditions from occuring will prevent

Deadlock prevention n Prevent any one of the 4 conditions from occuring will prevent deadlock Mutual exclusion – cannot prevent Resource holding – to prevent this a process must be allocated all it resources at once called one shot allocation very inefficient. Process may have to wait for resources it might not need. Process may hold resources for long time without using. 36

Deadlock prevention cont n No premption to deny this condition two ways u If

Deadlock prevention cont n No premption to deny this condition two ways u If a process is holding a resource requests another it could be forces to give up the resource it is holding. u A resources required by a process and held by a second could be forcibly removed from the second. Not possible with serially reusable resources. 37

Deadlock prevention n Circular wait – this condition can be prevented if resources are

Deadlock prevention n Circular wait – this condition can be prevented if resources are organsied in a particual order and require that resources follow an order. 38

Deadlock avoidance n n n Deadlock prevention means that deadlock will not occur due

Deadlock avoidance n n n Deadlock prevention means that deadlock will not occur due to fact that we deny one of the 4 conditions necessary. Innefficient Deadlock avoidance attempts to predict the possibility of deadlock as each resources request is made. Example if process A requests a resource held by process B then make sure that process B is not waiting for resource held by A 39

Bankers Algorithm n n The most common method of deadlock avoidance is to use

Bankers Algorithm n n The most common method of deadlock avoidance is to use the bankers algorithm. Uses banking anology, banker will only grant loan if he can meet the needs of customers based on their projected future loan requirements. Example three processes P 1, P 2 & P 3 and 10 resources available. Table shows requirements 40

Example cont. Process Max need Current usage n P 1 P 2 P 3

Example cont. Process Max need Current usage n P 1 P 2 P 3 8 5 8 3 1 2 Total maximum needs =21 means that allocation cannot be met at one time. We need a sequence of allocations which will allow all processes to finish. If we start with P 2 when it is finished it will release 5 resources. Next if we allow P 1 to run it will release 8 resources and so P 3 will be able to finish. 41

Problems with deadlock avoidance n n Each process has to pre-declare its maximum rersource

Problems with deadlock avoidance n n Each process has to pre-declare its maximum rersource requirements. This is not realistic for interactive systems. The avoidance algorithm must be executed every time a resource request is made. For a multi-user system with a large number of user processes, the processing overhead would be severe. 42

Deadlock detection n n A deadlock detection strategy accepts the risk of deadlock occuring

Deadlock detection n n A deadlock detection strategy accepts the risk of deadlock occuring and periodically executes a procedure to detect any in place. Breaking a deadlock implies that a process must be aborted or resources prempted from processes, either could result in loss of work. 43