OS 2nd
OS 2nd
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
-------------------------------------------------------------------------------------
---------------------------
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
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.
Process[ 2] 3 0 3
Process[ 4] 4 3 7
Process[ 1] 5 7 12
Process[ 3] 2 12 14
-------------------------------------------------------------------------------------
---------------------------
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.
Source Code:-
// Banker's Algorit hm
# include < iost ream>
using namespace st d;
int main()
{
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++)
-------------------------------------------------------------------------------------
---------------------------
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
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 ] );
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 ] );
ret urn 0 ;
}
Out put of Worst Fit :-
Process No. Process Size Block no.
-------------------------------------------------------------------------------------
---------------------------
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
-------------------------------------------------------------------------------------
---------------------------