The document describes a C++ program that simulates the Banker's algorithm for deadlock avoidance. The program takes input for allocation and maximum resource matrices, calculates the need matrix, and simulates checking for a safe state and finding a safety sequence. It initializes matrices, checks if a process can be granted resources while maintaining a safe state, and adds resources if so. The program outputs whether the system is in a safe or unsafe state.
2. 1
CPU Scheduling Algorithm - First-Come-First-Serve
Aim: To develop a C++ program for the simulation of First-Come-First-
Serve
Description: FIFO is an acronym for First In, First Out, an abstraction in
ways of organizing and manipulation of data relative to time and
prioritization. This expression describes the principle of a queue processing
technique or servicing conflicting demands by ordering process by first-
come, first-served (FCFS) behavior: what comes in first is handled first,
what comes in next waits until the first is finished, etc.
Program:
#include<iostream.h>
#include<conio.h>
class FCFS
{
int b_time[10];
int w_time[10];
int n_jobs;
float avg;
public:
FCFS()
{
n_jobs = 0;
avg = 0;
}
void Job_Entry(int n)
{
n_jobs = n;
cout<<"nEnter the Burst Time of each Job :";
cout<<"n";
4. 3
int n;
clrscr();
cout<<"nntttFirst Come First Served (FCFS)";
cout<<"nnHow many Jobs going to be Entered : ";
cin>>n;
F.Job_Entry(n);
F.Wait_Time();
getch();
}
Result:
Thus the program for the simulation of First-Come-First-Serve has
been developed and executed successfully.
5. 4
Output:
First Come First Serve (FCFS)
How many Jobs going to be Entered : 3
Enter the Burst Time of each Job :
Job 1: 12
Job 2: 10
Job 3: 4
Gantt chart
Burst Time | 12 | 10 | 4 |
Waiting Time 0 12 22
The Average Waiting Time is : 11.333333 ms
6. 5
CPU Scheduling Algorithm - Round Robin
Aim: To develop a C++ Program for the simulation of Round Robin
Scheduling
Description: Scheduling is a key concept in computer
multitasking and multiprocessing operating system design, and
in real-time operating system design. It refers to the way
processes are assigned priorities in a priority queue. This
assignment is carried out by software known as a scheduler.
Round-robin (RR) is one of the simplest scheduling
algorithms for processes in an operating system, which assigns
time slices to each process in equal portions and in order,
handling all processes without priority. Round-robin scheduling is
both simple and easy to implement, and starvation-free. Round-
robin scheduling can also be applied to other scheduling
problems, such as data packet scheduling in computer networks.
Program:
#include<iostream.h>
#include<conio.h>
class Round
{
int wt,st[20],com[20],brust[20],executed[20];
int tq,n;
float avg;
public:
void GetData();
void Calculate();
7. 6
};
void Round::GetData()
{
cout<<"nEnter the number of Process :";
cin>>n;
cout<<"nEnter the Brust Time each Process :n";
for(int i = 1;i <= n ; i++)
{
cout<<"nJob "<<i<<": ";
cin>>brust[i];
}
cout<<"nEnter the Time Quantum :";
cin>>tq;
}
void Round::Calculate()
{
int flag,i;
avg = 0,wt = 0;
for(i = 1 ; i <= n ; i++)
executed[i] = 0;
flag = 1;
cout<<"nProcess tStarting Time tCompletion Time";
while(flag == 1)
{
flag = 0;
for(i = 1; i <= n; i++)
{
if(brust[i] > tq)
{
st[i] = wt;
if(executed[i] == 1)
10. 9
Output:
Round Robin Scheduling
Enter the number of Process :4
Enter the Brust Time each Process :
Job 1: 23
Job 2: 12
Job 3: 50
Job 4: 20
Enter the Time Quantum: 5
Process Starting Time Completion Time
Process 1 0 5
Process 2 5 10
Process 3 10 15
Process 4 15 20
Process 1 20 25
Process 2 25 30
Process 3 30 35
Process 4 35 40
Process 1 40 45
Process 2 45 47
Process 3 47 52
Process 4 52 57
Process 1 57 62
Process 3 62 67
Process 4 67 72
Process 1 72 75
Process 3 75 80
Process 3 80 85
Process 3 85 90
Process 3 90 95
Process 3 95 100
Process 3 100 105
The Average Waiting Time is : 48.5 ms
11. 10
CPU Scheduling – Shortest Job First (Non-Preemptive)
Aim: To develop a C++ Program for the simulation of Shortest Job First
(SJF) - Non Preemptive
Description: Scheduling is a key concept in computer
multitasking and multiprocessing operating system design, and
in real-time operating system design. It refers to the way
processes are assigned priorities in a priority queue. This
assignment is carried out by software known as a scheduler.
Shortest job first (SJF) (also known as Shortest Job Next
(SJN)) is a scheduling policy that selects the waiting process with
the smallest execution time to execute next. Shortest job next is
advantageous because of its simplicity and because it maximizes
process throughput (in terms of the number of processes run to
completion in a given amount of time). However, it has the
potential for process starvation for processes which will require a
long time to complete if short processes are continually added.
Highest response ratio next is similar but provides a solution to
this problem.
Program:
#include<iostream.h>
#include<conio.h>
class SJF
{
int b_time[10];
int w_time[10];
int n_jobs;
13. 12
void Wait_Time(void)
{
/* Calculation */
for(int i = 0;i < n_jobs; i++)
{
w_time[i] = 0;
for(int j = 0; j < i;j++)
{
w_time[i] = w_time[i] + b_time[j];
}
avg = avg + w_time[i];
}
avg = avg/n_jobs;
/* Printing in Output Console */
cout<<"nttttGantt chartn";
cout<<"nBurst Time tt|";
for(i = 0;i < n_jobs;i++)
cout<<" "<<b_time[i]<<" |";
cout<<"nWaiting Timett";
for(i = 0;i < n_jobs;i++)
cout<<w_time[i]<<" ";
cout<<"nnThe Average Waiting Time is : "<<avg<<" ms";
}
};
int main()
{
SJF S;
int n;
clrscr();
cout<<"nnttShortest Job First (SJF) - Non Preemptive";
cout<<"nnHow many Jobs going to be Entered : ";
cin>>n;
S.Job_Entry(n);
S.Wait_Time();
14. 13
getch();
}
Result:
Thus the program for Simulation of SJF-non-preemptive has been
developed successfully and executed
Output:
Shortest Job First (SJF) - Non Preemptive
How many Jobs going to be Entered : 3
Enter the Burst Time of each Job :
Job 1: 10
Job 2: 5
Job 3: 15
Gantt chart
Burst Time | 5 | 10 | 15 |
Waiting Time 0 5 15
The Average Waiting Time is : 6.666667 ms
15. 14
PU Scheduling Algorithm – Priority
Aim: To develop a C++ program for the simulation of Priority Scheduling
Description: Scheduling is a key concept in computer
multitasking and multiprocessing operating system design, and
in real-time operating system design. It refers to the way
processes are assigned priorities in a priority queue. This
assignment is carried out by software known as a scheduler.
In case of priority scheduling, each task/ process will be given some
priority and based on this priority, the scheduling mechanism will be done.
For example, consider two processes – process-1 and process-2. Process-1
will perform the scanning of system and Process-2 will perform the
printing of a file. Among these two processes, the Process-1 will be given
priority and will be scheduled first because of the prime importance for
process-1 compared to process-2
Program:
#include<iostream.h>
#include<conio.h>
class Priority
{
public:
int p[25],ser[25],pri[25],pri1[25],wait[25],i,j,n;
double awt,tat,s;
void Get()
{
cout<<"nEnter the Number of Jobs : ";
cin>>n;
for(i = 0;i < n;i++)
{
16. 15
cout<<"nEnter the Burst time for the Process
P["<<i+1<<"] : ";
cin>>ser[i];
cout<<"nEnter the Priority for Process P["<<i+1<<"] : ";
cin>>pri[i];
pri1[i]=pri[i];
p[i]=i;
}
}
void Calculate()
{
int temp;
for(i = 0;i < n;i++)
{
for(j = 0;j <= (n-i-1);j++)
{
if(pri[j] >= pri[j+1])
{
temp = pri[j];
pri[j] = pri[j+1];
pri[j+1] = temp;
}
}
}
for(i = 0;i < n;i++)
for(j = 0;j < n;j++)
{
if(pri[i] == pri1[j])
{
if(i == 0)
{
wait[i] = 0;
awt=0;
tat = ser[i];
temp=ser[j];
19. 18
Output:
Enter the Number of Jobs : 4
Enter the Burst time for the Process P[1] : 23
Enter the Priority for Process P[1] : 4
Enter the Burst time for the Process P[2] : 20
Enter the Priority for Process P[2] : 2
Enter the Burst time for the Process P[3] : 10
Enter the Priority for Process P[3] : 1
Enter the Burst time for the Process P[4] : 12
Enter the Priority for Process P[4] : 3
Scheduling Order
PID Priority Service Time Wait time
3 1 10 11896
2 2 20 11906
4 3 12 11926
Average Waiting Time : 8932 ms
Average Turnaround Time : 8942.5 ms
20. 19
Banker’s Algorithm for Deadlock Avoidance
Aim: To develop a C++ program for the simulation of Banker’s Algorithm
Description: The Banker's algorithm is a resource allocation &
deadlock avoidance algorithm developed by Edsger Dijkstra that tests for
safety by simulating the allocation of pre-determined maximum possible
amounts of all resources, and then makes a "safe-state" check to test for
possible deadlock conditions for all other pending activities, before
deciding whether allocation should be allowed to continue.
Program:
#include<iostream.h>
#include<conio.h>
class Banker
{
private:
int allocated[50][50],max[50][50],available[50];
int need[50][50];
int flag[50];
int m,n;
int i,j;
public:
Banker()
{
cout<<"nEnter the Row for Allocation and Maximum Matrix :
";
cin>>m;
cout<<"nEnter the Column for Allocation and Maximum
Matrix : ";
cin>>n;
23. 22
}
}
if(temp == 0)
cout<<"nnSystem in Safe State";
else
cout<<"nnSystem in UnSafe State";
}
};
int main()
{
clrscr();
Banker B;
B.Need();
B.Process();
getch();
}
Result:
Thus the Program for Simulation of Banker’s algorithm is developed
and executed successfully.
24. 23
Output:
Enter the Row for Allocation and Maximum Matrix : 3
Enter the Column for Allocation and Maximum Matrix : 3
Allocation Matrix [3 X 3]
1 0 0
0 1 0
0 0 1
Maximum Matrix [3 X 3]
1 1 1
1 1 1
1 1 1
Available
1 2 3
Need Matrix [3 X 3]
0 1 1
1 0 1
1 1 0
Safety Sequence : P0 P1 P2
System in Safe State
25. 24
Page Replacement Algorithm – FIFO
Aim: To develop a C++ program for the simulation of FIFO Page
Replacement Algorithm
Description: The first-in, first-out (FIFO) page replacement algorithm is a
low-overhead algorithm that requires little bookkeeping on the part of the
operating system. The idea is obvious from the name - the operating
system keeps track of all the pages in memory in a queue, with the most
recent arrival at the back, and the earliest arrival in front. When a page
needs to be replaced, the page at the front of the queue (the oldest page)
is selected. While FIFO is cheap and intuitive, it performs poorly in practical
application.
Program:
#include<iostream.h>
#include<conio.h>
class FIFO
{
private:
int n,m;
int pf;
int count;
int a[100],b[100][100];
public:
FIFO()
{
pf = 0;
int i,j;
i = j = 0;
cout<<"nEnter the No. Of Frame :";
cin>>m;
cout<<"nEnter the Length of the Reference String :";
28. 27
int main()
{
clrscr();
cout<<"ttttFIFO Page Replacement Algorithmn";
FIFO F;
F.Replace();
getch();
}
Result:
Thus the program for the simulation of FIFO Page Replacement has
been developed and executed successfully.
29. 28
Output:
FIFO Page Replacement Algorithm
Enter the No. Of Frame :3
Enter the Length of the Reference String :3
Enter the Page Frame [1] :12
Enter the Page Frame [2] :14
Enter the Page Frame [3] :3
Reference String <12> 12 - - Page Fault
Reference String <14> 12 14 - Page Fault
Reference String <3> 12 14 3 Page Fault
Number of Page Fault :3
30. 29
Page Replacement Algorithm – LRU
Aim: To develop a C++ Program for the simulation of LRU page
replacement
Description: In LRU page replacement, the page which has not been
allocated very recently will be allocated now for the process.
Program:
#include<iostream.h>
#include<conio.h>
void display(int ar[], int n)
{
for(int i=0;i<n;i++)
cout<<"t"<<ar[i];
cout<<"n";
}
void main()
{
int pg[50],fr[10],rnk[5],set,tmp,n,nf,i,j,k,flag,flag1;
clrscr();
cout<<"nEnter the Length of the Reference String: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"nEnter the Page Frame["<<i+1<<"] : ";
cin>>pg[i];
}
cout<<"nEnter the No. of Frames: ";
cin>>nf;
for(i=0;i<nf;i++)
33. 32
Output:
Enter the Length of the Reference String: 3
Enter the Page Frame[1] : 128
Enter the Page Frame[2] : 100
Enter the Page Frame[3] : 200
Enter the No. of Frames: 3
128 -1 -1
128 100 -1
128 100 200
34. 33
First Fit, Best Fit and Worst Fit Algorithm
Aim: To develop a C++ Program for the simulation of First, best fit
algorithm
Description: In first fit algorithm, the frame which is free and first in
occurrence will be allocated. In Best fit algorithm, the algorithm searches
for the exact space where the particular process can be allocated. In worst
fit algorithm, the algorithm selects the part where there is more space and
will allocate for that particular process.
Program:
#include<iostream.h>
class Fit
{
private:
int pro[100],dup_pro[100];
int hol[100],dup_hol[100];
int e[100],flag[100];
int m,n;
public:
void Get()
{
int i;
cout<<"nEnter the number of Hole : ";
cin>>m;
cout<<endl;
for(i = 0;i < m;i++)
{
cout<<"Hole Size ["<<i+1<<"] : ";
cin>>hol[i];
dup_hol[i] = hol[i];
}
cout<<"nEnter the number of Process : ";
37. 36
{
cout<<"ntttt* Worst Fit *n";
Sort(dup_hol,m);
Sort(dup_pro,n);
MakeNull();
for(int i = 0;i < n;i++)
for(int j = m;j >= 0;j--)
if(dup_hol[j] >= dup_pro[i] && e[j] == 0)
{
cout<<"nProcess "<<dup_pro[i]<<" is Mapped with
Hole : "<<dup_hol[j];
e[j] = 1;
j = -1;
flag[i] = 1;
}
UnAllocated(dup_pro);
}
};
int main()
{
Fit F;
F.Get();
F.FirstFit();
F.BestFit();
F.WorstFit();
return 0;
Result:
Thus the program for the simulation of First Fit, Best Fit and Worst Fit
has been developed and executed successfully.
38. 37
Output:
Enter the number of Hole : 5
Hole Size [1] : 122
Hole Size [2] : 230
Hole Size [3] : 256
Hole Size [4] : 512
Hole Size [5] : 210
Enter the number of Process : 3
Process Size [1] : 100
Process Size [2] : 450
Process Size [3] : 80
* First Fit *
Process 100 is mapped with Hole : 122
Process 450 is mapped with Hole : 512
Process 80 is mapped with Hole : 210
* Best Fit *
Process 80 is Mapped with Hole : 122
Process 100 is Mapped with Hole : 210
Process 450 is Mapped with Hole : 512
* Worst Fit *
Process 80 is Mapped with Hole : 512
Process 100 is Mapped with Hole : 256
450 is Unallocated
39. 38
Producer- Consumer Problem
Aim: To develop a C program for the simulation of Producer Consumer
problem
Description: In Producer-Consumer Problem, there are one or more
producers and only one consumer. Either any of the producers or the
consumer can access the buffer.
Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<signal.h>
main()
{
char *ptr,str[20];
struct sembuf lock = {0,-1,0},unlock = {0,1,0};
int shmid,pid,prod,cons,y;
prod = semget((key_t)20,1,IPC_CREAT |0666);
cons = semget((key_t)21,1,IPC_CREAT |0666);
semctl(cons,0,SETVAL,0);
semctl(prod,0,SETVAL,1);
shmid = shmget((key_t)100,20,IPC_CREAT |0666);
ptr = (char*)shmat(shmid,0,0);
pid = fork();
if(pid == 0)
40. 39
{
while(1)
{
semop(prod,&lock,1);
fflush(stdin);
printf("Enter the value has to be produced :");
scanf("%s",str);
strcpy(ptr,str);
printf("Producer produced the value %s n",ptr);
printf("Producer Finished n");
semop(cons,&unlock,1);
}
}
else
{
char temp [] = {"END"};
while(1)
{
semop(cons,&lock,1);
printf("Consumer consumed the value %s n",ptr);
printf("Consumer Finished n");
if(strcmp(ptr,temp) == 0)
{
kill(pid,SIGINT);
exit(0);
}
else
semop(prod,&unlock,1);
}
}
}
Result: Thus the program for Simulation of Producer-Consumer
Problem has been developed successfully and executed
41. 40
Output:
Enter the value has to be produced :SRM
Producer produced the value SRM
Producer Finished
Consumer consumed the value SRM
Consumer Finished
Enter the value has to be produced :University
Producer produced the value University
Producer Finished
Consumer consumed the value University
Consumer Finished
Enter the value has to be produced :CSE
Producer produced the value CSE
Producer Finished
Consumer consumed the value CSE
Consumer Finished
Enter the value has to be produced :END
Producer produced the value END
Producer Finished
Consumer consumed the value END
Consumer Finished