Copy for 303 NWU Operating System [Scheduling Algorithms]
Copy for 303 NWU Operating System [Scheduling Algorithms]
3201
Operating System
Scheduling Algorithms
Moniruzzaman
Lecturer
North Western University, Khulna-9100
303
www.moniruzzaman.xyz
1
Moniruzzaman, CSE_NWU
Confession
✿ Most of the materials have been collected from Internet.
✿ Images are taken from Internet.
✿ Various books are used to make these slides.
✿ Various slides are also used.
moniruzzaman
2
Moniruzzaman, CSE_NUBTK
Operating System (OS) Scheduling refers to the process of
Scheduling managing the execution of processes by determining the order in
which processes are given access to the CPU or other resources.
3
Moniruzzaman, CSE_NUBTK
Key CPU Utilization: Ensure the CPU is kept as busy as possible.
Objectives Throughput: Maximize the number of processes completed per
of unit of time.
Scheduling Turnaround Time: Minimize the total time taken from process
submission to its completion.
Fairness: Ensure all processes get a fair share of CPU time and
other resources.
4
Moniruzzaman, CSE_NUBTK
There are several different criteria to consider when trying to
Scheduling select the "best" scheduling algorithm for a particular situation
Criteria and environment, including:
5
Moniruzzaman, CSE_NUBTK
Scheduling Turnaround time - Time required for a
particular process to complete, from submission
Criteria time to completion.
6
Moniruzzaman, CSE_NUBTK
Terms Arrival Time: Time at which the process
arrives in the ready queue.
7
Moniruzzaman, CSE_NUBTK
Scheduling Turn Around Time:
Criteria Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T):
Waiting Time = Turn Around Time – Burst Time
8
Moniruzzaman, CSE_NUBTK
FCFS
First-Come, First-Served Scheduling
www.moniruzzaman.xyz
9
Moniruzzaman, CSE_NWU
First- By far the simplest CPU-scheduling algorithm
is the first-come, first-served (FCFS)
Come, scheduling algorithm.
First-
The process that requests the CPU first is
Served allocated the CPU first. The implementation of
Scheduling the FCFS policy is easily managed with a FIFO
queue.
www.moniruzzaman.xyz
10
Moniruzzaman, CSE_NWU
First- When a process enters the ready queue, its
PCB(Process Control Blocks) is linked onto the
Come, tail of the queue. When the CPU is free, it is
First- allocated to the process at the head of the
queue.
Served
Scheduling FCFS scheduling algorithm is non-preemptive
www.moniruzzaman.xyz
11
Moniruzzaman, CSE_NWU
First- Consider the following set of processes that
arrive at time 0, with the length of the CPU burst
Come, given in milliseconds:
First-
Served
Scheduling
www.moniruzzaman.xyz
12
Moniruzzaman, CSE_NWU
First- If the processes arrive in the order P1, P2, P3,
and are served in FCFS order, we get the result
Come, shown in the following Gantt chart, which is a
First- bar chart that illustrates a particular schedule,
including the start and finish times of each of
Served the participating processes:
Scheduling
www.moniruzzaman.xyz
13
Moniruzzaman, CSE_NWU
First-
Come,
First-
Served
Scheduling
Waiting time for P1 = 0; P2 = 24; P3 = 27
www.moniruzzaman.xyz
14
Moniruzzaman, CSE_NWU
First- Suppose that the processes arrive in the order
p2, p3, p1
Come,
First-
Served
Scheduling
www.moniruzzaman.xyz
15
Moniruzzaman, CSE_NWU
Priority Scheduling
www.moniruzzaman.xyz
16
Moniruzzaman, CSE_NWU
Priority The SJF algorithm is a special case of the
Scheduling general priority scheduling algorithm.
www.moniruzzaman.xyz
17
Moniruzzaman, CSE_NWU
Priority An SJF algorithm is simply a priority algorithm
Scheduling where the priority(p) is the inverse of the
(predicted) next CPU burst. The larger the CPU
burst, the lower the priority, and vice versa.
www.moniruzzaman.xyz
18
Moniruzzaman, CSE_NWU
Priority Note that we discuss scheduling in terms of high
Scheduling priority and low priority. Priorities are generally
indicated by some fixed range of numbers, such
as 0 to 7 or 0 to 4,095.
www.moniruzzaman.xyz
20
Moniruzzaman, CSE_NWU
Priority Using priority scheduling, we would schedule
Scheduling these processes according to the following
Gantt chart:
www.moniruzzaman.xyz
21
Moniruzzaman, CSE_NWU
Priority Priorities can be defined either internally or
Scheduling externally. Internally defined priorities use
some measurable quantity or quantities to
compute the priority of a process. For example
⭓ Time limits
⭓ Memory requirements
⭓ The number of open files and
⭓ The ratio of average I/0 burst to average CPU burst.
www.moniruzzaman.xyz
22
Moniruzzaman, CSE_NWU
Priority External priorities are set by criteria outside the
Scheduling operating system, Such as
www.moniruzzaman.xyz
23
Moniruzzaman, CSE_NWU
Priority Priority scheduling can be either preemptive or
Scheduling non-preemptive. When a process arrives at the
ready queue, its priority is compared with the
priority of the currently running process. A
preemptive priority scheduling algorithm will
preempt the CPU if the priority of the newly
arrived process is higher than the priority of the
currently running process.
www.moniruzzaman.xyz
24
Moniruzzaman, CSE_NWU
Priority Problem
Scheduling Indefinite blocking or Starvation.
Solution
Aging
www.moniruzzaman.xyz
25
Moniruzzaman, CSE_NWU
SJF
Shortest Job First Scheduling
www.moniruzzaman.xyz
26
Moniruzzaman, CSE_NWU
Shortest A different approach to CPU scheduling is the
Job shortest-job-first (SJF) scheduling algorithm.
www.moniruzzaman.xyz
27
Moniruzzaman, CSE_NWU
Shortest If the next CPU bursts of two processes are the
Job same, FCFS scheduling is used to break the tie.
First More appropriate term for this scheduling
method would be the shortest-next-CPU-burst
Scheduling algorithm, because scheduling depends on the
length of the next CPU burst of a process, rather
than its total length.
www.moniruzzaman.xyz
28
Moniruzzaman, CSE_NWU
Shortest The SJF algorithm can be either preemptive or
Job non-preemptive.
www.moniruzzaman.xyz
29
Moniruzzaman, CSE_NWU
Shortest **A preemptive SJF algorithm will preempt the
Job currently executing process, whereas a
First non-preemptive SJF algorithm will allow the
currently running process to finish its CPU
Scheduling burst.**
www.moniruzzaman.xyz
30
Moniruzzaman, CSE_NWU
Shortest
Job
First
Scheduling
www.moniruzzaman.xyz
31
Moniruzzaman, CSE_NWU
Shortest
Job
First
Scheduling
www.moniruzzaman.xyz
32
Moniruzzaman, CSE_NWU
FS
Fair-Share Scheduling
www.moniruzzaman.xyz
33
Moniruzzaman, CSE_NWU
Fair-Share To prevent this situation, some systems take
into account which user owns a process before
Scheduling scheduling it. In this model, each user is
allocated some fraction of the CPU and the
scheduler picks processes in such a way as to
enforce it. Thus if two users have each been
promised 50% of the CPU, they will each get
that, no matter how many processes they have
in existence.
www.moniruzzaman.xyz
34
Moniruzzaman, CSE_NWU
Fair-Share As an example, consider a system with two
users, each of which has been promised 50% of
Scheduling the CPU. User 1 has four processes, A, B, C, and
D, and user 2 has only one process, E. If
round-robin scheduling is used, a possible
scheduling sequence that meets all the
constraints is this one:
A E B E C E D E A E B E C E D E…
www.moniruzzaman.xyz
35
Moniruzzaman, CSE_NWU
Fair-Share On the other hand, if user 1 is entitled to twice as
much CPU time as user 2, we might get
Scheduling
A B E C D E A B E C D E...
www.moniruzzaman.xyz
36
Moniruzzaman, CSE_NWU
RR
Round Robin Scheduling
www.moniruzzaman.xyz
37
Moniruzzaman, CSE_NWU
Round The round-robin (RR) scheduling algorithm is
Robin designed especially for time-sharing systems.
It is similar to FCFS scheduling, but preemption
Scheduling is added to enable the system to switch
between processes.
www.moniruzzaman.xyz
38
Moniruzzaman, CSE_NWU
Round The ready queue is treated as a circular queue.
Robin The CPU scheduler goes around the ready
queue, allocating the CPU to each process for
Scheduling a time interval of up to 1 time quantum.
www.moniruzzaman.xyz
39
Moniruzzaman, CSE_NWU
Round To implement RR scheduling, we keep the
Robin ready queue as a FIFO queue of processes. New
Scheduling processes are added to the tail of the ready
queue. The CPU scheduler picks the first process
from the ready queue, sets a timer to interrupt
after 1 time quantum
www.moniruzzaman.xyz
40
Moniruzzaman, CSE_NWU
Round One of two things will then happen. The process
Robin may have a CPU burst of less than 1 time
quantum. In this case, the process itself will
Scheduling release the CPU voluntarily. The scheduler will
then proceed to the next process in the ready
queue.
www.moniruzzaman.xyz
41
Moniruzzaman, CSE_NWU
Round Otherwise, if the CPU burst of the currently
running process is longer than 1 time quantum,
Robin the timer will go off and will cause an interrupt to
Scheduling the operating system. A context switch will be
executed, and the process will be put at the tail of
the ready queue. The CPU scheduler will then
select the next process in the ready queue.
www.moniruzzaman.xyz
42
Moniruzzaman, CSE_NWU
Round
Robin
Scheduling
www.moniruzzaman.xyz
43
Moniruzzaman, CSE_NWU
Round
Robin
Scheduling
www.moniruzzaman.xyz
44
Moniruzzaman, CSE_NWU
Round
Robin
Scheduling
www.moniruzzaman.xyz
45
Moniruzzaman, CSE_NWU
🔴
M-l-Q
Multilevel Queue Scheduling
www.moniruzzaman.xyz
46
Moniruzzaman, CSE_NWU
Multilevel Another class of scheduling algorithms has
Queue been created for situations in which processes
are easily classified into different groups.
Scheduling
For example, a common division is made
between foreground (interactive) processes
and background (batch) processes. These two
types of processes have different
response-time requirements and so may have
different scheduling needs.
www.moniruzzaman.xyz
47
Moniruzzaman, CSE_NWU
Multilevel In addition, foreground processes may have
Queue priority (externally defined) over background
processes. A multilevel queue scheduling
Scheduling algorithm partitions the ready queue into several
separate queues.
www.moniruzzaman.xyz
48
Moniruzzaman, CSE_NWU
Multilevel Each queue has its own scheduling
Queue algorithm. For example, separate queues might
be used for foreground and background
Scheduling processes.
www.moniruzzaman.xyz
49
Moniruzzaman, CSE_NWU
Multilevel In addition, there must be scheduling among
the queues, which is commonly implemented
Queue as fixed-priority preemptive scheduling. For
Scheduling example, the foreground queue may have
absolute priority over the background queue.
www.moniruzzaman.xyz
50
Moniruzzaman, CSE_NWU
Multilevel Let's look at an example of a multilevel queue
scheduling algorithm with five queues, listed
Queue below in order of priority:
Scheduling
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
www.moniruzzaman.xyz
51
Moniruzzaman, CSE_NWU
Multilevel
Queue
Scheduling
www.moniruzzaman.xyz
52
Moniruzzaman, CSE_NWU
Multilevel Issues about time-slice among the queues. Here,
each queue gets a certain portion of the CPU
Queue time, which it can then schedule among its
Scheduling various processes.
www.moniruzzaman.xyz
53
Moniruzzaman, CSE_NWU
M-l-FQ
Multilevel Feedback Queue Scheduling
www.moniruzzaman.xyz
54
Moniruzzaman, CSE_NWU
Multilevel In multilevel queue scheduling algorithm,
Feedback processes are permanently assigned to a
queue when they enter the system.
Queue
Scheduling If there are separate queues for foreground
and background processes, for example,
processes do not move from one queue to the
other, since processes do not change their
foreground or background nature.
www.moniruzzaman.xyz
55
Moniruzzaman, CSE_NWU
Multilevel In contrast multilevel feedback queue
scheduling algorithm allows a process to move
Feedback between queues.
Queue
Scheduling The idea is to separate processes according to
the characteristics of their CPU bursts. If a
process uses too much CPU time, it will be
moved to a lower-priority queue. This scheme
leaves I/O-bound and interactive processes in
the higher-priority queues.
www.moniruzzaman.xyz
56
Moniruzzaman, CSE_NWU
Multilevel In addition, a process that waits too long in a
Feedback lower-priority queue may be moved to a
higher-priority queue. This form of aging
Queue prevents starvation.
Scheduling
www.moniruzzaman.xyz
57
Moniruzzaman, CSE_NWU
Multilevel In addition, a process that waits too long in a
lower-priority queue may be moved to a
Feedback higher-priority queue. This form of aging
Queue prevents starvation.
Scheduling
For example, consider a multilevel feedback
queue scheduler with three queues, numbered
from 0 to 2 (Next Slide). The scheduler first
executes all processes in queue 0. Only when
queue 0 is empty will it execute processes in
queue 1. Similarly, processes in queue 2 will only
be executed if queues 0 and 1 are empty.
www.moniruzzaman.xyz
58
Moniruzzaman, CSE_NWU
Multilevel
Feedback
Queue
Scheduling
www.moniruzzaman.xyz
59
Moniruzzaman, CSE_NWU
Multilevel A process that arrives for queue 1 will preempt a
Feedback process in queue 2. A process in queue 1 will in
turn be preempted by a process arriving for
Queue queue 0.
Scheduling
www.moniruzzaman.xyz
60
Moniruzzaman, CSE_NWU
Multilevel In general, a multilevel feedback queue
Feedback scheduler is defined by the following
parameters:
Queue
Scheduling 1. The number of queues
2. The scheduling algorithm for each queue
3. The method used to determine when to
upgrade a process to a higher priority queue
4. The method used to determine when to
demote a process to a lower priority queue
5. The method used to determine which queue a
process will enter when that process needs
service
www.moniruzzaman.xyz
61
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
62
Moniruzzaman, CSE_NWU
Guaranteed If n users are logged in while you are working,
Scheduling you will receive about 1/n of the CPU power.
Similarly, on a single-user system with n
processes running, all things being equal, each
one should get 1/n of the CPU cycles. That
seems fair enough.
www.moniruzzaman.xyz
63
Moniruzzaman, CSE_NWU
Guaranteed To make good on this promise, the system must
keep track of how much CPU each process has
Scheduling had since its creation. It then computes the
amount of CPU each one is entitled to, namely
the time since creation divided by n. Since the
amount of CPU time each process has actually
had is also known, it is fairly straightforward to
compute the ratio of actual CPU time consumed
to CPU time entitled.
www.moniruzzaman.xyz
64
Moniruzzaman, CSE_NWU
Guaranteed Rules:
Calculate the ratio of allocated CPU time to the
Scheduling amount of CPU time each process is entitled to
i.e. the ratio of actual CPU time consumed to
CPU time entitled.
www.moniruzzaman.xyz
65
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
66
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
67
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
68
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
69
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
70
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
71
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
72
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
73
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
74
Moniruzzaman, CSE_NWU
Guaranteed
Scheduling
www.moniruzzaman.xyz
75
Moniruzzaman, CSE_NWU
Lottery The basic idea of lottery scheduling is to give
Scheduling processes lottery tickets for various system
resources, such as CPU time. Whenever a
scheduling decision has to be made, a lottery
ticket is chosen at random, and the process
holding that ticket gets the resource.
www.moniruzzaman.xyz
76
Moniruzzaman, CSE_NWU
Lottery More important processes can be given extra
Scheduling tickets, to increase their odds of winning. If there
are 100 tickets outstanding, and one process
holds 20 of them, it will have a 20% chance of
winning each lottery. In the long run, it will get
about 20% of the CPU.
www.moniruzzaman.xyz
77
Moniruzzaman, CSE_NWU
Lottery Lottery scheduling has several interesting
properties. For example, if a new process shows
Scheduling up and is granted some tickets, at the very next
lottery it will have a chance of winning in
proportion to the number of tickets it holds. In
other words, lottery scheduling is highly
responsive.
www.moniruzzaman.xyz
78
Moniruzzaman, CSE_NWU
Lottery Cooperating processes may exchange tickets if
they wish. For example, when a client process
Scheduling sends a message to a server process and then
blocks, it may give all of its tickets to the server, to
increase the chance of the server running next.
When the server is finished, it returns the tickets
so that the client can run again.
www.moniruzzaman.xyz
79
Moniruzzaman, CSE_NWU
Lottery So far we have assumed that each process is
scheduled on its own, without regard to who its
Scheduling owner is. As a result, if user 1 starts up nine
processes and user 2 starts up one process, with
round robin or equal priorities, user 1 will get 90%
of the CPU and user 2 only 10% of it.
www.moniruzzaman.xyz
80
Moniruzzaman, CSE_NWU
🔴
ありがとう
www.moniruzzaman.xyz
81
Moniruzzaman, CSE_NWU