0% found this document useful (0 votes)
33 views15 pages

Adobe Scan 27-Nov-2024

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)
33 views15 pages

Adobe Scan 27-Nov-2024

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/ 15

Important terms:

1. Turn Around Time = Completion Time - Arrival Time

2. Waiting Time = Turn Around Time - Burst Time

3. AGantt chart is a bar chart that provides a visual view of tasks scheduled
over time.

PO Pl P2 P3

16 22

First Come First Serve (FCFS) Scheduling algorithm:

a) the simplest CPU-scheduling algorithm.


b) In this scheduling, process that requests the CPU first is First In Frs: Ot

allocated the CPU first.


c) The implementation of the FCFS policy is managed with a FIFO
queue.
the queue.
d) When a process enters the ready queue, its PCB is linked onto the tail of
e) When the CPUis free, it is allocated to the process at the head of the queue.
f) The running process is then removed from the queue.

Example:
Process ID Arrival Burst Time
Time Calculate the average wafting time and average Turnaround
P1 4 S time, if FCES Scheduling Algorithm is followed
P2 6
P3 3
P4 6 P3 P1 PS P2

P5 5 13 17 19
9
Gantt Chart The shaded box represents the idle time of CPU
Turn Around Time s Completion Time- Arrival Time
Waiting Time = Turn Around Tme - Burst Time
Process ID Arrival Time Burst Time Completion
Time
Turnaround
Time
Waiting Time

P1
9-4=5 5-5=0
P2 6 4 17 17-6-11 11-4=7
P3 3 3-0=3 3-3=0
P&
2 19-6=13 13-2=11

PS 5 4 13 13-5=8 8-4=4

Average Turn Around time = (5+11+3+13+8)/5


40/5
=8 ms
Average Waiting Time = (0+7+0+11+4)/5
= 22/5
= 4.4 ms

Convoy Effect

1. FCFS algorithm is non-preemptive in nature, that is, once CPU time has been allocated to a
process, other processes can get CPU time only after the current process has finished. This
property of FCFS scheduling leads to the situation called Convoy Effect.

2. Convoy Effect is phenomenon associated with the First Come First Serve (FCFS) algorithm,
in which the whole Operating System slows down due to few slow pro cesses.

Hence in Convoy Effect, one slow process slows down the performance of the entire set of
processes, and leads to wastage of CPU time and other devices

Shortest-Job-First Scheduling:
1. This algorthm associates with each process the length of the process's next
CPUburst.
2 When the CPU is available, it is assigned to the process that has the
smallest next CPU burst.
3 This the best approach to minimize waiting time.
4. If the next CPU bursts of two processes are the same, FCFS scheduling is
used to break the tie.
5 A
more appropriate tem tor this scheduling method would be the shoreet
next- CPU-burst algorithm because scheduling depends on the leneth of
the next CPU burst of a process, rather than its total length.

Example:

and burst time are given tietoy


Consider the sot of 5 orocesses whose arrival time

Process Id Arrival time Burst time

P1

P2

P3

TumAround Time =Comoletion Time-Arrival Time


Wating Time =Turn Around Time-Burst Time

Process Id Completiontime Turn Around time Waiting time


7-34 4-1 3

16-1z 15 15 -4
P2 16

P3 9-45 5-2 3

P4 6-06 6-6O

12 12-2 = 10 10 -3* 7

NoW.

" Average Turn Around ime (4 " 155+ 6" 10)/5 40/5 8una
" Average waiting ine (3 1+3+0+7) /5 24 15 4,8 unit

0 6 12 16

P4 P1 P3 P5 P2

Gentt Chart

Problem with Non-Pre-emptive SJF

If the arrival time for processes are different, which means allthe processes are not avalabe
in the ready queue at time 0, and some jobs arrive after some time, in such situation,
sometimes process with short burst time have to wait for the current process's executiOn to
on arrival of a process with short duration, the
finish, because in Non Pre-emptive SJF.
halted/stopped to execute the short job first.
existing job/process's execution is not

Scheduling (Preemptive)
Shortest Remaining Time First or Shortest Job First
Scheduling, jobs are put into ready queue as theyis
1 In Preemptive Shortest Job First time arives, the existing process
arrive,but as a process with short burst
shorter job is executed first.
preempted or removed fromn execution, and the
shortest job first scheduling is less than
2 The average waiting time for preemptive
FCFS scheduling.
both, non-preemptive SJF scheduling andShortest
also known as Remaining Time First, becausce at
3 The Pre-emptive SJF is executed frst. remaining time is
any given point of time, the job with the shortest

Example:SJF (Preemptive)

Consider the set of5 processes whose arrival time and burst time are given below

Agess AivaTne Burst ne


0
1 1

If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average turn around time.
Process Arrival Time Burst Time
Gantt Chart:
P? 1

P3 1 2 13 20
P4
6

Process ArrivalTne Burst Time Completion Turn Around Waiting Time


Time Time
P1 8 20 20 12
P2 2 1 0

P3 3 5 3

6 13

Average Turn Around Time =37/5 =7.4 ms


Average Waiting Time = 17/53.4 ms
Example of SJF Scheduling (Preemptive)
consider the following four proceses, with the iength of the Clburst given in miseonds, snd prurss aiW# at tthe
ready queue at the times shown
Pu Arrival Tine Burst Tix

Arrval Burst Time Completion Turn Around Waning time


|Process Time
Time
17 17

1
1
P3
PA

Average Turn Around Time =52/4=13 ms


Average Waiting Time =26/4-6.5 ms

Priority Scheduling
with the highest priority.
1, Apriority is associated with each process, and the CPU is allocated to the process
2. Equal-priority processes are scheduled in FCFS order.
(predicted) next CPU hurst.
3. An SJF algorithm is simply a priority algorithm where the priority (p) Is the inverse of the
4. The larger the CPUburst, the lower the priority, and vice versa.
high priority. This difference
Note:Some systems use low numbers to represent low priority; others use low numbers for
can lead to confusíon.
1 We assume that low numbers represent high priority.
2 Priority scheduling can be either preemptive or nonpreemptive.
3 Apreemptive priority scheduling algorithm willpreempt the CPU if the priority of the newiy
arrived process is
higher than the priority of the currently running process.
queue.
4 Anonpreemptive priority scheduling algorithm will simply put the new process at the head of the ready

Example
In the Example, there are 7 processes P1, P2, P3, P4, PS, P6 and P7. Their prorit ies, Arriíval Titrea
nd burst time are given in the table.

Process ID Priority Arrival Time Burst Time


2
4

7 6

7 10 7 10

We can prepare the Gantt chart according to the Non Pre-cmptive priority scheduling.
number 2. Since No
The Process Pl arrives at time 0 with the burst time of 3units and the priority
other process has arrived tillnow hence the OS will schedule it immediately.
Meanwhile the execution of PI. two more Processes P2 and P3 are arrived. Since the priority of
P3 is 3 hence the CPU willexecute P3over P2.
Meanwhile the execution of P3, All the processes get available in the ready queue. The Process
with the lowest priority number will be given the priority. Since P6 has priority number assigned
as 4 hence it will be executed just after P3.
After P6, P4 has the least priority number among the available processes; itwill get executed for
the whole burst time.

Since all the jobs are available in the ready queue hence Allthe Jobs willget executed according
to their priorities. If two jobs have similar priority number assigned to them, the one with the least
arrival tine will be executed.

P1 P3 P6 P4 P2 P5 P7

3 11 13 18 27 37

From the GANTT Chart prepared, we can determine the completion time of every process. The
turnaround time, waiting time and response time will be determined.

1. Tun Around Time= Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time
Process Priority Arrival Burst. Completion Turnaround Waiting
Time Time Time Time Time

2 3 3

2 6 18 16 11

3 3 1 6 2

2 13 9
6 27 21 12

6 4 4 11 6 2

7 7 37 30

Avg Waiting Time (0+||+2+7+12+2+18)/7 52/7 units

Example Prlority Scheduling Preemptive


Consider the set of processes with arrival time(in milliseconds), CPU burst time (in milliseconds), and priority(0is the
highest priority) shown below. None of the processes have i/O burst time.
Calculate the average waiting time (in milliseconds) of allthe processes using preemptive priority scheduling algorithm

Process Arival Tine Burst Tune Prionty


P 0 11
P 28
P, 12 2
1
P 10
16 4
Ps
P1 P4 P2 P4 P1 P3
2 40 49 51 67
33

Walting Time for p1= 38 ms

Walting Time for p2= 0 ms


Waiting Time for p3= 37 ms
Waiting Time for p4= 28 ms
Waiting Time for p5 42 mns
=(38+0+37+28+42)/5
Average Wating Time
=145/S
=29 ns
Round-Robin Scheduling
1. The round-robin (RR) scheduling
algorithm is designed
especially for timesharing systems.
2 Itis similar toFCFS
scheduling, but preemption is added to enable
the system to switch between processes.
2 Asmall unit of time, calledatime
quantum or time slice, is
defined. Atime quantum is generally from 10 to 100
milliseconds in length.
4. The ready queue is treated as a circular queue.
S. The CPU scheduler goes around the
ready queue,
CPUtocach process for a time interval of up to 1 allocating the
time quantum.

P1 \P2 P3
PA CPU Scheduler

P10 cIRCULAR QUEUE


P5
P9 P6
P7
P8

Example:
Consider the set of 5 processes whose arrival time and burst time are
given below
Process ld Arrival time Burst time
P1

P2 1

P3
1

P4 3

P5

If the CPUscheduling policy is Round Robin with time


quantum =2 unit, calculate the
average waiting time and average turn around time.
2 4 5 9 12 13 14
P2 P3 P4 P5 P2 P1 P5

Gantt Chart
SuNet NutzrSng
Page Replacement Algorithms in Operating Systems

1. First In First Out (FIFO): This is the simplest page replacement algorithm. In this algorithm,
the operating system keeps track ofall pages in the memory ina queue, the oldest page is in
the front of the queue. When a page needs to be replaced page in the front of the queue s
selected for removal.

Example 1: Consider page reference string 1, 3,0. 3, 5, 6, 3with 3page frames.Find the number
of page faults.

Page 1, 3, 0, 3, 5,6, 3
reference

1 3 0 3 5 6

0 0 3

3 3 3 3 6 6

1 1 5 5 5

Miss Miss Miss Hit Miss Miss Miss

Total Page Fault = 6


>3
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots
Page Faults.

when 3 comes, it is already in memory so > 0 Page Faults. Then 5 comes, it is not available in
memory so it replaces the oldest page slot i.e 1. >1Page Fault. 6 comes, it is also not
available in memory so it replaces the oldest page slot i.e3>1Page Fault. Finally, when 3
come it is not available so it replaces 0 1page fault.

2.Optimal Page replacenment: In this algorithm, pages are replaced which would not be used
for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frame. Find number of page fault.
Page No. of Page frame - 4
reference 7,0.1,2,0,3.0 4.2,3,0,3,2,3

3 3 2 3
7 0 1 2 3 4 2
2 2 2 2
2 2 |2 2 2 2
1 1 4 4 4 4
1
0 0 0 0
7 7 3 3 3 3 3 3 3

Miss Hit Hit Hit Hit Hit Hit


Miss Miss Miss Miss Hit Miss Hit

Total Page Fault =6


Initially, all siots are empty, so when 7012 are allocated to the empty slots ->4 Page faults
0 is already there so >0Page fault. when 3 came it will take the place of 7because it is not
used for the longest duration of time in the future,>1 Page faut. Ois already there so ->0
Page fault. 4 will takes placeofl >1Page Fault.
Now for the further page reference string >0Page fault because they are already available
in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests. The use of Optimal Page replacement is to set up a benchmark so
that other replacement algorithms can be analysed against it.
3. Least Recently Used: Inthis algorithm, page will be replaced which is least recently used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frames. Find number of page faults.

Page 7,0,1,2,0,3,0,4,2,3,0,3,2,3 No. of Page frame - 4


reference

2 3 0 4 2 3 3 2 3
7
2 2 2 2 2 2 2 2 2 2|
1 1 1 4 4 4| 4 4

0 0 0
7 3 3 3 3 3 3 3

Miss Miss Miss Hit Miss Hit Miss Hi Hit Hit Hit Hit Hit
Miss
Total Page Fault = 6

Here LRU has same number of page fault as optimal but it may differ according to question.
Initially, all slots are empty, so when 7 012are allocated to the empty slots ->4 Page faults
0 is already their so ->0 Page fault. when 3 camne it will take the place of 7because it is least
recently used>1 Page fault
Ois already in memory so >0Page fault.
AVOIdance is kind of futuristic in nature, By using strategy of"A voidance", we must assume.
We need to ensure that all information about resources which
process will need are known to
US pror to execution of the process. We use Banker'salgorithm toavoid deadlock.
2) Deadlock detection and recovery: Let deadlock occur, then do
occurred. pre-emption to handle t once
3) Ignore the problem altogether: Ifdeadlock is very rare, then let it happen and reboot the
system. This is the approach that both Windows and UNIX take.

Bankers Algorithm:
Suppose the number of account holders in a particular bank is 'n', and the total money in a bank is
"T. Ifan account holder applies for a loan; first, the bank subtracts the loan amount from full
cash and then estimates the cash difference is greater than T to approve the loan amount.
These steps are taken because if another person applies for a loan or withdraws some amount
from the bank, it helps the bank manage and operate all things without any restriction in the
functionality of the banking system.

Similarly, it works in an operating system. When a new process is created in a computer system,
the process must provide all types of information to the operating system like upcoming processes,
requests for their resources, counting them, and delays. Based on these criteria, the operating
system decides which process sequence should be executed or waited so that no deadkock occurs
in a system Therefore, it is also known as deadlock avoidance algorithm or deadlock detection in
the operating system.

Advantages
Following are the essential characteristics of the Banker's algorithm:

It contains various resources that mneet the requirements of each process.


Each process should provide information to the operating system for upcoming resource requests,
the number of resources, and how long the resources will be held.
resource in the
It helps the operating system manage and control process requests for each type of
computer system.

Disadvantages
It requires a fixed number of processes, and no additional processes can be started in the system
while executing the process.
while
The algorithm does no longer allows the processes to exchange its maximum needs
processing its tasks.
system.
Each process must know and state their maximum resource requirement in advance for the

When working with a banker's algorithm, it requests to know about three things:

How much each process can request for each resource in the system. It is denoted by the [MAX]
request.
How much cach process is currently holding each resource ina system. It is denoted by the
[ALLOCATED] resource.
It represents the number of cach resource currently available in the system. It is denoted by the
[AVAILABLE] resource.
Exampke:

Ltus conider the foliowing for enderstanding the banker' alyorithm:

Processs Allocation A B( Mas A B Availabie A BC

433 210

P! 212 322

401 902

P3 020 753

P4 112 112

Solution:

1. The Cotent of the need matrix can be calculated by using the formula given below:
Nced = Max - Allocation

Process Need

L
A B C
Po 3 2
P
P 5
P: 7 3 3
P 0 2. Let us now check for the
safe state.

Safe sequence:
1 For process PO, Need =(3, 2, 1) and

Avaitabie--2,1,0)
3Fer Pee 2Need(5,6. I)
Avastabie (4, 2. )
Need c Avasiabie False

So,the stem will mve to the e proxe


4 For Prcess P3, Need (7. )

Availabje (4, 2, 2)
Need cAvailahie False

Se, the sysáen will xve o the ext prxcess

S For Frcess P4, Need - (0,0, 0)


Avaiiatie (4, 2, 2)

Neod Avaiiabie Truc


Kequest of P4 is graniod
AvBiistie =Avaabie Aocata

-(4, 2, 2}*0, 1, 2)
-(5, 3, 4) w, (New Availatsle)
6 Now agaui check kor Proves P2, Need (5,0, I)

Avaiiabie - (5, 3, 4)

Need c Avaiiabie rue


Request of P2 is granted.
Available = Available + Allocation

= (5, 3,4) + (4. 0, 1)

= (9, 3, 5) now, (New Available)


7.Now again check for Process P3, Need = (7, 3,3)
Available=(9, 3, 5)
Need <=A vailable = Tue

The request for P3 is granted.


Available = Available +Allocation

=(9, 3, 5) + (0, 2, 0) =(9,5, 5)


8. Now again check for Process P0, = Need (3, 2, 1)

- Available (9, 5, 5)
Need <= Available = True

So, the request will be granted to P0.


Safe sequence: < P1, P4, P2, P3, PO>

The system allocates allthe needed resources to each process. So, we can say that the system
is ina safe state.

3. The total amount of resources will be calculated by the following formula:


The total amount of resources= sum of columns of allocation + Available

= [8 57] + [2 1 0] - [10 6 7]

4.3 ostrich algorithm:

An algorithm krown as the Ostrich algorithm ignores deadlocks. The operating system assumes no
chances of running intoa stalemate when using this method. It's comparable to howan ostrich will
bury its head in the ground and act as if nothing is wrong. This approach is employed when
preventing dcadkocks is expensive, and they don't happen often.

The Windows and Unix operating systems use this algorithm. Restarting the system resolves the
deadlocks.

You might also like