You are on page 1of 110

CPU Scheduling

Dr Sudipta Saha
proc file system in Linux

Proc file system (procfs) is virtual file system created on


fly when system boots and is dissolved at time of system
shut down.

It contains the useful information about the processes


that are currently running

The proc file system also provides communication


medium between kernel space and user space.
total 0
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1
dr-xr-xr-x 9 root root 0 Mar 31 21:34 10
dr-xr-xr-x 9 avahi avahi 0 Mar 31 21:34 1034
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1036
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1039
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1041
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1043
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1044
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1048
dr-xr-xr-x 9 root root 0 Mar 31 21:34 105
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1078
dr-xr-xr-x 9 root root 0 Mar 31 21:34 11
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1121
dr-xr-xr-x 9 lp lp 0 Mar 31 21:34 1146
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1149
dr-xr-xr-x 9 mysql mysql 0 Mar 31 21:34 1169
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1180
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1181
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1182
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1183
dr-xr-xr-x 9 postgres postgres 0 Mar 31 21:34 1184
dr-xr-xr-x 9 root root 0 Mar 31 21:34 1186
dr-xr-xr-x 9 root root 0 Mar 31 21:34 12
/

/dev

/proc -

for each PID of a process there is dedicated directory.

Process with PID=7494, you can check that there is


entry for this process in /proc file system.

ls -ltr /proc/7494

ls -l /proc | grep '^d'


total 0
-rw-r--r-- 1 mandeep mandeep 0 Apr 1 01:14 oom_score_adj
dr-xr-xr-x 13 mandeep mandeep 0 Apr 1 01:14 task
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:16 status
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:16 stat
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:16 cmdline
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 wchan
-rw-r--r-- 1 mandeep mandeep 0 Apr 1 01:17 uid_map
-rw-rw-rw- 1 mandeep mandeep 0 Apr 1 01:17 timerslack_ns
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 timers
-r-------- 1 mandeep mandeep 0 Apr 1 01:17 syscall
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 statm
-r-------- 1 mandeep mandeep 0 Apr 1 01:17 stack
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 smaps
-rw-r--r-- 1 mandeep mandeep 0 Apr 1 01:17 setgroups
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 sessionid
-r--r--r-- 1 mandeep mandeep 0 Apr 1 01:17 schedstat
-rw-r--r-- 1 mandeep mandeep 0 Apr 1 01:17 sched
lrwxrwxrwx 1 mandeep mandeep 0 Apr 1 01:17 root ->
/proc/2341/fdinfo
-rw-r--r-- 1 mandeep mandeep 0 Apr 1 01:17 projid_map
-r-------- 1 mandeep mandeep 0 Apr 1 01:17 personality
In linux, /proc includes a directory for each running process, including kernel
processes, in directories named /proc/PID, these are the directories present:

directory description

/proc/PID/cmdline Command line arguments.


/proc/PID/cpu Current and last cpu in which it was executed.
/proc/PID/cwd Link to the current working directory.
/proc/PID/environ Values of environment variables.
/proc/PID/exe Link to the executable of this process.
/proc/PID/fd Directory, which contains all file descriptors.
/proc/PID/maps Memory maps to executables and library files.
/proc/PID/mem Memory held by this process.
/proc/PID/root Link to the root directory of this process.
/proc/PID/stat Process status.
/proc/PID/statm Process memory status information.
/proc/PID/status Process status in human readable form.
What is CPU-scheduling ?
• The basis of multi-programmed operating systems
• The CPU can do more using the scheduling
mechanism
• Switching among different processes
What is CPU-scheduling
• Single processor unit
• Only one process can run at a time
• Other processes have to wait

• Multi-processor –
• More processes can run at the same time

• Some process should always run.


How its done -
• Several process waits in the memory at the same
time
• When one process is waiting the CPU is taken away
to another process
• CPU will not sit idle
• How do we determine which process should get
schedule, how long it should run ….etc..
• Prepare the scheduling … Time schedule for the
processes
CPU and I/O burst cycles
• Process execution consists of a cycle of CPU
execution and I/O wait.
• Process state alternates between these two states.

• CPU execution state


• I/O wait state

• CPU execution state – when it begins


• I/O wait – waits for some input out waiting states
• Alternates between these to states
CPU and I/O burst cycles
• CPU-burst – The time when the process is doing
CPU operation / execution
• I/O-burst – The time when it waits for an I/O
operation to get completed

Initial CPU burst I/O burst CPU I/O burst

CPU burst I/O burst ….. Final CPU burst


CPU and I/O burst cycles
• Initial CPU-burst – to initiate the process – start the
execution etc.
• The final burst – for termination of the process

Initial CPU burst I/O burst CPU I/O burst

CPU burst I/O burst ….. Final CPU burst


Examples
Load to accumulator
Add , Subtract, Multiply
CPU burst
Store from accumulator
WRITE TO FILE

WAIT FOR I/O I/O burst

Load from register


Load to accumulator
CPU burst
Transfer to memory
READ FROM FILE

WAIT FOR I/O I/O burst


Process states
• A process changes its states throughout its execution
• State of the process is defined by part what its doing /
current activity

• NEW – The process being created – NEW QUEUE


• Running – Instructions being executed –
• Waiting – process waiting for some event to occur –
WAITING QUEUE
• Ready – process is waiting to be assigned to a processor
– READY QUEUE
• Terminated – finished execution
CPU-scheduler
When process becomes idle, the OS selects of the
process from the ready queue to be executed

The selection is done by short-term scheduler (CPU-


scheduler) –

It decides which process will get the processor next

All the processes are there in the memory


Dispatcher
It gives the control of the CPU to the processed
selected by the short term scheduler

Dispatcher has to be very quick – as many switching


has to be done

Time in between stopping one process and starting


another process – is called dispatch latency
Preemptive and Non-preemptive
CPU scheduling decision takes places in the following four
circumstances –

1. When a process switches from the running state to the waiting


state

2. When a process switches from the running state to the ready


state (when an interrupt occurs)

3. When a process switches from the waiting state to the ready


state (when completion of an I/O)

4. When a process terminates


Preemptive and Non-preemptive
• For situation 1 and 4 – there is no choice –
• A new process must be selected to run
• For situation 2 and 3 there are two choices –
• 1) The same process maybe selected which was running
• 2) A new process maybe selected

• When scheduling takes places only under 1 and 4


we call it non-preemptive scheduling [cooperative]
• When scheduling takes places in all the cases we
say preemptive scheduling
Scheduling criteria
How to judge – whether a scheduling algorithm is good
or bad -

CPU Utilization

Turn around time

Waiting time

Response Time
Scheduling criteria
CPU Utilization

The CPU should be made busy as much as possible to


get more out of the system

It can range from 0 to 100 percent – in reality it may


vary from 40 to 90 percent
Scheduling criteria
Throughput

Number of processes completed per unit time

How much work CPU is doing


Scheduling criteria
Turn-around time

How long a process takes to execute – Sum of the periods spent

To get into the memory


From the view of a particular process
Waiting in the ready
The span: queue
Time of submission ------- time of Executing in the CPU
completion
And doing I/O operation
Includes all the states through which the
process goes through
Scheduling criteria
Waiting time

CPU scheduling algorithms affects the amount of time a process


spend in the ready queue

Sum of the periods that the process spends in the ready queue

Scheduler – assigns the CPU – once the process gets the CPU –
the time it takes – So, how fast the process can be assigned with
the CPU – Should not be waiting long in the ready queue

Starvation – for the CPU- should be reduced for every process


Scheduling criteria
Response time

Turn-around time maynot be the right criteria

A process may create some results very fast and then continue with the other results

The time from the submission of a request until the first response is produced is called
response time

Time it takes to start responding –does not include the time to output the response

Turn around time is limited by the speed of the output device – which is not true for
response time
FCFS – First Come First Serve
Scheduling
• The simplest one
• Whoever comes first – will be given first
• Fair
• Simple to understand and simple to implement
• A FIFO queue is enough

• Its blind – its does not care the characteristics of the


processes –
• example – A queue in a counter – old, kids – will be
treated exactly in a same way
FCFS – First Come First Serve
Scheduling
• Tail ---element1--- element2---…. ElementN – Head

• When a process enters a ready queue – its PCB is


linked to the tail of the queue

• A process wont be removed from running state


unless it ends execution
FCFS
• Average waiting time – often is quite long

• Example

Process ID Burst time (ms) Arrival order


P1 24 0
P2 3 2
P3 3 3
FCFS
• Gantt Chart- for FCFS order – P1, P2, P3

P1 P2 P3
24 27 30
0

Process ID Waiting time


P1 0
P2 24
P3 27

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


FCFS – First Come First Serve
Scheduling
• Let us assume a different arrival order –

• P2, P3, P1

P2 P3 P1

0 3 6 30

Process ID Waiting time Average waiting


P1 6 time =
(6 + 0 + 3 )/ 3 = 3 ms
P2 0
P3 3
FCFS
• Average waiting time in FCFS in general is not minimal
• It may vary substantially if the process waiting time
varies
• It depends on the order the processes arrives
• FCFS is not preemptive
• As – when a process is executing in the CPU – it cannot be
stopped to execute some other process
• All process have to wait until the current one complete
• Similar to our normal life queues…
• Until – the process releases the CPU by terminating or by I/O
requesting
FCFS
• FCFS algorithm is problematic for time sharing
system – where each user should get a share of the
CPU at regular interval
• Its not desired to allow one process to get hold of
the CPU for extended period of time – there may
be many small processes …
• Entire system gets delayed
FCFS – example
• Disadvantage –
• If processes having higher burst time arrives before
processes having smaller burst time – then the later
need to keep waiting for longer

Convoy effect
FCFS Example
• Calculate the average waiting time and average
turn around time in the following scenario – if FCFS
scheduling is followed
Process ID Arrival Time Burst time (ms)
P1 4 5
P2 6 4
P3 0 3
P4 6 2
P5 5 4
FCFS example
• First create the Gantt chart

0 3 4 9 13 17 19
P3 P1 P5 P2 P4
FCFS example
• First create the Gantt chart
Process ID Arrival Time Burst time
P1 4 5
P2 6 4
P3 0 3
P4 6 2
P5 5 4

0 3 4 9 13 17 19
P3 Idle time P1 P5 P2 P4
FCFS - example
Turn around time = Completion time – Arrival time

Waiting time = Turn around time – burst time


FCFS - example
0 3 4 9 13 17 19
P3 Idle time P1 P5 P2 P4

Turn around time = Completion time – Arrival time


Waiting time = Turn around time – burst time

Process ID Completion Turn around Waiting time


Time time
P1 9 9–4=5 5–5=0
P2 17 17 – 6 = 11 11 – 4 = 7
P3 3 3–0=3 3–3=0
P4 19 19 – 6 = 13 13 – 2 = 11
P5 13 13 – 5 = 8 8–4=4
FCFS example
Average turn around time and average waiting time

Average turn around time


= (5 + 11 + 3 + 13 + 8)/5 = 8 units

Average waiting time


= (0 + 7 + 0 + 11 + 4)/5 = 4.4 units
Shortest Job First Algorithm (SJF)
• The length of the next CPU burst - we don’t need
the whole time
• When CPU is available – the process having the
smallest next CPU burst is allocated
• If next two processes have the same next CPU
burst, FCFS is used to break the tie
• Better name – Shortest-Next-CPU-burst-Algorithm
Shortest Job First Algorithm (SJF)

• If we strictly need to follow the rule – we may need


to make it preemptive
• But SJF can be either preemptive or nonpreemptive

• Non-preemptive - Completes / goes to waiting state


• Preemptive – otherwise
Example - SJF
• Non-premptive
Consider the following set of
processes – length of the CPU
bursts –
Process ID Burst time
(ms)
P1 6 Find out the average waiting
P2 8 time of the processes if SJF
P3 7 with non-preemptive
P4 3
scheduling is following
Process ID Burst time
(ms)
P1 6
GANTT Chart P2 8
P3 7
P4 3

Get the smallest burst time process


and allocate the process the first -
All processes
arrived at the
same time -
0 3 9 16 24
P4 P1 P3 P2
Example - SJF
Average waiting time

Process ID Burst time (ms) Waiting time


P1 6 = 3 ms
P2 8 = 16 ms
P3 7 = 9 ms
P4 3 = 0 ms

0 3 9 16 24
P4 P1 P3 P2
Example - SJF
Average waiting time = (3 + 16 + 9 + 0)/4 = 7 ms

Process ID Burst time (ms) Waiting time


P1 6 = 3 ms
P2 8 = 16 ms
P3 7 = 9 ms
P4 3 = 0 ms

0 3 9 16 24
P4 P1 P3 P2
Process ID Burst time
(ms) The same set of process will
P1 6 have an average waiting
P2 8 time of 10.25 ms in case of
P3 7 FCFS scheduling algorithms
P4 3

Why ?

If we allow smaller processes to execute first – that will


benefit the smaller processes more than increasing the
waiting time for the larger processes

Result: Lesser average waiting time – as all


processes are given equal weight
Example – SJF – preemptive version
Consider Four processes – and the time points
when the processes arrive at the ready queue -

Process ID Arrival time Burst time


P1 0 8
P2 1 4
P3 2 9
P4 3 5
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0
P1 arrives
first, but it
has high
burst time
P1
Process ID Arrival time Burst time
P1 0 8 (should be 7)
P2 1 4
P3 2 9
P4 3 5

0 1
P2 arrives having
lesser burst time
than P1

P1 P2
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1

P1 P2

P2 has execution time of 4 ms

But at time 2, P3 arrives

P3 has execution time 9 ms which is higher than P2’s rest CPU


time, that is 3  So P2 is not pre-empted
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1

P1 P2

P2 will continue until completion as it has the smallest


execution time
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5

P1 P2

At time 3 P4 arrives – burst time is 5 – which is still higher than


remaining burst time of P2 (ie 2)

So P2 will still continue


Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5

P1 P2

After completion of P2 ie at 5 ms

Who are waiting ?? P1 has remaining burst time = 7 ms


P3 = 9 ms
P1, P3, P4 P4 = 5 ms

So P4 is executed
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5 10

P1 P2 P4

P4 executes from 5 to 10

No one more arrives …. P1 has remaining burst time = 7 ms


P3 = 9 ms

So P1 is executed next
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5 10 17

P1 P2 P4 P1

P1 executes from 10 to 17

No one more arrives …. P3 = 9 ms

So P3 is executed next
Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5 10 17 26

P1 P2 P4 P1 P3

P3 executes from 17 to 26

No one more arrives …. Completes


Process ID Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

0 1 5 10 17 26

P1 P2 P4 P1 P3

Preemption occurs for P1


Waiting time = Turn around time – burst time

Calculate the average


waiting time
Process ID Arrival time Burst Waiting time
time
P1 0 8 10 – 1 – 0 = 9 ms
P2 1 4 1 – 0 – 1 = 0 ms
P3 2 9 17 – 0 – 2 = 15 ms
P4 3 5 5 – 0 – 3 = 2 ms

Waiting time = Turn around time – burst time


= 17 – 0 – 8 = 9 ms
0 1 5 10 17 26

P1 P2 P4 P1 P3
Waiting time =
Total waiting time
Calculate the average – No of ms process executed
– Arrival Time

waiting time
Process ID Arrival time Burst Waiting time
time
P1 0 8 10 – 1 – 0 = 9 ms
P2 1 4 1 – 0 – 1 = 0 ms
P3 2 9 17 – 0 – 2 = 15 ms
P4 3 5 5 – 0 – 3 = 2 ms

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

0 1 5 10 17 26

P1 P2 P4 P1 P3
In preemptive version of SJF
scheduling algorithm -

Which process gets the CPU –

The one having next smallest


remaining CPU burst time

Also called Shortest-remaining-time-


first scheduling
Problems with SJF scheduling
• An optimal algorithm, - But cannot be used for
short time scheduling algorithm
• Difficult to know the length of the next CPU burst
time
Way out -
• We may approximate –
• We may be able to predict its value –
• Its usually similar in length than the previous CPU
burst –
• Some cases it will be the same – but in some it
wont be –

• Shortest predicted CPU burst -


• An operating system uses
shortest remaining time first
SJF Example - algorithm for scheduling of
processes
• Consider the following
processes with arrival time and
CPU burst time
• What is the average waiting
time of all the processes?

Process ID Arrival time Burst time


P1 0 12
P2 2 4
P3 3 6
P4 8 5
SJF Example -
Process ID Arrival time Burst time
P1 0 12
P2 2 4
P3 3 6
P4 8 5

GANTT Chart
0 2 6 12 17 27

P1 P2 P3 P4 P1
Waiting time =
Total waiting time
Calculate the average – No of ms process executed
– Arrival Time

waiting time
Process ID Arrival time Burst Waiting time
time
P1 0 12 17 – 2 – 0 = 15 ms
P2 2 4 2 – 0 – 2 = 0 ms
P3 3 6 6 – 0 – 3 = 3 ms
P4 8 5 12 – 0 – 8 = 4 ms

Average waiting time = ( 15 + 0 + 3 + 4) / 4 = 5.5 ms

0 2 6 12 17 27

P1 P2 P3 P4 P1
Priority Scheduling
• There is a priority with each process
• CPU would allocate the process having the highest
priority
• If equal priority – FCFS is applied

• In SJF – a priority is the inverse of the predicted


next CPU burst
Priority scheduling can be either
preemptive or non-preemptive
• Preemptive – will pre-empt the CPU from the
currently executing process if a higher priority
process arrives

• Non-preemptive – will add the newly arrived


process having higher priority in the head of the
queue so that its selected next
[Running process wont be disturbed]
Priority scheduling
Process ID Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

• Lower the value – higher the priority


• All arrive at time 0
Gantt Chart
Process ID Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

• Lower the value – higher the priority

0 1 6 16 18 19

P2 P5 P1 P3 P4
Average waiting time
Process ID Burst time Priority Waiting time
P1 10 3 6 ms
P2 1 1 0 ms
P3 2 4 16 ms
P4 1 5 18 ms
P5 5 2 1 ms

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

0 1 6 16 18 19

P2 P5 P1 P3 P4
Problems with priority scheduling
• The major problem is indefinite blocking – starvation

• What is blocking – A process that is ready to run but


waiting in the ready queue – is called blocked

• In Priority Scheduling – low priority processes may wait


for ever in the ready queue – indefinitely

• A steady stream of high priority processes – may result


such situation
Solution
• Priority scheduling with aging

• Gradually increase the priority of the low priority


processes that are waiting in the ready queue for a
long time
• Say priority ranges from 0 to 127
• Increase the priority of a process every 15 minutes
• If a process has initial priority of 127 – will becomes
the highest priority process and get executed
Examples
• Will be discussed in the tutorial
Round Robin Scheduling
• Round robin scheduling is designed for time sharing
systems
• Its is similar to FCFS – with pre-emption

• A small unit of time is called a time quantum or


time slice – is assigned to a process
• Generally from 10 ms to 100 ms
• A process executes only for that amount of time
first … Then the next process etc..
Round Robin Scheduling
• The ready queue is treated a circular queue and the
CPU scheduler goes through the queue
• Allocate the processes in the queue for one time
quantum
P1 P2
New process is added
P7 P3 in the tail of the queue

P4
P6
P5
Round Robin Scheduling
• A queue is maintained
• CPU scheduler selects the head of the queue
• 1st process starts and a timer starts for one
quantum
• When the timer goes off – The scheduler takes the
next entry – again executes for one quantum
• There are two prossibilities-
Round Robin Scheduling
• When a process gets the process – but the burst
time is less than the time quantum –
• Process will itself release the CPU
• When a process gets the process – the burst time is
more than the quantum –
• The timer will trigger – interrupt to the OS
• Context switch will happen
• Current context is saved
• How it can be implemented ?? CPU scheduler does
not move around the queue – rather head entry is
appended to the tail ….
Round Robin Scheduling
Process ID Burst time
P1 24
P2 3
P3 3

Apply round robin scheduling mechanism –


Assume time quantum to be 4 ms
Calculate the Turnaround time and waiting time
Gantt chart
Process ID Burst time
P1 24
P2 3
P3 3

The timer interrupt – whether its needed or not ?

0 4 7 10 14 18 22 26 30

P1 P2 P3 P1 P1 P1 P1 P1
Turn around time and
waiting time
Process ID Burst time
P1 24
P2 3
P3 3

Turn around time = Completion time – arrival time

Waiting time = turn around time – burst time

0 4 7 10 14 18 22 26 30

P1 P2 P3 P1 P1 P1 P1 P1
Turn around time and
waiting time
Process ID Completion time Turn around time Waiting time

P1 30 30 – 0 = 30 30-24 = 6
P2 7 7–0=7 7–3=4
P3 10 10 – 0 = 10 10 – 3 = 7

Turn around time = Completion time – arrival time


Waiting time = turn around time – burst time

0 4 7 10 14 18 22 26 30

P1 P2 P3 P1 P1 P1 P1 P1
Waiting time
• Waiting time =
Last start time
- Arrival Time
- (Preemption number X Time quantum)
Process ID Completion time Turn around time Waiting time

P1 30 26 – 0 – 5X4 = 6
P2 7 4 – 0 – (0 X 4) =4
P3 10 7 – 0 – (0 X 4) = 7
Multilevel Queue Scheduling
• Scheduling among group of processes

• Processes are classified into groups –

• One scheduling algorithm for one class

• Two possible groups –


• Fore ground process – interactive – Quick
• Background process – not interactive – batch process

• Different response time requirements


• Scheduling needs are different
• Foreground processes – may have priority defined
over background processes
• [All foreground – high priority]
• Should not be slow – as bad experience of the users
need to be avoided

• Solution - The ready queue is partitioned into


several separate queues
• Multiple ready queues –
• Scheduling takes place within these groups and
among these groups

• The processes are permanently assigned to one


queue – based on –
• Properties such memory size, process priority and type
• Each queue will have its own scheduling algorithm,
• Example
• Classify based on foreground and background – two
queues are there
• Foreground processes will be scheduled based on RR
• Need to be good response time -
• Background processes will be scheduled based on FCFS
• They can wait for some time [higher waiting time]

• Scheduling among the queues –


• Commonly – Fixed priority preemptive scheduling [No aging]
An example of a multi-level queue scheduling
algorithm with five queues -
Highest priority

System processes

Interactive processes

Interactive editing processes

Batch processes

Student processes

Lowest priority
An example of a multi-level queue scheduling
algorithm with five queues -
Highest priority System processes
System processes
have the highest

Interactive processes

Interactive editing processes

All the queues


Batch processes
above should be
empty to allow
Student processes
the student
Lowest priority process
An example of a multi-level queue scheduling
algorithm with five queues -
Highest priority If batch queue is
System processes
empty, student
processes will
start execution
Interactive processes

Interactive editing processes But suddenly an


interactive
Batch processes process arrives,
then student
Student processes
process will be
preempted
Lowest priority
Multilevel Feedback Queue
Scheduling
• In MFQS – a process is allowed to move from one
queue to other queue
• Separate processes according to the characteristics
of their CPU bursts –
• some may take lower time and some may take higher
time
Multilevel Feedback Queue
Scheduling
• In MFQS – a process is allowed to move from one
queue to other queue
• Separate processes according to the characteristics
of their CPU bursts –
• some may take lower time and some may take higher
time

If a process uses too A processing if waiting


much CPU time – too long in a lower
move it to the low- priority queue may be
priority queue moved to a higher
priority queue
Multilevel Feedback Queue
Scheduling
• I/O bound processes OR interactive processes are
usually kept in the higher priority queue – as they
will take less CPU time
• This will allow all the other queues to get
addressed

If a process uses too A processing if waiting


much CPU time – too long in a lower
move it to the low- priority queue may be
priority queue moved to a higher
priority queue
Multilevel Feedback Queue
Scheduling
• Form of aging – help to solve the problem of
starvation

If a process uses too A processing if waiting


much CPU time – too long in a lower
move it to the low- priority queue may be
priority queue moved to a higher
priority queue
An example of a multi-level feedback queue
scheduling algorithm with five queues -
Highest priority

System processes RR with quantum = 4ms

Interactive processes RR with quantum = 8ms

Interactive editing processes RR with quantum = 10ms

Batch processes SJF

Student processes FCFS

Lowest priority
Multilevel Feedback Queue
Scheduling
• Its defined by the following properties
• Number of queues
• Scheduling algorithm for each queue
• The method used to determine when to upgrade a
process from lower priority queue to higher priority
queue
• The method to determine when a process is to be
downgraded from higher priority queue to lower priority
queue
• When a process is moved – where to go – how many
step down or how many step up – ie to which of the
higher or lower priority queue
Types of Schedulers
• Their main task is to select the jobs to be submitted
into the system and to decide which process to run.

• Schedulers are of three types −

• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
Long Term Scheduler
• Job scheduler.

• It selects processes from the queue and loads them


into memory for execution.
• Process loads into the memory for CPU scheduling.

• The primary objective of the job scheduler is to


provide a balanced mix of jobs, such as I/O bound
and processor bound.
Long Term Scheduler
• It also controls the degree of multiprogramming.
• For stability - Average rate of process creation must be
equal to the average departure rate of processes
leaving the system.
• Long-term scheduler may not be available or minimal.

• Time-sharing operating systems have no long term


scheduler. When a process changes the state from new
to ready, then there is use of long-term scheduler.
Example
• When in a batch processing system more processes
are submitted than the memory can take –

• The long-term scheduler or the job scheduler uses


some algorithms to pick up processes from the job
queue and loads it into the main memory for
execution.

• The long-term scheduler should pick up a


combination of I/O-Bound and CPU-Bound jobs.
Properties - LTS
• Process State Change:
• NEW - the jobs(code) are still stored on the disk.
• The long-term scheduler loads them into main memory
from the disk and the process state changes from NEW to
READY.

• Queue Change: The long-term scheduler picks up jobs from


the job queue and inserts them into the ready queue.
• The long-term scheduler decides the degree of
multiprogramming.
• Long-term schedulers are not used in time sharing operating
systems.
Short Term Scheduler
• CPU scheduler.
• To increase system performance.

• Change of ready state to running state of the


process.

• Short-term schedulers make the decision of which


process to execute next.
Short term scheduler
• Process State Change: In the ready queue, the state of the
processes is READY. The short-term scheduler allocates
them the cpu and the state now changes to RUNNING.

• Queue Change: The short-term scheduler picks up


processes from the ready queue. The process which has
been allocated cpu will either terminate or block on some
I/0 or will be preempted by some other process.
• In case, it blocks on I/O, it will enter the device queue. In
case, it gets preempted, it will enter the ready queue again.
• Short-term scheduler is used in both batch operating
system and time sharing operating system.
Medium Term Scheduler
• Part of Swapping process - in-charge of handling the swapped
out-processes.

• It removes the processes from the memory.

• A running process may become suspended if it makes an I/O


request.

• Swapping: To remove the process from memory and make space


for other processes, the suspended process is moved to the
secondary storage.,

• Swapping may be necessary to improve the process mix.


Medium Term Scheduler
• What happens when a running process requires
additional memory but the RAM is full?

• In this case, the pages of processes which are in ready


queue are swapped out to the disk. Medium-term
scheduler takes care of deciding which pages are to be
swapped out to disk.

• When the process whose pages were swapped out


starts running, the swapped out pages are then
swapped in by the medium-term scheduler.
Medium Term Scheduler
• Medium-term scheduler can also be called as
memory manager in modern operating systems.
• Medium-term scheduler decreases the degree of
multiprogramming since it is removing some
processes from the main memory.
• Process State Change: There is no process state
change in this case as the process still remains in
READY state whose pages were swapped out.
• Queue Change: No queue change.
Process states revisited

Scheduled / dispatched
Created Complete
Termin
New Ready Running
ated
Priority / Time
Suspend
I/O done
I/O
Resume Wait/
block
Suspended
ready Suspend
Resume

Completed I/O but suspended


Suspended
wait

You might also like