0% found this document useful (0 votes)
6 views24 pages

Lect 5

Uploaded by

ygf2024
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)
6 views24 pages

Lect 5

Uploaded by

ygf2024
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/ 24

Lecture 4- CPU Scheduling Continued

Instructor : Bibhas Ghoshal ([email protected])

Autumn Semester, 2019

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 1 / 24
Shortest Remaining Job First (SRJF)

Preemptive version of SJF


While a job A is running, if a new job B comes whose length is
shorter than the remaining time of job A, then B preempts A and B
is started to run.

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 2 / 24
SRJF-Example

Process Arrival Time Burst Time


P1 0.0 8
P2 1.0 4
P3 2.0 9
P4 3.0 5

SRJF scheduling chart

P1 P2 P4 P1 P3

0 1 5 10 17 26

Average waiting time = (9 + 0 + 2 + 15) / 4 = 6.5 ms

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 3 / 24
Numerical Example

Assume we have the following processes. Find out the finish time,
waiting time and turnaround time of each process for the following
scheduling algorithms: FCFS, SJF, SRJF.

Process Arrival Time CPU Burst


A 0 30
B 5 20
C 10 12
D 15 10

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 4 / 24
Numerical Example - FCFS

FCFS : Processes will run in the order they arrive. The following is
the completion, turnaround, waiting time of each process.
AT : Arrival Time ; BT : Burst Time
CT : Completion Time; TAT : Turnaround Time
WT : Waiting Time

AT BT CT TAT WT
A 0 30 30 30 0
B 5 20 50 45 25
C 10 12 62 52 40
D 15 10 72 57 47

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 5 / 24
Numerical Example - SJF

SJF: running order will be: A(30) D(10) C(12) B(20)


AT : Arrival Time ; BT : Burst Time
CT : Completion Time; TAT : Turnaround Time
WT : Waiting Time

AT BT CT TAT WT
A 0 30 30 30 0
B 5 20 72 67 47
C 10 12 52 42 30
D 15 10 40 25 15

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 6 / 24
Numerical Example - SRJF

SRJF: running order will be: A(5) B(5) C(12) D(10) B(15) A(25)
AT : Arrival Time ; BT : Burst Time
CT : Completion Time; TAT : Turnaround Time
WT : Waiting Time

AT BT CT TAT WT
A 0 30 72 72 42
B 5 20 47 42 22
C 10 12 22 12 0
D 15 10 32 17 7

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 7 / 24
Priority Scheduling

A priority number (integer) is associated with each process


The CPU is allocated to the process with the highest priority
(smallest integer is equivalent to highest priority)
Preemptive - higher priority process preempts the running one
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

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 8 / 24
Priority Scheduling - Example

AT CPU Burst Priority


A 0 20 3
B 5 15 2
C 10 20 0
D 25 15 1
E 30 20 1

Nonpreemptive priority scheduling: AAAACCCCDDDEEEEBBB


assuming each letter is 5 time units
Completion times: A: 20, B: 90, C: 40, D: 55, E: 75
Preemptive priority scheduling: ABCCCCDDDEEEEBBAAA
Completion times: A: 90, B: 75, C:30, D: 45, E: 65

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 9 / 24
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

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 10 / 24
Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 3
P3 3

The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

Typically, higher average turnaround than SJF, but better


response

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 11 / 24
Example of RR with Time Quantum = 20

Process Burst Time


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

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 12 / 24
Time Quantum and Context Switch Time

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 13 / 24
Turnaround Time Varies With The Time Quantum

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 14 / 24
Multilevel Queue

Ready queue is partitioned into separate queues:foreground


(interactive) and background (batch)
Each queue has its own scheduling algorithm
foreground – RR
background – FCFS
Scheduling must be done between the queues
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

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 15 / 24
Multilevel Queue

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 16 / 24
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

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 17 / 24
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.

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 18 / 24
Multilevel Feedback Queue

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 19 / 24
Multiple-Processor Scheduling

CPU scheduling more complex when multiple CPUs are available


Homogeneous processors within a multiprocessor
Load sharing : Preserve locality of data and state
Asymmetric multiprocessing – only one processor accesses the
operating system data structures, alleviating the need for kernel
data sharing among processors
Some cooperative processes like to run with n processors or none
at all : Gang scheduling to assign group of processors

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 20 / 24
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
In both cases, RT behaviour is achieved by dividing the program
into number of process, each of whose behaviour is predictable.
When an external event is detected, it is the job of the scheduler to
schedule the processes in such a way that all deadlines are met.
The events a RT system has to handle are periodic(occuring at
regular intervals) or aperiodic(occuring unpredictably)
If there are m periodic events and event i occurs with period Pi
and requires Ci seconds Pof CPU to handle each event, then the
load can be handled if m Ci
i=1 Pi ≤ 1 - schedulable RT system

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 21 / 24
Thread Scheduling

Local Scheduling – How the threads library decides which thread


to put onto an available light weight process (LWP) (kernel thread)
Global Scheduling – How the kernel decides which kernel thread
to run next

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 22 / 24
Windows Xp Priorities

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 23 / 24
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
Real Time
Softy real time
Posix.1b compliant – two classes
FCFS and RR
Highest Priority Process always run first

Bibhas Ghoshal IOSY 332C & IOPS 332C: OS Autumn Semester, 2019 24 / 24

You might also like