You are on page 1of 23

Operating Systems

Scheduling Algorithms
Scheduling Algorithms

Scheduling Algorithms:
• First-Come, First-Served Scheduling
• Shortest-Job-First Scheduling
• Priority Scheduling
• Round-Robin Scheduling
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling

Dr.B.Hemantha Kumar. RVR&JC CE 2


Scheduling Algorithms

• First-Come, First-Served Scheduling (FCFS):


• in the (FCFS) scheduling algorithm, the
process that requests the CPU first is allocated
the CPU first. The implementation of the FCFS
policy is easily managed with a FIFO queue.
Consider the following set of processes that
arrive at time 0, with the length of the CPU burst
given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
Dr.B.Hemantha Kumar. RVR&JC CE 3
Scheduling Algorithms

• If the processes arrive in the order P1, P2, P3,


and are served in FCFS order, we get the result
shown in the following Gantt chart, which is a
bar chart that illustrates a particular schedule,
including the start and finish times of each of
the participating processes: Process BT
P1 24
P2 3
Wiating Time of P1 :0 P3 3
Turnaround Time of P1 :0+24=24
Wiating Time of P2 :24 Turnaround Time of P2 :24+3=27
Wiating Time of P3 :27 Turnaround Time of P3 :27+3=30
average waiting timeis
(0+24+27)/3=17

Dr.B.Hemantha Kumar. RVR&JC CE 4


Scheduling Algorithms
First-Come, First-Served Scheduling (FCFS):
• If the processes arrive in the order P2, P3, P1,
however, the results will be as shown in the following
Gantt chart:

Process BT
P1 24
P2 3
Wiating Time of P2 :0 Turnaround Time of P2 :0+3=3 P3 3
Wiating Time of P3 :3 Turnaround Time of P3 :3+3=6
Wiating Time of P1 :6 Turnaround Time of P1 :6+24=30
average waiting timeis
(0+3+6)/3=3 milli sec

Dr.B.Hemantha Kumar. RVR&JC CE 5


Scheduling Algorithms
• Shortest-Job-First Scheduling(SJFS): The SJF scheduling
algorithm is an optimal, in that it gives the minimum
average waiting time for a given set of processes
Process BT
P1 6
P2 8
P3 7
P4 3

Wiating Time of P1 :3
Turnaround Time of P1 :3+6=9
Wiating Time of P2 :16
Turnaround Time of P2 :16+8=24
Wiating Time of P3 :9
Turnaround Time of P3 :9+7=16
Wiating Time of P4 :0
Turnaround Time of P4 :0+3=3
average waiting timeis
(3 + 16 + 9 + 0)/4 = 7 milli sec
Dr.B.Hemantha Kumar. RVR&JC CE 6
Scheduling Algorithms
• Shortest-Job-First Scheduling(SJFS):
• Although the SJF algorithm is optimal, it cannot be
implemented at the level of short-term CPU scheduling.
With short-term scheduling, there is no way to know the
length of the next CPU burst.
• The next CPU burst is generally predicted as an
exponential average of the measured lengths of previous
CPU bursts.
• We can define the exponential average with the following
formula.
• Let tn be the length of the nth CPU burst, and let τn+1 be
our predicted value for the next CPU burst. Then, for α, 0 ≤
α ≤ 1, define τn+1 = tn + (1 − α) τn.
• The value of tn contains our most recent information, while τn stores
the past history. The parameter α controls the relative weight of
recent and past history in our prediction
Dr.B.Hemantha Kumar. RVR&JC CE 7
Scheduling Algorithms
• Preemptive SJFS
Process AT BT
If the processes arrive at P1 0 8
the ready queue at the P2 1 4
times as shown P3 2 9
P4 3 5

Process P1 is started at time 0, since it is the only process in the


queue. Proces P2 arrives at time 1. The remaining time for
process P1 (7 milliseconds) is larger than the time required by
process P2 (4 milliseconds), so process P1 is preempted, and
process P2 is scheduled. The average waiting time for this
example is *(10 − 1) + (1 − 1) + (17 − 2) + (5 − 3)+/4 = 26/4 = 6.5
milliseconds. Dr.B.Hemantha Kumar. RVR&JC CE 8
Scheduling Algorithms
• Preemptive SJFS
Process AT BT
If the processes arrive at P1 0 8
the ready queue at the P2 1 4
times as shown P3 2 9
P4 3 5

Wiating Time of P1 :(10-1)=9


Turnaround Time of P1 :9+8=17
Wiating Time of P2 :(1-1)=0
Turnaround Time of P2 :0+4=4
Wiating Time of P3 :(17-2)=15
Turnaround Time of P3 :15+9=24
Wiating Time of P4 :(5-3)=2
Turnaround Time of P4 :2+5=7
average waiting timeis
(9 + 0 + 15+ 2)/4 = 6.5 milli sec
Dr.B.Hemantha Kumar. RVR&JC CE 9
Scheduling Algorithms
Process are arrived at time 0
• Priority Scheduling in the order P1, P2, · · ·, P5,
Apriority is associated with each Process BT Priority
process, and the CPUis allocated P1 10 3
to the process with the highest P2 1 1
P3 2 4
priority. Equal-priority processes P4 1 5
are scheduled in FCFS order. P5 5 2

Wiating Time of P1 :=6


Turnaround Time of P1 :6+10=16
Wiating Time of P2 :=0
Turnaround Time of P2 :0+1=1
Wiating Time of P3 :16
Turnaround Time of P3 :16+2=18
Wiating Time of P4 :18
Turnaround Time of P4 :18+1=19
Wiating Time of P5 :1
Turnaround Time of P5 :1+5=6
average waiting timeis
(6 + 0 + 16+ 18+1)/5 = 8.2 milliDr.B.Hemantha
sec Kumar. RVR&JC CE 10
Scheduling Algorithms
• Priority Scheduling: Priority scheduling can be either
preemptive or 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.
• A non preemptive priority scheduling algorithm will simply
put the new process at the head of the ready queue.
• A major problem with priority scheduling algorithms is
indefinite blocking, or starvation. A process that is ready
to run but waiting for the CPU can be considered blocked.
• Asolution to the problem of indefinite blockage of low-
priority processes is aging. Aging involves gradually
increasing the priority of processes that wait in the system
for a long time.
Dr.B.Hemantha Kumar. RVR&JC CE 11
Scheduling Algorithms
• Round-Robin Scheduling: The round-robin (RR) scheduling
algorithm is designed especially for timesharing systems.
• A small unit of time, called a time quantum or time slice,
is defined. A time quantum is generally from 10 to 100
milliseconds in length.
• The CPU scheduler goes around the ready queue,
allocating the CPU to each process for a time interval of up
to 1 time quantum.
• One of two things will then happen. The process may have
a CPU burst of less than 1 time quantum. In this case, the
process itself will release the CPU voluntarily. The
scheduler will then proceed to the next process in the
ready queue.
• If the CPU burst of the currently running process is longer
than 1 time quantum, the timer will go off and will cause
an interrupt to the operating system. 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.
Dr.B.Hemantha Kumar. RVR&JC CE 12
Scheduling Algorithms
• Round-Robin Scheduling:
Process BT
Consider the following set of P1 24
processes that arrive at time 0, with P2 3
the length of the CPU burst time with P3 3
time quantum of 4 milliseconds

Wiating Time of P1 :(10-4)=6


Turnaround Time of P1 :6+24=30
Wiating Time of P2 :4
Turnaround Time of P2 :4+3=7
Wiating Time of P3 :7
Turnaround Time of P3 :7+3=10
average waiting timeis
(6 + 4 + 7)/3 = 5.66 milli sec

Dr.B.Hemantha Kumar. RVR&JC CE 13


Scheduling Algorithms
• Round-Robin Scheduling:
• The performance of the RR algorithm depends on the
size of the time quantum. If the time quantum is
extremely large, the RR policy is the same as the FCFS
policy.
• In contrast, if the time quantum is extremely small
(say, 1 millisecond), the RR approach can result in a
large number of context switches.

Dr.B.Hemantha Kumar. RVR&JC CE 14


Scheduling Algorithms
• Multilevel Queue Scheduling:
• In this processes are classified into different groups like
foreground & background process.
• A multilevel queue scheduling algorithm partitions the
ready queue into several separate queues
• The processes are permanently assigned to one queue,
generally based on some property of the process, such as
memory size, process priority, or process type.
• Each queue has its own scheduling algorithm.
• For example, separate queues might be used for
foreground and background processes. The foreground
queue might be scheduled by an RR algorithm, while the
background queue is scheduled by an FCFS algorithm

Dr.B.Hemantha Kumar. RVR&JC CE 15


Scheduling Algorithms
Multilevel Queue Scheduling:

Dr.B.Hemantha Kumar. RVR&JC CE 16


Scheduling Algorithms
Multilevel Queue Scheduling:
• In addition, there must be scheduling among the
queues, which is commonly implemented as fixed-
priority preemptive scheduling. For example, the
foreground queue may have absolute priority over the
background queue.
• Each queue has absolute priority over lower-priority
queues. No process in the batch queue, for example,
could run unless the queues for system processes,
interactive processes, and interactive editing
processes were all empty.
• If an interactive editing process entered the ready
queue while a batch process was running, the batch
process would be preempted.

Dr.B.Hemantha Kumar. RVR&JC CE 17


Scheduling Algorithms
Multilevel Feedback Queue Scheduling:
• Normally, when the multilevel queue scheduling
algorithm is used, processes are permanently
assigned to a queue when they enter the system.
• The multilevel feedback queue scheduling algorithm,
allows a process to move between queues.
• 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.
• In addition, a process that waits too long in a lower-
priority queue may be moved to a higher-priority
queue. This form of aging prevents starvation.
Dr.B.Hemantha Kumar. RVR&JC CE 18
Scheduling Algorithms
Multilevel Feedback Queue Scheduling:

Dr.B.Hemantha Kumar. RVR&JC CE 19


Scheduling Algorithms
Multilevel Feedback Queue Scheduling:
In general, a multilevel feedback queue scheduler is
defined by the following parameters:
• The number of queues
• The scheduling algorithm for each queue
• The method used to determine when to upgrade a
process to a higher priority queue
• The method used to determine when to demote a
process to a lower priority queue
• The method used to determine which queue a process
will enter when that process needs service

Dr.B.Hemantha Kumar. RVR&JC CE 20


Example: find avg waiting time (FCFS,SJFS,RR(q=10))
Process Burst Time
P1 10
• Example P2 29
For FCFS P3 3
P4 7
Gantt chart P5 12

P2(29) P4(7) P5(12)


P1(10) P3(3)

0 10 39 42 49 61
Waiting Time TAT: =WT+BT
–P1: 0 p1: 10,
P2 : 10, P2:39
P3: 39, P3: 42
P4: 42, P4: 49
P5:49 P5:61
Avg Wt (0+10+39+42+49)/5= 28

Dr.B.Hemantha Kumar. RVR&JC CE 21


Example: find avg waiting time (FCFS,SJFS,RR(q=10))
Process Burst Time
P1 10
P2 29
• Example P3 3
P4 7
SJF P5 12

P4(7) P1(10) P5(12) P2(29)


P3(3)

0 3 10 20 32 61

Waiting Time TAT: =WT+BT


–P1: 10 p1: 20,
P2 : 32, P2:61
P3: 0, P3: 3
P4: 3, P4: 10
P5:20 P5:32
Avg Wt (10+32+0+3+20)/5= 13
Dr.B.Hemantha Kumar. RVR&JC CE 22
Example: find avg waiting time (FCFS,SJFS,RR(q=10))
Process Burst Time
P1 10
P2 29
• Example P3 3
P4 7
RR(q=10) P5 12
P1 19
P5 2
P1 9

P1(10) P2(10) P3(3) P4(7) P5(10) P1(10) P5(2) P1(9)

0 10 20 23 30 40 50 52 62
Waiting Time TAT: =WT+BT
–P1: 0+(40-10)+(52-2)=32 p1: 42,
P2 : 10, P2:39
P3: 20, P3: 23
P4: 23, P4: 30
P5:30+(50-40)=40 P5:52
Avg Wt (32+10+20+23+40)/5= 25
Dr.B.Hemantha Kumar. RVR&JC CE 23

You might also like