0% found this document useful (0 votes)
15 views45 pages

OS 2nd

The document outlines four experiments focused on different CPU scheduling algorithms: FCFS, Preemptive SJF, Non-Preemptive SJF, and Preemptive Priority Scheduling. Each experiment includes a description of the algorithm, key points, an algorithmic approach, source code, and sample input/output. The experiments illustrate how processes are managed in an operating system based on their arrival times, burst times, and priorities.

Uploaded by

ag5250715
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)
15 views45 pages

OS 2nd

The document outlines four experiments focused on different CPU scheduling algorithms: FCFS, Preemptive SJF, Non-Preemptive SJF, and Preemptive Priority Scheduling. Each experiment includes a description of the algorithm, key points, an algorithmic approach, source code, and sample input/output. The experiments illustrate how processes are managed in an operating system based on their arrival times, burst times, and priorities.

Uploaded by

ag5250715
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/ 45

Experiment - 1

Aim:- Implement FCFS(First Come First Serve) Scheduling.


Descript ion:-
FCFS is a simple and st raight forward scheduling algorit hm used in operat ing
syst ems for process management . In FCFS, processes are execut ed in t he order
t hey arrive in t he ready queue, wit hout preempt ion. The process t hat arrives
fi rst get s execut ed fi rst , and t he one t hat arrives lat er has t o wait unt il all
previous processes are complet ed.
Key Point s:
● Non- preempt ive: Once a process st art s execut ing, it runs t o complet ion.
● Simple t o implement , but it can lead t o long wait ing t imes, especially if
short processes are st uck behind longer ones.
Algorit hm:-
Sort t he processes by arrival t ime (earliest fi rst ).
Init ialize t he current t ime t o 0 .
For each process:
● St art t ime = current t ime (if t he process arrives aft er current t ime, set
it t o t he arrival t ime).
● Complet ion t ime = st art t ime + burst t ime.
● Updat e current t ime = complet ion t ime.
=> Calculat e wait ing t ime = complet ion t ime - arrival t ime - burst t ime.
=> Calculat e t urnaround t ime = complet ion t ime - arrival t ime.
=> Repeat unt il all processes are scheduled.
Source Code:-
# include< st dio.h>
int main()
{
int n,avwt =0 ,avt at =0 ,i,j;
print f(" Ent er number of processes : " );
scanf(" % d" ,&n);
int bt [ n] ,wt [ n] ,t at [ n] ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


print f(" Ent er Process Burst Time\n" );
for(i=0 ;i< n;i++)
{
print f(" P[ % d] :" ,i+1);
scanf(" % d" ,&bt [ i] );
}
wt [ 0 ] =0 ;
for(i=1;i< n;i++)
{
wt [ i] =wt [ i- 1] +bt [ i- 1] ;
}
print f(" Process\t \t Burst Time\t Wait ing Time\t Turnaround Time" );
for(i=0 ;i< n;i++)
{
t at [ i] =bt [ i] +wt [ i] ;
avwt +=wt [ i] ;
avt at +=t at [ i] ;
print f(" \nP[ % d] \t \t % d\t \t % d\t \t % d" ,i+1,bt [ i] ,wt [ i] ,t at [ i] );
}
avwt /=i;
avt at /=i;
print f(" \n\nAverage Wait ing Time: % d" ,avwt );
print f(" \nAverage Turnaround Time: % d" ,avt at );
ret urn 0 ;
}
Input :-
Ent er number of processes : 3
Ent er Process Burst Time
P[ 1] :5
P[ 2] :3

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


P[ 3] :8

Out put :-
Process Burst Time Wait ing Time Turnaround Time
P[ 1] 5 05
P[ 2] 3 58
P[ 3] 8 816
Average Wait ing Time: 4
Average Turnaround Time: 9
-------------------------------------------------------------------------------------
---------------------------

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Experiment - 2
Aim:- Implement Preempt ive SJF(Short est Job First ) Scheduling.
Descript ion:-
Preempt ive Short est Job First (SJF) is a CPU scheduling algorit hm where t he
process wit h t he short est remaining burst t ime is select ed for execut ion next .
If a new process arrives wit h a short er remaining burst t ime t han t he current
process, t he current process is preempt ed, and t he new process is given t he
CPU. This is also known as Short est Remaining Time First (SRTF).
Key Point s:
● Preempt ive: A process can be int errupt ed if a new process wit h a
short er burst t ime arrives.
● The process wit h t he short est burst t ime left is always execut ed next .
Algorit hm:-
Preempt ive SJF Algorit hm in Easy St eps:
1. Sort processes by arrival t ime.
2. Init ialize: Set current t ime = 0 .
3. For each t ime unit (from 0 t o t he maximum t ime):
o Select t he process wit h t he short est remaining burst t ime t hat has
arrived by t he current t ime.
o If a new process arrives wit h a short er remaining burst t ime t han
t he current process, preempt t he current process and execut e t he
new process.
o If no process is available, increase t he current t ime.
4. Repeat unt il all processes are complet ed.
5. Calculat e:
o Wait ing Time = Turnaround Time - Burst Time
o Turnaround Time = Complet ion Time - Arrival Time
Source Code:-
# include < iost ream>

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


using namespace st d;
int main() {
int n;
cout < < " Ent er t he number of processes: " ;
cin > > n;
int burst [ n] , complet ion[ n] , t urnaround[ n] , wait ing[ n] ;
// Input burst t imes
for (int i = 0 ; i < n; i++) {
cout < < " Ent er burst t ime for process " < < (i + 1) < < " : " ;
cin > > burst [ i] ;
}
// Sort burst t imes using Bubble Sort
for (int i = 0 ; i < n - 1; i++) {
for (int j = 0 ; j < n - i - 1; j++) {
if (burst [ j] > burst [ j + 1] ) {
int t emp = burst [ j] ;
burst [ j] = burst [ j + 1] ;
burst [ j + 1] = t emp;
}
}
}
// Calculat e Complet ion Time
int current Time = 0 ;
for (int i = 0 ; i < n; i++) {
current Time += burst [ i] ;
complet ion[ i] = current Time;
t urnaround[ i] = complet ion[ i] ; // Turnaround Time = Complet ion Time
wait ing[ i] = t urnaround[ i] - burst [ i] ; // Wait ing Time = Turnaround Time -
Burst Time
}

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


// Out put result s
cout < < " \nProcess\t Burst \t Complet ion\t Turnaround\t Wait ing\n" ;
double t ot alTurnaround = 0 , t ot alWait ing = 0 ;
for (int i = 0 ; i < n; i++) {
cout < < (i + 1) < < " \t "
< < burst [ i] < < " \t "
< < complet ion[ i] < < " \t \t "
< < t urnaround[ i] < < " \t \t "
< < wait ing[ i] < < " \n" ;
t ot alTurnaround += t urnaround[ i] ;
t ot alWait ing += wait ing[ i] ;
}
// Calculat e and print average t imes
cout < < " \nAverage Turnaround Time: " < < t ot alTurnaround / n < < endl;
cout < < " Average Wait ing Time: " < < t ot alWait ing / n < < endl;
ret urn 0 ;
}
Input :-
Ent er t he number of processes: 4
Ent er burst t ime for process 1: 8
Ent er burst t ime for process 2: 4
Ent er burst t ime for process 3: 9
Ent er burst t ime for process 4: 5
Out put :-
Process Burst Complet ion Turnaround Wait ing
1 4 4 4 0
2 5 9 9 4
3 8 17 17 9
4 9 26 26 17

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Average Turnaround Time: 14
Average Wait ing Time: 7.5
-------------------------------------------------------------------------------------
---------------------------

Experiment - 3
Aim:- Implement Non Preempt ive Short est Job fi rst Scheduling.
Descript ion:-
Non- preempt ive Short est Job First (SJF) is a scheduling algorit hm where t he
process wit h t he short est burst t ime (CPU t ime required) is select ed for
execut ion next . Once a process st art s execut ing, it runs t o complet ion wit hout
int errupt ion. This algorit hm does not allow preempt ion; a running process can
only be int errupt ed when it fi nishes it s execut ion.
Key Point s:
● Non- preempt ive: A process runs t o complet ion once it st art s.
● The short est burst t ime is select ed next from t he ready queue.
Algorit hm:-
1. Sort t he processes by arrival t ime.
2. Init ialize: Set current t ime = 0 .
3. At each moment :
o From t he ready queue, select t he process wit h t he short est burst
t ime t hat has arrived.
o Execut e t he select ed process unt il complet ion.
4. Once a process complet es, updat e t he current t ime and check for new
processes in t he queue.
5. Repeat st eps unt il all processes are complet ed.
6. Calculat e:
o Wait ing Time = Turnaround Time - Burst Time
o Turnaround Time = Complet ion Time - Arrival Time

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Source Code:-
# include< st dio.h>
int main()
{
int i,n,p[ 10 ] ={1,2,3,4,5,6,7,8,9,10 },min,k=1,bt ime=0 ;
int bt [ 10 ] ,t emp,j,arr[ 10 ] ,wait [ 10 ] ,t urn[ 10 ] ,t a=0 ,sum=0 ;
fl oat wavg=0 ,t avg=0 ,t sum=0 ,wsum=0 ;
print f(" \nEnt er t he No. of processes :" );
scanf(" % d" ,&n);
print f(" Ent er t he arrival t ime and burst t ime of t he processes:\n" );
for(i=0 ;i< n;i++)
scanf(" % d % d" ,&arr[ i] ,&bt [ i] );
for(i=0 ;i< n;i++)
{
for(j=0 ;j< n;j++)
{
if(arr[ i] < arr[ j] )
{
t emp=p[ j] ;
p[ j] =p[ i] ;
p[ i] =t emp;
t emp=arr[ j] ;
arr[ j] =arr[ i] ;
arr[ i] =t emp;
t emp=bt [ j] ;
bt [ j] =bt [ i] ;
bt [ i] =t emp;
}
}
}

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


for(j=0 ;j< n;j++)
{
bt ime=bt ime+bt [ j] ;
min=bt [ k] ;
for(i=k;i< n;i++)
{
if (bt ime> =arr[ i] && bt [ i] < min)
{
t emp=p[ k] ;
p[ k] =p[ i] ;
p[ i] =t emp;
t emp=arr[ k] ;
arr[ k] =arr[ i] ;
arr[ i] =t emp;
t emp=bt [ k] ;
bt [ k] =bt [ i] ;
bt [ i] =t emp;
}
}
k++;
}
wait [ 0 ] =0 ;
for(i=1;i< n;i++)
{
sum=sum+bt [ i- 1] ;
wait [ i] =sum- arr[ i] ;
wsum=wsum+wait [ i] ;
}
wavg=(wsum/n);
for(i=0 ;i< n;i++)

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


{
t a=t a+bt [ i] ;
t urn[ i] =t a- arr[ i] ;
t sum=t sum+t urn[ i] ;
}
t avg=(t sum/n);
print f(" \nProcess\t Burst \t Arrival\t Wait ing\t Turn- around" );
for(i=0 ;i< n;i++)
print f(" \n p% d\t % d\t % d\t \t % d\t \t \t % d" ,p[ i] ,bt [ i] ,arr[ i] ,wait [ i] ,t urn[ i] );
print f(" \n\nAVERAGE WAITING TIME : % f" ,wavg);
print f(" \nAVERAGE TURN AROUND TIME : % f" ,t avg);
ret urn 0 ;
}
Input :-
Ent er t he No. of processes :3
Ent er t he arrival t ime and burst t ime of t he processes:
0
10
1
1
1
2
Out put :-
Process Burst Arrival Wait ing Turn- around
p1 10 0 0 10
p2 1 1 9 10
p3 2 1 10 12

AVERAGE WAITING TIME : 6.333333


AVERAGE TURN AROUND TIME : 10 .666667

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


-------------------------------------------------------------------------------------
---------------------------

Experiment - 4
Aim:- Implement Preempt ive Priorit y Scheduling.
Descript ion:-
Preempt ive Priorit y Scheduling is a CPU scheduling algorit hm where each
process is assigned a priorit y. The CPU is allocat ed t o t he process wit h t he
highest priorit y (lowest priorit y number). If a new process arrives wit h a higher
priorit y t han t he current ly running process, t he CPU is preempt ed, and t he new
process is execut ed.
Key feat ures:
● Preempt ion: A running process can be int errupt ed if a higher- priorit y
process arrives.
● Priorit y: Each process is assigned a priorit y value (usually, lower
numbers indicat e higher priorit y).
● Non- Preempt ion: If a process wit h t he same priorit y arrives, it has t o
wait for t he current process t o complet e.
Algorit hm:-
1. Init ializat ion:
o For each process, assign a priorit y.
o Keep t rack of t he arrival t ime and burst t ime for each process.
2. Ready Queue:
o Maint ain a ready queue t hat holds processes wait ing t o be execut ed.
3. Process Arrival:
o As each process arrives, check it s priorit y.

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


o If a process has a higher priorit y t han t he current ly running process,
preempt t he current process and assign t he CPU t o t he new
process.
4. CPU Allocat ion:
o The process wit h t he highest priorit y (smallest priorit y value) get s
execut ed.
o The process will run unt il eit her:
● It complet es.
● A higher- priorit y process arrives.
5. Repeat St eps:
o As long as t here are processes left in t he ready queue, repeat st ep 3
and 4.
6. Complet ion:
o Once all processes are complet ed, calculat e t heir t urnaround t ime
and wait ing t ime.
Source Code:-
# include < iost ream>
# include < climit s> // For INT_ MAX
using namespace st d;
int main() {
int n;
cout < < " Ent er t he number of processes: " ;
cin > > n;
int t imeQuant um;
cout < < " Ent er t he t ime Quant um: " ;
cin> > t imeQuant um;
int burst [ n] , remaining[ n] , priorit y[ n] , complet ion[ n] , wait ing[ n] ,
t urnaround[ n] ;
bool complet ed[ n] = {false}; // Tracks if a process is complet ed
// Input burst t imes and priorit ies for each process
for (int i = 0 ; i < n; i++) {

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


cout < < " Ent er burst t ime for process " < < (i + 1) < < " : " ;
cin > > burst [ i] ;
cout < < " Ent er priorit y for process " < < (i + 1) < < " : " ;
cin > > priorit y[ i] ;
remaining[ i] = burst [ i] ; // Init ialize remaining burst t ime
}
int complet edCount = 0 , current Time = 0 ;
// Preempt ive Priorit y Scheduling wit h Time Quant um
while (complet edCount < n) {
int highest Priorit y = INT_ MAX;
int processToRun = - 1;
// Find process wit h t he highest priorit y (lowest number) t hat has not
complet ed
for (int i = 0 ; i < n; i++) {
if (!complet ed[ i] && priorit y[ i] < highest Priorit y) {
highest Priorit y = priorit y[ i] ;
processToRun = i;
}
}
if (processToRun == - 1) {
// No process left t o schedule
current Time++;
cont inue;
}
// Execut e t he select ed process for t he t ime quant um (2 unit s or remaining
t ime)
int t imeToExecut e = (remaining[ processToRun] < t imeQuant um) ?
remaining[ processToRun] : t imeQuant um;
remaining[ processToRun] - = t imeToExecut e;
current Time += t imeToExecut e;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


// If t he process is complet ed
if (remaining[ processToRun] == 0 ) {
complet ed[ processToRun] = t rue;
complet ion[ processToRun] = current Time;
complet edCount ++; }
}
// Calculat e Turnaround Time and Wait ing Time
for (int i = 0 ; i < n; i++) {
t urnaround[ i] = complet ion[ i] ; // TAT = Complet ion t ime (since arrival t ime
is 0 )
wait ing[ i] = t urnaround[ i] - burst [ i] ; // WT = TAT - Burst t ime
}
// Out put result s
cout < < " \nProcess\t Burst \t Priorit y\t Complet ion\t Turnaround\t Wait ing\n" ;
double t ot alTurnaround = 0 , t ot alWait ing = 0 ;
for (int i = 0 ; i < n; i++) {
cout < < (i + 1) < < " \t "
< < burst [ i] < < " \t "
< < priorit y[ i] < < " \t \t "
< < complet ion[ i] < < " \t \t "
< < t urnaround[ i] < < " \t \t "
< < wait ing[ i] < < " \n" ;
t ot alTurnaround += t urnaround[ i] ;
t ot alWait ing += wait ing[ i] ; }
// Calculat e and print average t imes
cout < < " \nAverage Turnaround Time: " < < t ot alTurnaround / n < < endl;
cout < < " Average Wait ing Time: " < < t ot alWait ing / n < < endl;
ret urn 0 ;
}
Input :-

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Ent er t he number of processes: 4
Ent er t he t ime Quant um: 2
Ent er burst t ime for process 1: 5
Ent er priorit y for process 1: 3
Ent er burst t ime for process 2: 3
Ent er priorit y for process 2: 1
Ent er burst t ime for process 3: 2
Ent er priorit y for process 3: 4
Ent er burst t ime for process 4: 4
Ent er priorit y for process 4: 2
Out put :-
Process Burst Priorit y Complet ion Turnaround Wait ing
1 5 3 12 12 7
2 3 1 3 3 0
3 2 4 14 14 12
4 4 2 7 7 3
Average Turnaround Time: 9
Average Wait ing Time: 5.5
-------------------------------------------------------------------------------------
---------------------------
Experiment - 5
Aim:- Implement Non Preempt ive Priorit y Scheduling.
Descript ion:-
Non- Preempt ive Priorit y Scheduling is a CPU scheduling algorit hm where each
process is assigned a priorit y. In t his met hod, once a process st art s execut ion,
it runs t o complet ion, and no ot her process can int errupt it . The process wit h
t he highest priorit y (lowest priorit y number) is select ed for execut ion. If
mult iple processes have t he same priorit y, t hey are scheduled based on t heir
arrival t imes.
Key feat ures:
● Non- preempt ion: Once a process st art s execut ing, it cannot be
int errupt ed unt il it fi nishes.

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


● Priorit y: Each process has a priorit y value (lower values mean higher
priorit y).
● Execut ion: The CPU is given t o t he process wit h t he highest priorit y
from t he ready queue.
Algorit hm:-
Algorit hm of Non- Preempt ive Priorit y Scheduling
Here is a st ep- by- st ep algorit hm for Non- Preempt ive Priorit y Scheduling:
1. Init ializat ion:
o For each process, assign a priorit y, burst t ime, and arrival t ime.
2. Ready Queue:
o Maint ain a ready queue t hat holds t he processes wait ing t o be
execut ed.
3. Process Select ion:
o Select t he process wit h t he highest priorit y (lowest priorit y
number) from t he ready queue.
o If t wo processes have t he same priorit y, select t he one t hat arrived
fi rst .
4. Execut ion:
o Allocat e CPU t o t he select ed process and let it execut e t o
complet ion.
o Once t he process fi nishes, remove it from t he ready queue.
5. Repeat St eps:
o Aft er t he complet ion of a process, repeat st ep 3 and 4 for t he next
process in t he ready queue.
6. Complet ion:
o The process cont inues unt il all processes have been execut ed.
Source Code:-
# include< st dio.h>
int main()
{
int burst _ t ime[ 20 ] , process[ 20 ] , wait ing_ t ime[ 20 ] , t urnaround_ t ime[ 20 ] ,
priorit y[ 20 ] ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


int i, j, limit , sum = 0 , posit ion, t emp;
fl oat average_ wait _ t ime, average_ t urnaround_ t ime;
print f(" Ent er Tot al Number of Processes:\t " );
scanf(" % d" , &limit );
print f(" \nEnt er Burst Time and Priorit y For % d Processes\n" , limit );
for(i = 0 ; i < limit ; i++)
{
print f(" \nProcess[ % d] \n" , i + 1);
print f(" Process Burst Time:\t " );
scanf(" % d" , &burst _ t ime[ i] );
print f(" Process Priorit y:\t " );
scanf(" % d" , &priorit y[ i] );
process[ i] = i + 1;
}
for(i = 0 ; i < limit ; i++)
{
posit ion = i;
for(j = i + 1; j < limit ; j++)
{
if(priorit y[ j] < priorit y[ posit ion] )
{
posit ion = j;
}
}
t emp = priorit y[ i] ;
priorit y[ i] = priorit y[ posit ion] ;
priorit y[ posit ion] = t emp;
t emp = burst _ t ime[ i] ;
burst _ t ime[ i] = burst _ t ime[ posit ion] ;
burst _ t ime[ posit ion] = t emp;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


t emp = process[ i] ;
process[ i] = process[ posit ion] ;
process[ posit ion] = t emp;
}
wait ing_ t ime[ 0 ] = 0 ;
for(i = 1; i < limit ; i++)
{
wait ing_ t ime[ i] = 0 ;
for(j = 0 ; j < i; j++)
{
wait ing_ t ime[ i] = wait ing_ t ime[ i] + burst _ t ime[ j] ;
}
sum = sum + wait ing_ t ime[ i] ;
}
average_ wait _ t ime = sum / limit ;
sum = 0 ;
print f(" \nProcess ID\t \t Burst Time\t Wait ing Time\t Turnaround Time\n" );
for(i = 0 ; i < limit ; i++)
{
t urnaround_ t ime[ i] = burst _ t ime[ i] + wait ing_ t ime[ i] ;
sum = sum + t urnaround_ t ime[ i] ;
print f(" \nProcess[ % d] \t \t % d\t \t % d\t \t % d\n" , process[ i] ,
burst _ t ime[ i] , wait ing_ t ime[ i] , t urnaround_ t ime[ i] );
}
average_ t urnaround_ t ime = sum / limit ;
print f(" \nAverage Wait ing Time:\t % f" , average_ wait _ t ime);
print f(" \nAverage Turnaround Time:\t % f\n" , average_ t urnaround_ t ime);
ret urn 0 ;
}
Input :-

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Ent er Tot al Number of Processes: 4
Ent er Burst Time and Priorit y For 4 Processes
Process[ 1]
Process Burst Time: 5
Process Priorit y: 3
Process[ 2]
Process Burst Time: 3
Process Priorit y: 1
Process[ 3]
Process Burst Time: 2
Process Priorit y: 4
Process[ 4]
Process Burst Time: 4
Process Priorit y: 2
Out put :-
Process ID Burst Time Wait ing Time Turnaround Time

Process[ 2] 3 0 3

Process[ 4] 4 3 7

Process[ 1] 5 7 12

Process[ 3] 2 12 14

Average Wait ing Time: 5.0 0 0 0 0 0


Average Turnaround Time: 9.0 0 0 0 0 0

-------------------------------------------------------------------------------------
---------------------------

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Experiment - 6
Aim:- Implement Round- Robin CPU Scheduling.
Descript ion:-
Round Robin (RR) is a CPU scheduling algorit hm t hat allocat es a fi xed t ime
slice (or quant um) t o each process in a cyclic order. Once a process’s t ime slice
expires, it is preempt ed and placed at t he end of t he ready queue, and t he next
process get s execut ed. This cont inues unt il all processes are complet ed.
Key feat ures:
● Time Slice (Quant um): Each process is assigned a small fi xed t ime unit
(quant um) t o execut e.
● Cyclic Order: Processes are execut ed in a circular order, wit h each
process get t ing a t urn in t he queue.
● Preempt ion: If a process doesn’t fi nish wit hin it s t ime slice, it is
preempt ed and ret urned t o t he ready queue.
Algorit hm:-
Algorit hm of Round Robin Scheduling
Here is a st ep- by- st ep algorit hm for Round Robin Scheduling:
1. Init ializat ion:
o Assign each process a t ime slice (quant um).
o Maint ain a ready queue t hat holds processes wait ing t o be execut ed.
o Init ialize each process’s remaining burst t ime (t ot al burst t ime
minus t ime used).
2. Ready Queue:
o Insert processes int o t he ready queue in t he order t hey arrive.
3. Execut ion:
o The process at t he front of t he ready queue is select ed t o run.
o If t he process's remaining burst t ime is less t han or equal t o t he
t ime slice (quant um), t he process runs t o complet ion.
o If t he process’s remaining burst t ime is great er t han t he quant um,
t he process is preempt ed and placed at t he end of t he queue aft er
execut ing for t he t ime slice.
4. Repeat St eps:

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


o This process cont inues cyclically for each process in t he queue unt il
all processes are complet ed.
5. Complet ion:
o When all processes have complet ed t heir execut ion, t he algorit hm
t erminat es.
Source Code:-
# include< st dio.h>
# include< conio.h>
void main()
{
int i, NOP, sum=0 ,count =0 , y, quant , wt =0 , t at =0 , at [ 10 ] , bt [ 10 ] , t emp[ 10 ] ;
fl oat avg_ wt , avg_ t at ;
print f(" Tot al number of process in t he syst em: " );
scanf(" % d" , &NOP);
y = NOP;
for(i=0 ; i< NOP; i++)
{
print f(" \n Ent er t he Arrival and Burst t ime of t he Process[ % d] \n" , i+1);
print f(" Arrival t ime is: \t " );
scanf(" % d" , &at [ i] );
print f(" \nBurst t ime is: \t " );
scanf(" % d" , &bt [ i] );
t emp[ i] = bt [ i] ;
}
print f(" Ent er t he Time Quant um for t he process: \t " );
scanf(" % d" , &quant );
print f(" \n Process No \t \t Burst Time \t \t TAT \t \t Wait ing Time " );
for(sum=0 , i = 0 ; y!=0 ; )
{
if(t emp[ i] < = quant && t emp[ i] > 0 )

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


{
sum = sum + t emp[ i] ;
t emp[ i] = 0 ;
count =1;
}
else if(t emp[ i] > 0 )
{
t emp[ i] = t emp[ i] - quant ;
sum = sum + quant ;
}
if(t emp[ i] ==0 && count ==1)
{
y- - ;
print f(" \nProcess No[ % d] \t \t % d\t \t \t \t % d\t \t \t % d" , i+1, bt [ i] , sum-
at [ i] , sum- at [ i] - bt [ i] );
wt = wt +sum- at [ i] - bt [ i] ;
t at = t at +sum- at [ i] ;
count =0 ;
}
if(i==NOP- 1)
{
i=0 ;
}
else if(at [ i+1] < =sum)
{
i++;
}
else
{
i=0 ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


}
}
avg_ wt = wt * 1.0 /NOP;
avg_ t at = t at * 1.0 /NOP;
print f(" \n Average Turn Around Time: \t % f" , avg_ wt );
print f(" \n Average Wait ing Time: \t % f" , avg_ t at );
get ch();
}
Input :-
Tot al number of process in t he syst em: 4
Ent er t he Arrival and Burst t ime of t he Process[ 1]
Arrival t ime is: 0
Burst t ime is: 5
Ent er t he Arrival and Burst t ime of t he Process[ 2]
Arrival t ime is: 1
Burst t ime is: 3
Ent er t he Arrival and Burst t ime of t he Process[ 3]
Arrival t ime is: 2
Burst t ime is: 8
Ent er t he Arrival and Burst t ime of t he Process[ 4]
Arrival t ime is: 3
Burst t ime is: 6
Ent er t he Time Quant um for t he process: 2
Out put :-
Process No Burst Time TAT Wait ing Time
Process No[ 2] 3 10 7
Process No[ 1] 5 16 11
Process No[ 4] 6 17 11
Process No[ 3] 8 20 12
Average Turn Around Time: 10 .250 0 0 0

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Average Wait ing Time: 15.750 0 0 0
-------------------------------------------------------------------------------------
---------------------------

Experiment - 7
Aim:- Implement Banker’s Algorit hm .
Descript ion:-
The Banker's Algorit hm is a deadlock avoidance algorit hm used in operat ing
syst ems t o allocat e resources t o processes in such a way t hat t he syst em
never ent ers an unsafe st at e. It checks whet her a resource allocat ion request
can be grant ed safely, meaning it ensures t hat t here is enough remaining
resources t o fulfi ll t he fut ure needs of all processes in t he syst em. The
algorit hm get s it s name because it simulat es t he way a banker allocat es loans
t o cust omers, ensuring t hat enough resources are available for all processes t o
event ually complet e.
Algorit hm:-
Banker's Algorit hm St eps :-
1. Init ializat ion:
o Available[ ] : The available resources in t he syst em.
o Max[ ] [ ] : Maximum resources required by each process.
o Allocat ion[ ] [ ] : The resources current ly allocat ed t o each process.
o Need[ ] [ ] : Remaining resources each process needs t o complet e
(calculat ed as Need[ i] [ j] = Max[ i] [ j] - Allocat ion[ i] [ j] ).
2. Request ing Resources: When a process request s resources, t he syst em
checks if t he request can be grant ed:
o St ep 1: Check if t he request ed resources are less t han or equal t o
t he process's remaining needs.

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


● If Request [ i] ≤ Need[ i] , cont inue. If not , reject t he request .
o St ep 2: Check if t he request ed resources are less t han or equal t o
t he available resources.
● If Request [ i] ≤ Available[ ] , cont inue. If not , t he request must
wait .
3. Pret end t he Request is Grant ed:
o St ep 3: Temporarily allocat e t he request ed resources t o t he process:
● Updat e Available[ ] : Available = Available - Request [ i]
● Updat e Allocat ion[ i] : Allocat ion[ i] = Allocat ion[ i] +
Request [ i]
● Updat e Need[ i] : Need[ i] = Need[ i] - Request [ i]
4. Safet y Check: Aft er grant ing t he request , t he syst em performs a safet y
check t o ensure t he syst em remains in a safe st at e.
o St ep 4: Check if a safe sequence exist s:
● Find a process t hat can fi nish (i.e., whose remaining needs
are less t han or equal t o available resources).
● Simulat e t he process complet ion by updat ing Available[ ]
and marking t he process as fi nished.
● Repeat unt il all processes can fi nish.
o If a safe sequence exist s, grant t he request . If not , roll back t he
allocat ion (ret urn t o t he previous st at e) and deny t he request .
5. Grant or Deny:
o If t he syst em remains in a safe st at e aft er t he request , grant t he
request .
o Ot herwise, deny t he request and t he process must wait .

Source Code:-
// Banker's Algorit hm
# include < iost ream>
using namespace st d;
int main()
{

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


// P0 , P1, P2, P3, P4 are t he Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[ 5] [ 3] = { { 0 , 1, 0 }, // P0 // Allocat ion Mat rix
{ 2, 0 , 0 }, // P1
{ 3, 0 , 2 }, // P2
{ 2, 1, 1}, // P3
{ 0 , 0 , 2 } }; // P4
int max[ 5] [ 3] = { { 7, 5, 3 }, // P0 // MAX Mat rix
{ 3, 2, 2 }, // P1
{ 9, 0 , 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int avail[ 3] = { 3, 3, 2 }; // Available Resources
int f[ n] , ans[ n] , ind = 0 ;
for (k = 0 ; k < n; k++) {
f[ k] = 0 ;
}
int need[ n] [ m] ;
for (i = 0 ; i < n; i++) {
for (j = 0 ; j < m; j++)
need[ i] [ j] = max[ i] [ j] - alloc[ i] [ j] ;
}
int y = 0 ;
for (k = 0 ; k < 5; k++) {
for (i = 0 ; i < n; i++) {
if (f[ i] == 0 ) {
int fl ag = 0 ;
for (j = 0 ; j < m; j++) {

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


if (need[ i] [ j] > avail[ j] ){
fl ag = 1;
break;
}
}
if (fl ag == 0 ) {
ans[ ind++] = i;
for (y = 0 ; y < m; y++)
avail[ y] += alloc[ i] [ y] ;
f[ i] = 1;
}
}
}
}

int fl ag = 1;
// To check if sequence is safe or not
for(int i = 0 ;i< n;i++)
{
if(f[ i] ==0 )
{
fl ag = 0 ;
cout < < " The given sequence is not safe" ;
break;
}
}
if(fl ag==1)
{
cout < < " Following is t he SAFE Sequence" < < endl;
for (i = 0 ; i < n - 1; i++)

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


cout < < " P" < < ans[ i] < < " - > " ;
cout < < " P" < < ans[ n - 1] < < endl;
}
ret urn (0 );
}
Out put :-
Following is t he SAFE Sequence
P1- > P3 - > P4 - > P0 - > P2

-------------------------------------------------------------------------------------
---------------------------

Experiment - 8
Aim:- Implement Cont iguous Memory Management Algorit hms (First fi t ,Best
fi t ,Worst fi t ).
Descript ion:-
Memory allocat ion st rat egies are used by operat ing syst ems t o assign
memory t o processes effi cient ly. The t hree commonly used st rat egies are First
Fit , Best Fit , and Worst Fit . Below is a brief descript ion of each st rat egy:
1. First Fit :
In First Fit , t he operat ing syst em searches t he memory blocks
from t he beginning and allocat es t he fi rst block t hat is large
enough t o accommodat e t he process.
2. Best Fit :
In Best Fit , t he operat ing syst em searches all memory blocks and
allocat es t he smallest block t hat is large enough for t he process.
3. Worst Fit :
In Worst Fit , t he operat ing syst em allocat es t he largest block of
memory available, hoping t hat t his will leave larger left over blocks

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


for fut ure processes.
Algorit hm:-
1. First Fit Algorit hm:-
St ep- by- St ep Process:-
1. St art from t he beginning of t he memory list .
2. Find t he fi rst block t hat is large enough t o accommodat e t he process.
3. Allocat e t he process t o t hat block.
4. Updat e t he block's st at us t o " allocat ed" and reduce it s size if
necessary.
5. If no suit able block is found, t he process cannot be allocat ed.
2. Best Fit Algorit hm:-
St ep- by- St ep Process:-
1. Search t hrough all available blocks t o fi nd t he smallest block t hat is
large enough t o accommodat e t he process.
2. Allocat e t he process t o t his block.
3. Updat e t he block's st at us t o " allocat ed" and reduce it s size if
necessary.
4. If no suit able block is found, t he process cannot be allocat ed.

3. Worst Fit Algorit hm:-


St ep- by- St ep Process:-
1. Search t hrough all available blocks t o fi nd t he largest block.
2. Allocat e t he process t o t he largest block.
3. Updat e t he block's st at us t o " allocat ed" and reduce it s size if
necessary.
4. If no suit able block is found, t he process cannot be allocat ed.
Source Code :-
1. First Fit :-
# include< bit s/st dc++.h>
using namespace st d;
void fi rst Fit (int blockSize[ ] , int m,

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


int processSize[ ] , int n)
{
int allocat ion[ n] ;
memset (allocat ion, - 1, sizeof(allocat ion));
for (int i = 0 ; i < n; i++)
{
for (int j = 0 ; j < m; j++)
{
if (blockSize[ j] > = processSize[ i] )
{ allocat ion[ i] = j;
blockSize[ j] - = processSize[ i] ;
break;
}
}
}
cout < < " \nProcess No.\t Process Size\t Block no.\n" ;
for (int i = 0 ; i < n; i++)
{
cout < < " " < < i+1< < " \t \t "
< < processSize[ i] < < " \t \t " ;
if (allocat ion[ i] != - 1)
cout < < allocat ion[ i] + 1;
else
cout < < " Not Allocat ed" ;
cout < < endl;
}
}
int main()
{
int blockSize[ ] = {10 0 , 50 0 , 20 0 , 30 0 , 60 0 };

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


int processSize[ ] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[ 0 ] );
int n = sizeof(processSize) / sizeof(processSize[ 0 ] );

fi rst Fit (blockSize, m, processSize, n);


ret urn 0 ;
}
Out put of First Fit :-
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 2
4 426 Not Allocat ed
Source Code :-
2. Best Fit :-
# include< bit s/st dc++.h>
using namespace st d;
void best Fit (int blockSize[ ] , int m, int processSize[ ] , int n)
{ int allocat ion[ n] ;
memset (allocat ion, - 1, sizeof(allocat ion));
for (int i=0 ; i< n; i++)
{
int best Idx = - 1;
for (int j=0 ; j< m; j++)
{
if (blockSize[ j] > = processSize[ i] )
{
if (best Idx == - 1)
best Idx = j;
else if (blockSize[ best Idx] > blockSize[ j] )

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


best Idx = j;
}
}
if (best Idx != - 1)
{
allocat ion[ i] = best Idx;
blockSize[ best Idx] - = processSize[ i] ;
}
}

cout < < " \nProcess No.\t Process Size\t Block no.\n" ;
for (int i = 0 ; i < n; i++)
{
cout < < " " < < i+1< < " \t \t " < < processSize[ i] < < " \t \t " ;
if (allocat ion[ i] != - 1)
cout < < allocat ion[ i] + 1;
else
cout < < " Not Allocat ed" ;
cout < < endl;
}
}
int main()
{
int blockSize[ ] = {10 0 , 50 0 , 20 0 , 30 0 , 60 0 };
int processSize[ ] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[ 0 ] );
int n = sizeof(processSize)/sizeof(processSize[ 0 ] );

best Fit (blockSize, m, processSize, n);

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


ret urn 0 ;
}
Out put of Best Fit :-
Process No. Process Size Block no.
1 212 4
2 417 2
3 112 3
4 426 5
Source Code :-
3. Worst Fit :-
# include< bit s/st dc++.h>
using namespace st d;

void worst Fit (int blockSize[ ] , int m, int processSize[ ] ,int n)


{ int allocat ion[ n] ;
memset (allocat ion, - 1, sizeof(allocat ion));
for (int i=0 ; i< n; i++)
{
int wst Idx = - 1;
for (int j=0 ; j< m; j++)
{
if (blockSize[ j] > = processSize[ i] )
{
if (wst Idx == - 1)
wst Idx = j;
else if (blockSize[ wst Idx] < blockSize[ j] )
wst Idx = j;
}
}
if (wst Idx != - 1)

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


{
allocat ion[ i] = wst Idx;
blockSize[ wst Idx] - = processSize[ i] ;
}
}

cout < < " \nProcess No.\t Process Size\t Block no.\n" ;
for (int i = 0 ; i < n; i++)
{
cout < < " " < < i+1< < " \t \t " < < processSize[ i] < < " \t \t " ;
if (allocat ion[ i] != - 1)
cout < < allocat ion[ i] + 1;
else
cout < < " Not Allocat ed" ;
cout < < endl;
}
}
int main()
{
int blockSize[ ] = {10 0 , 50 0 , 20 0 , 30 0 , 60 0 };
int processSize[ ] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[ 0 ] );
int n = sizeof(processSize)/sizeof(processSize[ 0 ] );

worst Fit (blockSize, m, processSize, n);

ret urn 0 ;
}
Out put of Worst Fit :-
Process No. Process Size Block no.

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


1 212 5
2 417 2
3 112 5
4 426 Not Allocat ed

-------------------------------------------------------------------------------------
---------------------------

Experiment - 9
Aim:- Implement Page replacement algorit hm (Virt ual Memory Techniques)
like:- LRU , FCFS , LIFO .
Descript ion:-
1. LRU (Least Recent ly Used):-
LRU is a page replacement algorit hm used in operat ing syst ems. It replaces t he
page t hat has not been used for t he longest period of t ime when a page needs
t o be loaded int o memory. LRU is based on t he assumpt ion t hat pages used

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


recent ly will likely be used again soon.
2. FCFS (First - Come, First - Served):-
FCFS is a scheduling algorit hm used in CPU scheduling. It processes t asks in
t he order t hey arrive. The fi rst process t o arrive get s t he CPU fi rst , and t hen t he
next process wait s unt il t he current one fi nishes. It 's simple but can lead t o
convoy eff ect s, where short jobs are delayed by long ones.
3. LIFO (Last - In, First - Out ):-
LIFO is anot her simple scheduling algorit hm similar t o a st ack, where t he last
t ask added is t he fi rst one t o be execut ed. It 's oft en used in st ack- based dat a
st ruct ures, but in scheduling, it can be ineffi cient since it doesn't priorit ize
t asks based on t heir arrival t ime or urgency.
Algorit hm:-
1. LRU Algorit hm St ep- by- St ep:-
1. Maint ain a record of t he pages in memory, usually in t he order of usage.
2. Track page usage: Whenever a page is accessed, mark it as recent ly
used (move it t o t he t op of t he list or updat e it s t imest amp).
3. When a page needs t o be replaced:
o Select t he page t hat has been unused for t he longest t ime (t he one
at t he bot t om of t he list ).
4. Replace t he least recent ly used page wit h t he new page.
5. Repeat t he process as new pages arrive.
2. FCFS Algorit hm St ep- by- St ep:-
1. Queue processes in t he order of arrival.
2. The fi rst process in t he queue get s execut ed fi rst .
3. Once t he process fi nishes, t he next process in line is execut ed.
4. Repeat unt il all processes are complet ed.

3.LIFO Algorit hm St ep- by- St ep:-


1. Add processes t o t he st ack as t hey arrive.
2. The last process added t o t he st ack is select ed for execut ion fi rst .
3. Aft er complet ing t he execut ion of t he select ed process, remove it from

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


t he st ack.
4. Repeat wit h t he next process at t he t op of t he st ack.
Source Code:-
1. LRU:-
# include< st dio.h>
main() {
int q[ 20 ] ,p[ 50 ] ,c=0 ,c1,d,f,i,j,k=0 ,n,r,t ,b[ 20 ] ,c2[ 20 ] ;
print f(" Ent er no of pages:" );
scanf(" % d" ,&n);
print f(" Ent er t he reference st ring:" );
for(i=0 ;i< n;i++)
scanf(" % d" ,&p[ i] );
print f(" Ent er no of frames:" );
scanf(" % d" ,&f);
q[ k] =p[ k] ;
print f(" \n\t % d\n" ,q[ k] );
c++;
k++;
for(i=1;i< n;i++)
{
c1=0 ;
for(j=0 ;j< f;j++)
{
if(p[ i] !=q[ j] )
c1++;
}
if(c1==f)
{
c++;
if(k< f)

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


{
q[ k] =p[ i] ;
k++;
for(j=0 ;j< k;j++)
print f(" \t % d" ,q[ j] );
print f(" \n" );
}
else
{
for(r=0 ;r< f;r++)
{
c2[ r] =0 ;
for(j=i- 1;j< n;j- - )
{
if(q[ r] !=p[ j] )
c2[ r] ++;
else
break;
}
}
for(r=0 ;r< f;r++)
b[ r] =c2[ r] ;
for(r=0 ;r< f;r++)
{
for(j=r;j< f;j++)
{
if(b[ r] < b[ j] )
{
t =b[ r] ;
b[ r] =b[ j] ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


b[ j] =t ;
}
}
}
for(r=0 ;r< f;r++)
{
if(c2[ r] ==b[ 0 ] )
q[ r] =p[ i] ;
print f(" \t % d" ,q[ r] );
}
print f(" \n" );
}
}
}
print f(" \nThe no of page fault s is % d" ,c);
}
Input of LRU:-
Ent er no of pages:3
Ent er t he reference st ring:4
5
6
Ent er no of frames:3
Out put of LRU:-
4
4 5
4 5 6
The no of page fault s is 3
Source Code:-
2. FCFS:-
# include < iost ream>

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


# include < vect or>
# include < unordered_ map>
using namespace st d;
// FCFS Page Replacement Algorit hm
class FCFS {
public:
FCFS(int capacit y) : capacit y(capacit y) {}
void accessPage(int pageNumber) {
// If t he page is already in memory, no need t o replace it
if (pageMap.fi nd(pageNumber) != pageMap.end()) {
cout < < " Page " < < pageNumber < < " is already in memory.\n" ;
ret urn;
}
// If memory is full, remove t he fi rst page (oldest ) t o make space
if (pageFrames.size() == capacit y) {
int removedPage = pageFrames.front ();
pageFrames.erase(pageFrames.begin()); // Remove t he oldest page
pageMap.erase(removedPage); // Remove it from t he map
cout < < " Page " < < removedPage < < " replaced by page " < < pageNumber
< < " .\n" ;
}
// Add t he new page t o memory
pageFrames.push_ back(pageNumber);
pageMap[ pageNumber] = t rue;
cout < < " Page " < < pageNumber < < " added t o memory.\n" ;
}
void displayMemory() {
cout < < " Current pages in memory: " ;
for (int page : pageFrames) {
cout < < page < < " " ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


}
cout < < " \n" ;
}
privat e:
int capacit y; // The capacit y of t he memory (number of frames)
vect or< int > pageFrames; // List of pages in memory
unordered_ map< int , bool> pageMap; // Map t o keep t rack of pages in
memory
};
int main() {
int capacit y, numPages;
// Get memory capacit y and number of page references from user
cout < < " Ent er t he number of frames in memory: " ;
cin > > capacit y;
FCFS fcfs(capacit y);
cout < < " Ent er t he number of page references: " ;
cin > > numPages;
cout < < " Ent er t he page references: \n" ;
vect or< int > pages(numPages);
for (int i = 0 ; i < numPages; ++i) {
cin > > pages[ i] ;
}
// Simulat e accessing pages based on t he FCFS algorit hm
for (int page : pages) {
fcfs.accessPage(page);
fcfs.displayMemory();
}
ret urn 0 ;
}
Input of FCFS:-

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Ent er t he number of frames in memory: 3
Ent er t he number of page references: 4
Ent er t he page references:
5
6
4
3
Out put of FCFS:-
Page 5 added t o memory.
Current pages in memory: 5
Page 6 added t o memory.
Current pages in memory: 5 6
Page 4 added t o memory.
Current pages in memory: 5 6 4
Page 5 replaced by page 3.
Page 3 added t o memory.
Current pages in memory: 6 4 3
Source Code:-
3. LIFO:-
# include < iost ream>
# include < vect or>
# include < st ack>
# include < unordered_ map>
using namespace st d;
// LIFO Page Replacement Algorit hm
class LIFO {
public:
LIFO(int capacit y) : capacit y(capacit y) {}
// Funct ion t o simulat e accessing a page
void accessPage(int pageNumber) {

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


// If t he page is already in memory, no need t o replace it
if (pageMap.fi nd(pageNumber) != pageMap.end()) {
cout < < " Page " < < pageNumber < < " is already in memory.\n" ;
ret urn;
}
// If memory is full, remove t he last added page (most recent )
if (pageSt ack.size() == capacit y) {
int removedPage = pageSt ack.t op(); // Get t he most recent ly added page
pageSt ack.pop(); // Remove t he last added page
pageMap.erase(removedPage); // Remove it from t he map
cout < < " Page " < < removedPage < < " replaced by page " < < pageNumber
< < " .\n" ;
}
// Add t he new page t o memory (st ack and map)
pageSt ack.push(pageNumber);
pageMap[ pageNumber] = t rue;
cout < < " Page " < < pageNumber < < " added t o memory.\n" ;
}
// Funct ion t o display t he current st at e of memory
void displayMemory() {
cout < < " Current pages in memory: " ;
st ack< int > t empSt ack = pageSt ack;
// Display pages in t he order t hey were added (most recent on t op)
vect or< int > memory;
while (!t empSt ack.empt y()) {
memory.push_ back(t empSt ack.t op());
t empSt ack.pop();
}
for (int page : memory) {
cout < < page < < " " ;

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


}
cout < < endl;
}
privat e:
int capacit y; // Maximum number of pages t hat can be st ored in memory
st ack< int > pageSt ack; // St ack t o keep t rack of pages in memory
unordered_ map< int , bool> pageMap; // Map t o check if a page is already in
memory
};
int main() {
int capacit y, numPages;
// Get memory capacit y and number of page references from user
cout < < " Ent er t he number of frames in memory: " ;
cin > > capacit y;
LIFO lifo(capacit y);
cout < < " Ent er t he number of page references: " ;
cin > > numPages;
cout < < " Ent er t he page references: \n" ;
vect or< int > pages(numPages);
for (int i = 0 ; i < numPages; ++i) {
cin > > pages[ i] ;
}
// Simulat e accessing pages based on t he LIFO algorit hm
for (int page : pages) {
lifo.accessPage(page);
lifo.displayMemory();
}
ret urn 0 ;
}

Brajesh Pat el 2310 0 BTCSE14825Page No. 2


Input of LIFO:-
Ent er t he number of frames in memory: 3
Ent er t he number of page references: 4
Ent er t he page references:
5
6
4
3
Out put of LIFO:-
Page 5 added t o memory.
Current pages in memory: 5
Page 6 added t o memory.
Current pages in memory: 6 5
Page 4 added t o memory.
Current pages in memory: 4 6 5
Page 4 replaced by page 3.
Page 3 added t o memory.
Current pages in memory: 3 6 5

-------------------------------------------------------------------------------------
---------------------------

Brajesh Pat el 2310 0 BTCSE14825Page No. 2

You might also like