Unit-II: CPU Scheduling

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 69

Unit-II: CPU Scheduling

CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling

Operating System Concepts 7th Edition, Feb 2, 2005

5.2

Silberschatz, Galvin and Gagne 2005

Scheduling Criteria
CPU utilization keep the CPU as busy as possible Throughput Number of processes that complete their

execution per time unit


Turnaround time amount of time to execute a particular

process
Waiting time amount of time a process has been waiting

in the ready queue


Response time amount of time it takes from when a

request was submitted until the first response is produced, not output (for time-sharing environment)

Operating System Concepts 7th Edition, Feb 2, 2005

5.3

Silberschatz, Galvin and Gagne 2005

Optimization Criteria
Max CPU utilization

Max throughput
Min turnaround time Min waiting time Min response time

Operating System Concepts 7th Edition, Feb 2, 2005

5.4

Silberschatz, Galvin and Gagne 2005

Scheduling
Preemptive A Process which can be terminated. Non-Preemptive A Process which cannot terminated. Algorithms:

First Come First Serve(FCFS) (Non- Preemptive)


Shortest Job first(SJFS) Preemptive and Non-

preemptive

Priority - Preemptive and Non-preemptive Round Robin Preemptive FCFS


5.5 Silberschatz, Galvin and Gagne 2005

Operating System Concepts 7th Edition, Feb 2, 2005

First-Come, First-Served (FCFS) Scheduling


Process P1 P2 P3 The Gantt Chart for the schedule is: P1 0 24 Burst Time 24 3 3

Suppose that the processes arrive in the order: P1 , P2 , P3

P2 27

P3 30

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts 7th Edition, Feb 2, 2005

5.6

Silberschatz, Galvin and Gagne 2005

First-Come, First-Served (FCFS) Scheduling


Process P1 P2 P3 WT 0 24 27 BT 24 3 3 TT 24 27 30 81

Turn around time for P1 = 24; P2 = 27; P3 = 30 Average Turn around time: (24 + 27+30)/3 = 81/3 = 27ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.7

Silberschatz, Galvin and Gagne 2005

FCFS Scheduling (Cont.)


Suppose that the processes arrive in the order

P2 , P3 , P1
The Gantt chart for the schedule is:

P2 0 3

P3 6

P1 30

Waiting time for P1 = 6 ; P2 = 0 ; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3

Much better than previous case


Convoy effect short process behind long process

Operating System Concepts 7th Edition, Feb 2, 2005

5.8

Silberschatz, Galvin and Gagne 2005

First-Come, First-Served (FCFS) Scheduling


Process P1 P2 P3 WT 6 0 3 BT 24 3 3 TT 30 3 6 39

Turn around time for P1 = 30; P2 = 3; P3 = 6 Average Turn around time: (30 + 3+6)/3 = 39/3 = 13ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.9

Silberschatz, Galvin and Gagne 2005

Shortest-Job-First (SJF) Scheduling


Associate with each process the length of its next CPU burst. Use

these lengths to schedule the process with the shortest time


Two schemes:

nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)

SJF is optimal gives minimum average waiting time for a given

set of processes

Operating System Concepts 7th Edition, Feb 2, 2005

5.10

Silberschatz, Galvin and Gagne 2005

Example of Non-Preemptive SJF


Process Burst Time

P1
P2 P3 P4
SJF (non-preemptive)

6
8 7 3

P4 0 3

P1 9

P3 16

P2 24

Average waiting time = (3 + 16 + 9 + 0)/4 = 7Ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.11

Silberschatz, Galvin and Gagne 2005

Example of Non-Preemptive SJF


Process P1 P2 P3 WT 3 16 9 BT 6 8 7 TT 9 24 16

P4

3 52

Turn around time for P1 = 9; P2 = 24; P3 = 16 ; p4 = 3 Average Turn around time: (9 + 24 +16 + 3)/4 = 52/4 = 13ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.12

Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJF


Process Arrival Time Burst Time

P1
P2 P3 P4
SJF (preemptive)

0.0
2.0 4.0 5.0

7
4 1 4

2 P1 0 After 2ms of Time period p2 enters Compare the RT of p1= 5ms and BT of entering process p2 = 4ms. BT of p2 is less than the RT of p1 so p1 is preempted.

Operating System Concepts 7th Edition, Feb 2, 2005

5.13

Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJF


Process Arrival Time Burst Time

P1
P2 P3 P4
SJF (preemptive)

0.0
2.0 4.0 5.0

7
4 1 4

2 P1 0 2

2 P2

After 4ms of Time period p2 has executed for 2ms. The RT of

p2 = 2ms has to be compared with RT of p1= 5ms and BT of entering process p3 = 1ms. Since p3 BT is minimum compared to p1 and p2, p3 starts executing.
Operating System Concepts 7th Edition, Feb 2, 2005 5.14 Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJF


Process Arrival Time Burst Time

P1
P2 P3 P4
SJF (preemptive)

0.0
2.0 4.0 5.0

7
4 1 4

2 P1 0 2

2 P2 4

1 P3

P3 executes for 1ms i.e after 5ms of Time p4 enters by that time p3

would have completely been executed. Now we have p1,p2 & p4 in the ready queue with corresponding BT p1=5ms, p2=2ms & p4 =4ms.Now apply non-preemptive concept to execute the processes. P2 p4 p1
Operating System Concepts 7th Edition, Feb 2, 2005 5.15 Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJF


Process Arrival Time Burst Time

P1
P2 P3 P4
SJF (preemptive)

0.0
2.0 4.0 5.0

7
4 1 4

2 P1 0 2

2 P2 4

1 P3 5

2 P2

Operating System Concepts 7th Edition, Feb 2, 2005

5.16

Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJFS


Process Arrival Time Burst Time

P1 P2 P3 P4
SJF (preemptive)

0.0 2.0 4.0 5.0

7 4 1 4

2 P1 0 2

2 P2 4

1 P3 5

2 P2 7

4 P4

5 P1

11

AWT = ( (11-2) + (5-(2+2)) + (4-4) + (7-5) ) = 12/4 = 3 Ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.17

Silberschatz, Galvin and Gagne 2005

Example of Preemptive SJFS


Process
P1 P2 P3

Burst Time
7 4 1

WT
9 3 0

TT
16 7 1

p4
Total

2
14

6
30

2
P1 0 2

2 P2 4

1
P3 5

2 P2

4 P4

5 P1

11

AWT = ( (11-2) + (5-(2+2)) + (4-4) + (7-5) ) = 12/4 = 3 Ms


ATT = 30/4 = 7.5ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.18

Silberschatz, Galvin and Gagne 2005

Priority Scheduling
A priority number (integer) is associated with each process

The CPU is allocated to the process with the highest priority

(smallest integer highest priority)


Preemptive nonpreemptive

SJF is a priority scheduling where priority is the predicted next CPU

burst time
Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the

process

Operating System Concepts 7th Edition, Feb 2, 2005

5.19

Silberschatz, Galvin and Gagne 2005

Priority Scheduling
Problem Starvation low priority processes may never execute

Solution Aging as time progresses increase the priority of the

process. P1 100 P2 2 P3 1

Operating System Concepts 7th Edition, Feb 2, 2005

5.20

Silberschatz, Galvin and Gagne 2005

Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T0 P1 P2 2 P3

10

Operating System Concepts 7th Edition, Feb 2, 2005

5.21

Silberschatz, Galvin and Gagne 2005

Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T0 P1 P2 2 P3

10

2
4

1
2 P3 1

T1

P1 9

P2 1

Operating System Concepts 7th Edition, Feb 2, 2005

5.22

Silberschatz, Galvin and Gagne 2005

Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T1 P1 P2 1 2 P3

RT=2 T2 P1 8 P2 1

2 P3 1

Operating System Concepts 7th Edition, Feb 2, 2005

5.23

Silberschatz, Galvin and Gagne 2005

Example of Non-Preemptive Priority Scheduling


Process Burst Time Priority

P1
P2 P3 P4

10
1 2 1

3
1 4 5

P5
Priority (non-preemptive)

P2 0 1

P5 6

P1 16

P3

P4

18 19

Average waiting time = (6 + 0 + 16 + 18 + 1) / 5 = 41 / 5 = 8.2Ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.24

Silberschatz, Galvin and Gagne 2005

Example of Preemptive Priority


Process Arrival Time Burst Time Priority

P1
P2 P3 P4
priority (preemptive)

0.0
2.0 4.0 5.0

7
4 1 4

1
0.5 4 3

2 P1 0

Operating System Concepts 7th Edition, Feb 2, 2005

5.25

Silberschatz, Galvin and Gagne 2005

Example of Preemptive Priority


Process Arrival Time Burst Time Priority

P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2

0.0
2.0 4.0 5.0

7
4 1 4

1
0.5 4 3

Operating System Concepts 7th Edition, Feb 2, 2005

5.26

Silberschatz, Galvin and Gagne 2005

Example of Preemptive Priority


Process Arrival Time Burst Time Priority

P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2 6

0.0
2.0 4.0 5.0

7
4 1 4

1
0.5 4 3

5 P1

Operating System Concepts 7th Edition, Feb 2, 2005

5.27

Silberschatz, Galvin and Gagne 2005

Example of Preemptive Priority


Process Arrival Time Burst Time Priority

P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2 6

0.0
2.0 4.0 5.0

7
4 1 4

1
0.5 4 3

5 P1 11

4 P4 15

1 P3

P1 = 6 2 = 4ms P2 = 2 2 = 0ms

p4 = 11 5 = 6ms Avg WT = 21/4 = 5.2ms

P3 = 15 4 = 11ms
Operating System Concepts 7th Edition, Feb 2, 2005 5.28 Silberschatz, Galvin and Gagne 2005

Round Robin (RR)


Each process gets a small unit of CPU time (time quantum),

usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time

quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
Performance

q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high

Operating System Concepts 7th Edition, Feb 2, 2005

5.29

Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 4


Process P1 P2 P3
The Gantt chart is: 4 P1 0 P1 is preempted and p2 starts its execution. Pending BT of p1 = 4ms

Burst Time 8 3 3

Operating System Concepts 7th Edition, Feb 2, 2005

5.30

Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 4


Process P1 P2 P3
The Gantt chart is: 4 P1 0 4 3 P2

Burst Time 8 3 3

P2 is completely executed since its BT is less than the Time Quantum.

Operating System Concepts 7th Edition, Feb 2, 2005

5.31

Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 4


Process P1 P2 P3
The Gantt chart is: 4 P1 0 4 3 P2 7 3 P3

Burst Time 8 3 3

Operating System Concepts 7th Edition, Feb 2, 2005

5.32

Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 4


Process P1 P2 P3
The Gantt chart is: 4 P1 0 WT 4 3 P2 7 TT Avg WT = 17/3 = 5.66ms 3 P3 10 4 P1

Burst Time 8 3 3

P1 = 10 4 = 6 8+6 = 14 P2 = 4 3+4 = 7

P3 = 7 3+7 = 10 Avg TT = 31/3 = 10.33ms Typically, higher average turnaround than SJF, but better response
Operating System Concepts 7th Edition, Feb 2, 2005 5.33 Silberschatz, Galvin and Gagne 2005

Example of RR with Time Quantum = 20


Process P1 P2 P3 P4 The Gantt chart is: Burst Time 53 17 68 24

P1 0 20

P2 37

P3 57

P4 77

P1

P3 97 117

P4

P1

P3

P3

121 134 154 162

Typically, higher average turnaround than SJF, but better response

Operating System Concepts 7th Edition, Feb 2, 2005

5.34

Silberschatz, Galvin and Gagne 2005

Time Quantum and Context Switch Time

What will happen to RR scheduling if the Time Quantum is high when compared to all the process in the ready queue?
Operating System Concepts 7th Edition, Feb 2, 2005 5.35 Silberschatz, Galvin and Gagne 2005

Exercise
Process P1 P2 P3 P4 P5 Burst Time 10 1 2 1 5 Priority 3 1 3 4 2

The Processes are assumed to have arrived in the order p1,p2,p3,p4,p5 all at time 0ms.
a) Draw 4 Gantt charts illustrating the execution of these processes

using FCFS, SJF, a non RR(Quantum=1ms) scheduling.

preemptive

priority

and

b) What is the turnaround time & waiting Time of each process for

each of the scheduling algorithms in part (a)?


c) Which of the schedules in part (a) results in the minimal average

waiting time(overall process)?


Operating System Concepts 7th Edition, Feb 2, 2005 5.36 Silberschatz, Galvin and Gagne 2005

Problem 1: Process P1 P2 Burst Time 6 8 Priority 3 1

P3
P4

7
3

2
4

(i) For the above processes apply non-preemptive FCFS, SJFS and Priority scheduling and find the waiting Time and Turnaround Time for each process. (ii) Calculate the Average Waiting Time and Average Turnaround Time for the above 3 algorithms and compare the AWT and ATT and discuss the effectiveness of the algorithms based on the AWT and ATT.
Operating System Concepts 7th Edition, Feb 2, 2005 5.37 Silberschatz, Galvin and Gagne 2005

Problem 1: Solution:

FCFS :

AWT = 41/4 = 10.2ms


ATT = 65/4 = 16.2ms

SJFS :

AWT = 28/4 = 7ms ATT = 52/4 = 13ms

Priority : AWT = 54 /4 = 13.5ms

ATT = 78/4 = 19.5ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.38

Silberschatz, Galvin and Gagne 2005

Problem 2: Process P1 P2 P3 P4 Arrival Time 0 1 2 3 Burst Time 8 4 9 5 Priority 3 4 1 2

(i) For the above processes apply preemptive SJFS , and Priority. find the waiting Time and Turnaround Time for each process. (ii) Calculate the Average Waiting Time and Average Turnaround Time for the above 2 algorithms and compare the AWT and ATT and discuss the effectiveness of the algorithms based on the AWT and ATT.
Operating System Concepts 7th Edition, Feb 2, 2005 5.39 Silberschatz, Galvin and Gagne 2005

Problem 2: Solution:

SJFS : AWT = 26/4 = 6.5ms


ATT = 52/4 = 13ms Priority : AWT = 43/4 = 10.75ms ATT = 69/4 = 17.25ms

Operating System Concepts 7th Edition, Feb 2, 2005

5.40

Silberschatz, Galvin and Gagne 2005

Multilevel Queue
Ready queue is partitioned into separate queues:

foreground (interactive) background (batch)


Each queue has its own scheduling algorithm

foreground RR background FCFS Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR 20% to background in FCFS

Scheduling must be done between the queues


Operating System Concepts 7th Edition, Feb 2, 2005

5.41

Silberschatz, Galvin and Gagne 2005

Multilevel Queue Scheduling

Operating System Concepts 7th Edition, Feb 2, 2005

5.42

Silberschatz, Galvin and Gagne 2005

Multilevel Feedback Queue


A process can move between the various queues; aging can be

implemented this way


Multilevel-feedback-queue scheduler defined by the following

parameters:

number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service

Operating System Concepts 7th Edition, Feb 2, 2005

5.43

Silberschatz, Galvin and Gagne 2005

Example of Multilevel Feedback Queue


Three queues:

Q0 RR with time quantum 8 milliseconds


Q1 RR time quantum 16 milliseconds Q2 FCFS

Scheduling

A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Operating System Concepts 7th Edition, Feb 2, 2005

5.44

Silberschatz, Galvin and Gagne 2005

Multilevel Feedback Queues

Operating System Concepts 7th Edition, Feb 2, 2005

5.45

Silberschatz, Galvin and Gagne 2005

Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are

available
Homogeneous processors within a multiprocessor Load sharing Asymmetric multiprocessing only one processor

accesses the system data structures, alleviating the need for data sharing

Operating System Concepts 7th Edition, Feb 2, 2005

5.46

Silberschatz, Galvin and Gagne 2005

Real-Time Scheduling
Hard real-time systems required to complete a

critical task within a guaranteed amount of time


Soft real-time computing requires that critical

processes receive priority over less fortunate ones

Operating System Concepts 7th Edition, Feb 2, 2005

5.47

Silberschatz, Galvin and Gagne 2005

Thread Scheduling
Local Scheduling How the threads library decides which

thread to put onto an available LWP


Global Scheduling How the kernel decides which kernel

thread to run next

Operating System Concepts 7th Edition, Feb 2, 2005

5.48

Silberschatz, Galvin and Gagne 2005

Pthread Scheduling API


#include <pthread.h> #include <stdio.h> #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM THREADS]; pthread attr t attr; /* get the default attributes */ pthread attr init(&attr); /* set the scheduling algorithm to PROCESS or SYSTEM */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread attr setschedpolicy(&attr, SCHED OTHER); /* create the threads */ for (i = 0; i < NUM THREADS; i++) pthread create(&tid[i],&attr,runner,NULL);

Operating System Concepts 7th Edition, Feb 2, 2005

5.49

Silberschatz, Galvin and Gagne 2005

Pthread Scheduling API


/* now join on each thread */

for (i = 0; i < NUM THREADS; i++)


pthread join(tid[i], NULL); } /* Each thread will begin control in this function */

void *runner(void *param)


{ printf("I am a thread\n"); pthread exit(0); }

Operating System Concepts 7th Edition, Feb 2, 2005

5.50

Silberschatz, Galvin and Gagne 2005

Operating System Examples


Solaris scheduling Windows XP scheduling Linux scheduling

Operating System Concepts 7th Edition, Feb 2, 2005

5.51

Silberschatz, Galvin and Gagne 2005

Solaris 2 Scheduling

Operating System Concepts 7th Edition, Feb 2, 2005

5.52

Silberschatz, Galvin and Gagne 2005

Solaris Dispatch Table

Operating System Concepts 7th Edition, Feb 2, 2005

5.53

Silberschatz, Galvin and Gagne 2005

Windows XP Priorities

Operating System Concepts 7th Edition, Feb 2, 2005

5.54

Silberschatz, Galvin and Gagne 2005

Linux Scheduling
Two algorithms: time-sharing and real-time

Time-sharing

Prioritized credit-based process with most credits is scheduled next Credit subtracted when timer interrupt occurs When credit = 0, another process chosen

When all processes have credit = 0, recrediting occurs Based on factors including priority and history Real-time Soft real-time

Posix.1b compliant two classes FCFS and RR Highest priority process always runs first

Operating System Concepts 7th Edition, Feb 2, 2005

5.55

Silberschatz, Galvin and Gagne 2005

The Relationship Between Priorities and Time-slice length

Operating System Concepts 7th Edition, Feb 2, 2005

5.56

Silberschatz, Galvin and Gagne 2005

List of Tasks Indexed According to Prorities

Operating System Concepts 7th Edition, Feb 2, 2005

5.57

Silberschatz, Galvin and Gagne 2005

Algorithm Evaluation
Deterministic modeling takes a particular

predetermined workload and defines the performance of each algorithm for that workload
Queueing models Implementation

Operating System Concepts 7th Edition, Feb 2, 2005

5.58

Silberschatz, Galvin and Gagne 2005

5.15

Operating System Concepts 7th Edition, Feb 2, 2005

5.59

Silberschatz, Galvin and Gagne 2005

End of Chapter 5

5.08

Operating System Concepts 7th Edition, Feb 2, 2005

5.61

Silberschatz, Galvin and Gagne 2005

In-5.7

Operating System Concepts 7th Edition, Feb 2, 2005

5.62

Silberschatz, Galvin and Gagne 2005

In-5.8

Operating System Concepts 7th Edition, Feb 2, 2005

5.63

Silberschatz, Galvin and Gagne 2005

In-5.9

Operating System Concepts 7th Edition, Feb 2, 2005

5.64

Silberschatz, Galvin and Gagne 2005

Dispatch Latency

Operating System Concepts 7th Edition, Feb 2, 2005

5.65

Silberschatz, Galvin and Gagne 2005

Java Thread Scheduling


JVM Uses a Preemptive, Priority-Based Scheduling Algorithm

FIFO Queue is Used if There Are Multiple Threads With the Same

Priority

Operating System Concepts 7th Edition, Feb 2, 2005

5.66

Silberschatz, Galvin and Gagne 2005

Java Thread Scheduling (cont)


JVM Schedules a Thread to Run When:
1. 2.

The Currently Running Thread Exits the Runnable State A Higher Priority Thread Enters the Runnable State

* Note the JVM Does Not Specify Whether Threads are Time-Sliced or Not

Operating System Concepts 7th Edition, Feb 2, 2005

5.67

Silberschatz, Galvin and Gagne 2005

Time-Slicing
Since the JVM Doesnt Ensure Time-Slicing, the yield() Method May Be Used: while (true) { // perform CPU-intensive task ... Thread.yield(); } This Yields Control to Another Thread of Equal Priority

Operating System Concepts 7th Edition, Feb 2, 2005

5.68

Silberschatz, Galvin and Gagne 2005

Thread Priorities

Priority Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

Comment Minimum Thread Priority Maximum Thread Priority Default Thread Priority

Priorities May Be Set Using setPriority() method: setPriority(Thread.NORM_PRIORITY + 2);

Operating System Concepts 7th Edition, Feb 2, 2005

5.69

Silberschatz, Galvin and Gagne 2005

You might also like