0% found this document useful (0 votes)
7 views

07.Scheduling_Introduction

Scheduling_Introduction

Uploaded by

himanshujhaa4262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

07.Scheduling_Introduction

Scheduling_Introduction

Uploaded by

himanshujhaa4262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Scheduling: Introduction

Chapter 7 of Operating System: Three Easy Pieces

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 1


Review: Process State Transitions

 How to transition? (“mechanism”) – Previous lectures

 When to transition? (“policy”) – on context switch OS scheduler decides


which process to run next, from set of ready processes

Descheduled
Running Ready
Scheduled

I/O: initiate I/O: done

Blocked

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 2


Exercise

 Think of examples in life where scheduling is involved

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 3


Scheduling: Outline

 How should we develop a basic framework for thinking about


scheduling policies?

 What metrics are important?

 What are the key assumptions?

 What basic approaches have been used in different computer systems

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 4


Scheduling: Terminology

 Workload: set of job descriptions (arrival time, run time)

 Job: current CPU burst (CPU time used in one stretch) of a process

 Performance Metrics: measurement of scheduling quality


 Turnaround time: The amount of time taken to execute the particular job

Turnaround time = (completion time - arrival time)

 Waiting Time: The amount of time a process has been waiting in the ready
queue. Waiting Time = (turnaround time - burst time)

 Response time: The amount of time from when a request was submitted
until the first response is produced. Response Time = (the time at which a
process get the CPU first time - arrival time)

 Throughput: How many jobs complete per unit of time

 Fairness: All jobs get same amount of CPU over some time interval
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 5
Exercise

 Take a real life scenario/example and try to explain these metrics in


that context.

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 6


Scheduling: A beginning

 Workload assumptions:
1. Each job runs for the same amount of time.

2. All jobs arrive at the same time.

3. All jobs only use the CPU (i.e., they perform no I/O).

4. The run-time of each job is known.

 Don’t worry… we will relax them as we move along

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 7


Scheduling Metrics

 Performance metric: Turnaround time


 The time at which the job completes minus the time at which the job
arrived in the system.

𝑻𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 = 𝑻𝒄𝒐𝒎𝒑𝒍𝒆𝒕𝒊𝒐𝒏 − 𝑻𝒂𝒓𝒓𝒊𝒗𝒂𝒍

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 8


First In, First Out (FIFO)

 First Come, First Served (FCFS)


 Very simple and easy to implement

 Example: three jobs arrive at t=0 in the order A,B,C


 Each job runs for 10 seconds.

 Gantt chart below : Illustrates how jobs are scheduled over time on a CPU
A B C

0 20 40 60 80 100 120

Time (Second)

𝟏𝟎 + 𝟐𝟎 + 𝟑𝟎
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟐𝟎 𝒔𝒆𝒄
𝟑

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 9


Why FIFO is not that great?

 Let’s relax assumption 1: Each job no longer runs for the same
amount of time.

 Example: three jobs arrive at t=0 in the order A,B,C


 A runs for 100 seconds, B and C run for 10 each.

 Draw the Gantt chart and calc. Avg. turnaround time


A B C

0 20 40 60 80 100 120

Time (Second)

𝟏𝟎𝟎 + 𝟏𝟏𝟎 + 𝟏𝟐𝟎


𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟏𝟏𝟎 𝒔𝒆𝒄
𝟑

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 10


FIFO problem: Convoy Effect

 Think of a similar scenario you may have encountered personally

 What do you do in that situation?

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 11


Shortest Job First (SJF)

 Run the shortest job first, then the next shortest, and so on
 Non-preemptive scheduler

 Same example: three jobs arrive at t=0 in the order A,B,C


 A runs for 100 seconds, B and C run for 10 each.

 Provably optimal when all processes arrive together

B C A

0 20 40 60 80 100 120

Time (Second)

𝟏𝟎 + 𝟐𝟎 + 𝟏𝟐𝟎
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟓𝟎 𝒔𝒆𝒄
𝟑

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 12


SJF with Late Arrivals from B and C

 Let’s relax assumption 2: Jobs can arrive at any time.

 Example: A arrives at t=0 and needs to run for 100 seconds.


 B and C arrive at t=10 and each need to run for 10 seconds

 SJF is non- preemptive, so short jobs can still get stuck behind long ones

[B,C arrive]
A B C

0 20 40 60 80 100 120

Time (Second)

𝟏𝟎𝟎 + 𝟏𝟏𝟎 − 𝟏𝟎 + (𝟏𝟐𝟎 − 𝟏𝟎)


𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟏𝟎𝟑. 𝟑𝟑 𝒔𝒆𝒄
𝟑

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 13


Shortest Time-to-Completion First (STCF)

 Add preemption to SJF


 Also knows as Preemptive Shortest Job First (PSJF)

 When a new job enters the system:


 Preempts running task if its remaining CPU burst is more than that of new
arrival

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 14


Shortest Time-to-Completion First (STCF)

 Example:
 A arrives at t=0 and needs to run for 100 seconds.

 B and C arrive at t=10 and each need to run for 10 seconds

 Draw Gantt chart and calc. avg. TT

[B,C arrive]
A B C A

0 20 40 60 80 100 120

Time (Second)

(𝟏𝟐𝟎 − 𝟎) + 𝟐𝟎 − 𝟏𝟎 + (𝟑𝟎 − 𝟏𝟎)


𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟓𝟎 𝒔𝒆𝒄
𝟑

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 15


New scheduling metric

 Given our new assumption, STCF is provably optimal. Hence used in


early batch processing computing systems

 With advent of Time sharing demand for interactive performance


came

 Response time: The time from when the job arrives to the first time
it is scheduled.

𝑻𝒓𝒆𝒔𝒑𝒐𝒏𝒔𝒆 = 𝑻𝒇𝒊𝒓𝒔𝒕𝒓𝒖𝒏 − 𝑻𝒂𝒓𝒓𝒊𝒗𝒂𝒍

 STCF and related disciplines are not particularly good for response time.

How can we build a scheduler that is


sensitive to response time?

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 16


Round Robin (RR) Scheduling

 Time slicing Scheduling


 Run a job for a time slice and then switch to the next job in the run
queue until the jobs are finished.
 Time slice is sometimes called a scheduling quantum.

 It repeatedly does so until the jobs are finished.

 The length of a time slice must be a multiple of the timer-interrupt period.

RR is fair, but performs poorly on metrics


such as turnaround time

Performance and fairness are often at odds in scheduling

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 17


RR Scheduling Example

 A, B and C arrive at the same time.

 They each wish to run for 5 seconds.


A B C
0 + 5 + 10
𝑇𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒 = = 5𝑠𝑒𝑐
3

0 5 10 15 20 25 30
Time (Second)

SJF (Bad for Response Time)

A B CA B CA B CA B CA B C

0+1+2
𝑇𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒 = = 1𝑠𝑒𝑐
3

0 5 10 15 20 25 30
Time (Second)

RR with a time-slice of 1sec (Good for Response Time)

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 18


The length of the time slice is critical.

 The shorter time slice


 Better response time

 The cost of context switching will dominate overall performance.

 The longer time slice


 Amortize the cost of switching

 Worse response time

Deciding on the length of the time slice presents


a trade-off to a system designer

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 19


Incorporating I/O

 Let’s relax assumption 3: All programs perform I/O

 Example:
 A and B need 50ms of CPU time each.

 A runs for 10ms and then issues an I/O request


 I/Os each take 10ms

 B simply uses the CPU for 50ms and performs no I/O

 The scheduler runs A first, then B after

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 20


Incorporating I/O (Cont.) with STCF

A A A A A B B B B B

CPU

Disk

0 20 40 60 80 100 120 140


Time (msec)

Poor Use of Resources

A B A B A B A B A B Maximize the
CPU utilization by
CPU treating each 10ms
sub job of A as new
Disk job

0 20 40 60 80 100 120 140


Time (msec)

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 21


Incorporating I/O (Cont.)

 When a job initiates an I/O request.


 The job is blocked waiting for I/O completion.

 The scheduler should schedule another job on the CPU.

 When the I/O completes


 An interrupt is raised.

 The OS moves the process from blocked back to the ready state.

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 22

You might also like