Chapter 5 Processor Scheduling 5 1 Introduction Processor

  • Slides: 28
Download presentation
Chapter 5 Processor Scheduling

Chapter 5 Processor Scheduling

5. 1 Introduction • Processor (CPU) scheduling is the sharing of the processor(s) among

5. 1 Introduction • Processor (CPU) scheduling is the sharing of the processor(s) among the processes in the ready queue • The critical activities are: – the ordering of the allocation and de-allocation of the CPU to the various processes and threads, one at a time – deciding when to de-allocate and allocate the CPU from a process to another process 2

Scheduling and Performance These activities must be carried out in such a way as

Scheduling and Performance These activities must be carried out in such a way as to meet the performance objectives of the system • Scheduling algorithms: FCFS, SJF, RR, SRT 3

5. 2 Types of Schedulers • Long-term scheduler (memory allocation) – Determines which processes

5. 2 Types of Schedulers • Long-term scheduler (memory allocation) – Determines which processes are loaded into memory – Controls the degree of multiprogramming • Medium-term scheduler – Suspends (swaps out) and resumes (swaps in) processes • Short-term scheduler (processor scheduling) – Selects one of the processes that are ready and allocates the CPU to it. 4

Medium-Term Scheduler The basic idea is that some of the processes can be removed

Medium-Term Scheduler The basic idea is that some of the processes can be removed from memory (to disk) and thus reduce the degree of multiprogramming. this is called swapping 5

5. 3 Processor Scheduling • A CPU scheduling policy defines the order in which

5. 3 Processor Scheduling • A CPU scheduling policy defines the order in which processes are selected from the ready queue for CPU processing. • The scheduling mechanism decides when and how to carry out the context switch to the selected process, i. e. , the de-allocation of the CPU from the current process and allocation of the CPU to the selected process. 6

Processor Scheduling (2) • The scheduler selects the next process to execute from among

Processor Scheduling (2) • The scheduler selects the next process to execute from among several processes waiting in the ready queue. • The dispatcher allocates the CPU to the selected process at the appropriate time. 7

Implementing the Process Abstraction Pi CPU Pj CPU Pi Executable Memory Pj Executable Memory

Implementing the Process Abstraction Pi CPU Pj CPU Pi Executable Memory Pj Executable Memory Pk CPU … Pk Executable Memory OS Address Space CPU ALU Control Unit Pi Address Space Pk Address Space … Pj Address Space Machine Executable Memory OS interface

Process Activities Every process that request CPU service, carries out the following sequence of

Process Activities Every process that request CPU service, carries out the following sequence of actions: 1. Join the ready queue and wait for CPU service. 2. Execute (receive CPU) for the duration of the current CPU burst or for the duration of the time slice (timeout). 3. Join the I/O queue to wait for I/O service or return to the ready queue to wait for more CPU service. 4. Terminate and exit if service is completed, i. e. , there are no more CPU or I/O bursts. If more service is required, return to the ready queue to wait for more CPU service. 9

Simple Model for processor Scheduling 10

Simple Model for processor Scheduling 10

5. 3. 1 CPU Scheduler • Insertion of processes that request CPU service into

5. 3. 1 CPU Scheduler • Insertion of processes that request CPU service into the ready queue. This queue is usually a data structure that represents a simple first-in-first-out (FIFO) list, a set of simple lists, or as a priority list. This function is provided by the enqueuer, a component of the scheduler. 11

Scheduler (2) • The occurrence of a context switch, carried by the context switcher

Scheduler (2) • The occurrence of a context switch, carried by the context switcher that saves the context of the current process and de-allocates the CPU from that process. • The selection of the next process from the ready queue and loading its context. This can be carried out by the dispatcher, which then allocates the CPU to the newly selected process. 12

Occurrence of a Context Switch A context switch can occur at any of the

Occurrence of a Context Switch A context switch can occur at any of the following possible times: • The executing process has completed its current CPU burst. This is the normal case in simple batch systems. • The executing process is interrupted by the operating system because its allocated time (time slice) has expired. This is a normal case in time-sharing systems. • The executing process is interrupted by the operating system because a higher priority process has arrived requesting CPU service. 13

Process Queues The processes that are ready and waiting to execute are kept on

Process Queues The processes that are ready and waiting to execute are kept on a list called the ready queue. A similar list exists for each I/O device, called the device queue. 14

5. 3. 2 Multiple Classes of Processes Single-class system: If the OS treats all

5. 3. 2 Multiple Classes of Processes Single-class system: If the OS treats all processes in the same manner, it is referred as to a single-class system. fair Multiclass system: A system with different groups of processes, each group is assigned a priority depending on some criteria. 15

Scheduling with Multiple Queues 16

Scheduling with Multiple Queues 16

Scheduling of Multi-Class Systems • Not Fair – Processes are not treated alike, as

Scheduling of Multi-Class Systems • Not Fair – Processes are not treated alike, as preference is given to higher-priority processes • A major problem in multi-class systems is STARVATION – indefinite waiting, one or more low-priority processes may never execute – solution is AGING 17

5. 4 CPU Scheduling Policies Categories of scheduling policies: • Non-Preemptive -- no interruptions

5. 4 CPU Scheduling Policies Categories of scheduling policies: • Non-Preemptive -- no interruptions are allowed. A process completes execution of its CPU burst • Preemptive – a process can be interrupted before the process completes its CPU burst 18

Priorities and Scheduling • Priorities can be used with either preemptive or non-preemptive scheduling.

Priorities and Scheduling • Priorities can be used with either preemptive or non-preemptive scheduling. • Depending on the goals of an operating system, one or more of various scheduling policies can be used; each will result in a different system performance. 19

Criteria for Scheduling Algorithms • CPU utilization • Throughput • Turnaround time • Waiting

Criteria for Scheduling Algorithms • CPU utilization • Throughput • Turnaround time • Waiting time • Response time • Fairness 20

CPU-IO Bursts of Processes An important property of a process is its CPU-IO burst

CPU-IO Bursts of Processes An important property of a process is its CPU-IO burst • An I/O bound process has many short CPU burst • A CPU bound process has few long CPU bursts • The OS tries to maintain a balance of these two types of processes 21

CPU Scheduling Policies • • • 22 First-come-first-served (FCFS) Shortest job first (Shortest process

CPU Scheduling Policies • • • 22 First-come-first-served (FCFS) Shortest job first (Shortest process next) Longest job first Priority scheduling Round robin (RR) Shortest remaining time (SRT) also known as shortest remaining time first (SRTF)

5. 4. 1 FCFS Scheduling First come first served (FCFS) scheduling algorithm, a non-preemptive

5. 4. 1 FCFS Scheduling First come first served (FCFS) scheduling algorithm, a non-preemptive policy – The order of service is the same order of arrivals – Managed with FIFO queue – Simple to understand implement – Scheduling is FAIR – The performance of this scheme is relatively poor 23

FCFS Scheduling (cont’d) Example: Five processes arrive at time 0, in the order: P

FCFS Scheduling (cont’d) Example: Five processes arrive at time 0, in the order: P 1, P 2, P 3, P 4, P 5. Their CPU burst time are shown in the following table. Using FCFS algorithm, find the average turnaround time, average waiting time, throughput, and CPU utilization rate. 24 Process CPU burst (Unit: ms) P 1 135 P 2 102 P 3 56 P 4 148 P 5 125

FCFS Scheduling (cont’d) Solution: The Gantt chart for FCFS The average waiting time is:

FCFS Scheduling (cont’d) Solution: The Gantt chart for FCFS The average waiting time is: (0 + 135 + 237 + 293 + 441)/ 5 = 221. 2 msec Throughput: 5 Turnaround Time (unit: msec): T(p 1)=135, T(p 2)=135+102=237, CPU utilization: 100% T(p 3)=135+102+56=293, Throughput: 5 T(p 4)=135+102+56+148=441, CPU utilization: 100% T(p 5)=135+102+56+148+125=566 The average turnaround time: (135 + 237 + 293 + 441+566) / 5 = 334. 4 msec 25

5. 4. 2 SJF (SPN) Scheduling • The scheduler selects the next the process

5. 4. 2 SJF (SPN) Scheduling • The scheduler selects the next the process with the shortest CPU burst • Basically a non-preemptive policy • SJF is optimal - gives the minimum average waiting time for a given set of processes. 26

SJF Scheduling (cont’d) Example: Five processes arrive at time 0, in the order: P

SJF Scheduling (cont’d) Example: Five processes arrive at time 0, in the order: P 1, P 2, P 3, P 4, P 5. Their CPU burst time are shown in the following table. Using SJF algorithm, find the average turnaround time, and average waiting time. 27 Process CPU burst (Unit: ms) P 1 135 P 2 102 P 3 56 P 4 148 P 5 125

Solution: Gantt chart for SJF: Waiting Time: Tw(p 3)=0, Tw (p 2)=56, Tw (p

Solution: Gantt chart for SJF: Waiting Time: Tw(p 3)=0, Tw (p 2)=56, Tw (p 5) =158, Tw (p 1)=283, Tw (p 4)=418 Average Waiting Time = (0+56+158+283+418)/5=183 msec P 3 P 4 Time: P 1 P 5 P 2 Turnaround Ta(p 3)=56, Ta (p 2)=56+102=158, Ta (p 5) =56+102+125=283 Ta (p 1)=56+102+125+135=418 Ta (p 4)=56+102+125+135+148=566 Average Turnaround Time = (56+158+283+418+566)/5=296 msec 28