0% found this document useful (0 votes)
2 views28 pages

Scheduling

The document provides an overview of scheduling in operating systems, detailing various scheduling methods such as First-Come First-Served, Shortest Job First, Priority Scheduling, and Round Robin. It discusses the properties, advantages, and disadvantages of each method, along with scheduling criteria like CPU utilization, throughput, and response time. Additionally, it touches on real-time scheduling and the concepts of preemptive and non-preemptive kernels.

Uploaded by

vamah16794
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)
2 views28 pages

Scheduling

The document provides an overview of scheduling in operating systems, detailing various scheduling methods such as First-Come First-Served, Shortest Job First, Priority Scheduling, and Round Robin. It discusses the properties, advantages, and disadvantages of each method, along with scheduling criteria like CPU utilization, throughput, and response time. Additionally, it touches on real-time scheduling and the concepts of preemptive and non-preemptive kernels.

Uploaded by

vamah16794
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/ 28

Operating Systems: Scheduling

Indian Statistical Institute

https://www.isical.ac.in/~mandar/courses.html#os

ISI Scheduling 1 / 27
Background: mode switch, process switch
1. Scheduling
2. Possible context-switch
3. Return to user mode

Kernel mode
User mode

Pi running in user Pj running in user Pk running in user


mode mode mode
User → kernel mode switch

Scheduling chooses Pj , Pk , etc. from all ready-to-run processes.

ISI Scheduling 2 / 27
Preemptive vs. non-preemptive scheduling

Process switch can occur when a process


1. needs to wait for some resource / puts itself to sleep (via sleep ())
2. exits
(conclusion of exit system call invokes context switch code)
3. returns from kernel mode to user mode but is not the most eligible
process to run
Non-preemptive scheduling: scheduling takes place only in 1
and 2
when a process gets the CPU, it keeps it until it sleeps/exits
Preemptive scheduling: case 3 is also permissible

thumb rule: Preemptive scheduler runs whenever a


process is added to the Ready Queue.

ISI Scheduling 3 / 27
Terminology / recap
Reference: Section 3.2, 6.1
Process states
running, ready, waiting, etc.
typically alternates between CPU bursts and I/O bursts
time-sharing / multiprogramming: to maximize CPU utilization
multiple processes are kept in memory simultaneously
when one process is waiting, another process executes

CPU bound process: spends more time doing computations,


generates I/O requests infrequently
I/O bound process: spends more time doing I/O than computing

Job queue: contains all processes in the system


Ready queue: contains all processes that reside in main memory and
are ready to run
Device queue: contains all procs. waiting for a particular device
ISI Scheduling 4 / 27
First-come first-served
Reference: Section 6.3.1
Method:
1. Maintain a FIFO queue.
2. When a process enters the ready queue, it is placed at the end of
the queue.
3. When the CPU is free, it is allocated to the process at the head of
the queue.

Properties:
Non-preemptive
Unsuitable for time-sharing systems (∵ each user should get a
share of the CPU at regular intervals)
Average waiting time is not minimal
Convoy effect: many processes may have to wait for one long
process to finish
Example: 1 CPU-bound proc. + many I/O bound procs.
ISI Scheduling 5 / 27
First-come first-served

Example:

Ready processes Burst time


P1 24
P2 3
P3 3
Processes arrive in the order P1 , P2 , P3

Gantt chart:
P1 P2 P3

Average waiting time: (0 + 24 + 27)/3 = 17ms

ISI Scheduling 6 / 27
Shortest job first
Reference: Section 6.3.2
Method:
1. When the CPU is available, assign it to the process with the
shortest next CPU burst.
2. Break ties on a FCFS basis.

Properties:
Optimal in terms of average waiting time
Suitable for job scheduling in a batch system
(use time limit specified by user at time of submission)
Length of the next CPU request is generally not known

Pre-emptive SJF: (shortest remaining time first)


1. When a new process arrives at the ready queue, compare its CPU
burst with remaining time for current process.
2. If new process has shorter burst, preempt current process.
ISI Scheduling 7 / 27
Shortest job first

Example:

Ready processes Arrival time Burst time


P1 0 8
P2 1 4
P3 2 9
P4 3 5

ISI Scheduling 8 / 27
FCFS

P1 P2 P3 P4
FCFS

0 5 10 15 20 25

P1 P1 P1 P1

P2 P2 P2

P3 P3

P4

Ready Queue

Average waiting time: (0 + 7 + 10 + 18)/4 = 8.75ms

ISI Scheduling 9 / 27
Shortest job first

P1 P2 P4 P3
SJF

0 5 10 15 20 25

P1 (8) P2 (4) P2 (4) P2 (4)

P3 (9) P4 (5)

P3 (9)
Ready Queue

Average waiting time: (0 + 7 + 15 + 9)/4 = 7.75ms

ISI Scheduling 10 / 27
Shortest remaining time first
Turnaround time for P1

P1 P2 P4 P1 P3
SRTF
P1
P3
Waiting times
P4

0 5 10 15 20 25

P1 (8) P2 (4) P2 (3) P2 (2)

P1 (7) P1 (7) P4 (5)

P3 (9) P1 (7)
Ready Queue
P3 (9)

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

Turnaround times: 17, 4, 24, 7 resp.

ISI Scheduling 11 / 27
Priority scheduling
Reference: Section 6.3.3
Method:
1. Compute a priority for each process.
Internal priorities: computed using time limits, memory
requirements, ratio of avg. I/O burst to avg. CPU burst, etc.
External priorities: computed on the basis of external political /
administrative factors

2. Allocate CPU to process with highest priority.


3. Break ties on a FCFS basis.

Properties:
Can be preemptive or non-preemptive (cf. SJF)

ISI Scheduling 12 / 27
Priority scheduling

Example: (low numbers ⇒ high priority)

Processes Burst time Priority


P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2

ISI Scheduling 13 / 27
Priority scheduling

Example: (low numbers ⇒ high priority)

Processes Burst time Priority Scheduled Waiting time


P1 10 3 6–16 6
P2 1 1 0–1 0
P3 2 3 16–18 16
P4 1 4 18–19 18
P5 5 2 1–6 1

Average waiting time: 41/5 = 8.2 ms

ISI Scheduling 13 / 27
Priority scheduling

Properties:
Starvation / indefinite blocking: if high priority processes keep
arriving, low priority process may have to wait indefinitely for CPU.
Priority scheduling with aging: priority may be increased in
proportion to waiting time to prevent starvation

ISI Scheduling 14 / 27
Round robin
Reference: Section 6.3.4

Time quantum (or time slice): maximum interval of time between two
invocations of the scheduler
a process can be allocated the CPU for one quantum at one time
usually between 10–100ms
Method:
1. Maintain a FIFO queue of ready processes.
2. Allocate CPU to first process from queue; set timer for 1 time
quantum.
3. If running process releases CPU, or timer expires:
preempt current process and switch context to the next process in
the ready queue;
add previously running process to tail of ready queue.

ISI Scheduling 15 / 27
Round robin

Example:
Ready processes Burst time
P1 24
P2 3
P3 3
Time quantum = 4ms

P1 P2 P3 P1
...
0 5 10 15 20

Context-switch happens Context-switch happens No context-switch because


when quantum expires before quantum expires no other process is
ready-to-run

Average waiting time: 5.66ms


ISI Scheduling 16 / 27
Round robin

Properties:
Suitable for time-sharing systems (∵ every process gets the CPU
for q time units after waiting for (n − 1)q time units)
Duration of time quantum:
large time quantum ⇒ RR → FCFS
small time quantum ⇒ context-switching overhead ↑

ISI Scheduling 17 / 27
Multilevel queue
Reference: Section 6.3.5
Method:
1. Partition the queue into several separate queues; assign a fixed
priority value to each.
2. Assign each process to some fixed queue, based on its properties.
Example: system procs. / interactive procs. / interactive editing
procs. / batch procs. / student procs.
3. Select a queue based on:
fixed priority (starvation possible), OR
priority-based proportional time slicing, e.g.,
50% of time servicing Q0 ,
30% of time servicing Q1 ,
20% of time servicing Q2 .
4. Select a job from the queue using a suitable scheduling algorithm
(e.g. FCFS, RR).

ISI Scheduling 18 / 27
Multilevel queue

Scheduling algorithm 1
Queue1

Queue2
Schedule

.. across
queues

Queuek Scheduling algorithm k

Properties:
Preemptive

ISI Scheduling 19 / 27
Multilevel feedback queue
Reference: Section 6.3.6

Processes may be moved between scheduling queues


Parameters:
# of queues
scheduling algorithm / time slice for each queue
initial queue selection policy
promotion/demotion policies

Example:
3 queues, Q0 , Q1 , Q2
scheduling policies:
Q0 = RR (quantum = 8ms) Q1 = RR (quantum = 16ms) Q2 = FCFS
on entry to ready queue, processes assigned to Q0
on exit from Q0 , process is placed at tail of Q1
on exit from Q1 , process is placed at tail of Q2
OPTIONAL : if process waits too long in Q2 , promote it to Q1

ISI Scheduling 20 / 27
Scheduling criteria I
Reference: Section 6.2

CPU utilization: proportion of time that CPU does “useful work”


CPU time spent executing in user mode
CPU time spent by kernel servicing a user request
does not include CPU time spent by kernel doing system work
e.g., scheduling, context switching
Throughput: number of processes that are completed per unit
time
long processes ⇒ throughput ↓
short processes ⇒ throughput ↑
Turnaround time: interval from the time of submission of a process
to the time of completion
Waiting time: total amount of time spent by a process in the ready
queue

ISI Scheduling 21 / 27
Scheduling criteria II

Response time: time from the submission of a request until the


first response is produced
(amount of time taken to start responding, not including the time
taken to complete the output)

NOTE : maximum (minimum)/average/variance may be suitable for evaluation

ISI Scheduling 22 / 27
Commonly ignored / simplified details

When / how do processes “arrive”?


Process switching time in Gantt charts

What actually makes process P arrive?

Kernel mode
User mode

Pi | {z } System call / interrupt that creates


P or changes P ’s state to ready

... Pi Pj Pk ...
Process switches are NOT instantaneous.

z }| {
Kernel mode
User mode

Pj Pk

ISI Scheduling 23 / 27
Real-time scheduling

ISI Scheduling 24 / 27
Real-time scheduling
Reference: Section 6.6

Hard real-time systems:


Critical tasks must be completed within a guaranteed amount of
time
Resource reservation:
processes are submitted with deadlines
scheduler may admit the process and guarantee completion, or
reject
Duration of operating system functions must be predictable and
bounded
Consists of special-purpose software running on dedicated
hardware
Soft real-time systems:
Critical processes receive priority over “ordinary” processes
May be implemented as a general-purpose system
ISI Scheduling 25 / 27
Real-time scheduling

Preemptible vs. non-preemptible kernels:


Non-preemptible kernels
context switch can happen only at restricted points
completion of system call/interrupt
sleep()
specially inserted preemption points
delays may be unpredictable
easier to implement
Preemptible kernels
suitable for soft real-time systems
harder to implement

ISI Scheduling 26 / 27
Real-time scheduling

Priority inversion
High priority process may have to wait for resource held by a low
priority process
Priority inheritance: processes that are accessing resources
required by high priority process inherit the high priority until they
release the resource

ISI Scheduling 27 / 27

You might also like