SlideShare a Scribd company logo
1 of 41
Download to read offline
OPERATING
SYSTEM
LAB MANUAL
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";
2
for(int i = 0;i < n_jobs;i++)
{
cout<<"nJob "<<i+1<<": ";
cin>>b_time[i];
}
}
void Wait_Time(void)
{
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()
{
FCFS F;
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.
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
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();
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)
7
avg = avg + st[i] - com[i];
else
avg = avg + st[i];
wt = wt + tq;
com[i] = wt;
brust[i] = brust[i] - tq;
flag = 1;
executed[i] = 1;
cout<<"nProcess "<<i<<"t
"<<st[i]<<"ttt"<<com[i];
}
else if(brust[i] > 0)
{
st[i] = wt;
if(executed[i] == 1)
avg = avg + st[i] - com[i];
else
avg = avg + st[i];
wt = wt + brust[i];
com[i] = wt;
cout<<"nProcess "<<i<<"t
"<<st[i]<<"ttt"<<com[i];
brust[i] = 0;
executed[i] = 1;
}
}
}
avg = avg/n;
cout<<"nnThe Average Waiting Time is : "<<avg<<" ms";
}
int main()
{
clrscr();
Round R;
cout<<"nnttt Round Robin Scheduling";
8
R.GetData();
R.Calculate();
getch();
}
Result:
Thus the program for Simulation of Round-Robin Scheduling has
been developed successfully and executed
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
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;
11
float avg;
public:
SJF()
{
n_jobs = 0;
avg = 0;
}
void Job_Entry(int n)
{
n_jobs = n;
cout<<"nEnter the Burst Time of each Job :";
cout<<"n";
for(int i = 0;i < n_jobs;i++)
{
cout<<"nJob "<<i+1<<": ";
cin>>b_time[i];
}
Sorting(b_time,n);
}
void Sorting(int b_time[],int n)
{
for(int i = 0;i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(b_time[j] < b_time[i])
{
int temp = b_time[i];
b_time[i] = b_time[j];
b_time[j] = temp;
}
}
}
}
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();
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
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++)
{
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];
16
}
else
{
wait[i]=wait[i-1]+temp;
temp=ser[j];
awt +=wait[i];
tat +=ser[i]+wait[i];
}
}
}
awt=awt/n;
tat=tat/n;
}
void Put()
{
wait[0]=0;
cout<<"nnScheduling Order";
cout<<"nPID"<<"tPriority"<<"tService Time"<<"tWait
time";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri[i] == pri1[j])
cout<<"n"<<p[j]+1<<"t"<<pri[i]<<"tt"<<ser[j]
<<"tt"<<wait[i];
}
}
cout<<"nnAverage Waiting Time : "<<awt<<" ms";
cout<<"nAverage Turnaround Time : "<<tat<<" ms";
}
};
17
void main()
{
Priority P;
clrscr();
P.Get();
P.Calculate();
P.Put();
getch();
}
Result:
Thus the program for simulation of Priority Scheduling has been
developed successfully and executed
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
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;
20
cout<<"nAllocation Matrix ["<<m<<" X "<<n<<"]n";
for(i = 0;i < m;i++)
for(j = 0;j < n;j++)
cin>>allocated[i][j];
cout<<"nMaximum Matrix ["<<m<<" X "<<n<<"]n";
for(i = 0;i < m;i++)
for(j = 0;j < n;j++)
cin>>max[i][j];
cout<<"nAvailablen";
for(i = 0;i < n;i++)
{
cin>>available[i];
}
}
void MakeNull()
{
for(i = 0;i < 50;i++)
flag[i] = 0;
}
void Need()
{
cout<<"nNeed Matrix ["<<m<<" X "<<n<<"]n";
for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
need[i][j] = max[i][j] - allocated[i][j];
cout<<need[i][j]<<"t";
}
cout<<"n";
}
}
21
int Check(int column)
{
for(i = 0;i < n;i++)
if(need[column][i] > available[i])
return 0;
return 1;
}
void AddResource(int column)
{
for(int i = 0;i < n;i++)
available[i] = available[i] + allocated[column][i];
}
void Process()
{
int t,k,l;
int temp = 0;
MakeNull();
cout<<"nSafety Sequence :";
for(k = 0;k < 3;k++)
{
temp = 0;
for(l = 0;l < m;l++)
{
if(flag[l] == 0)
{
temp = 1;
if(Check(l) == 1)
{
AddResource(l);
flag[l] = 1;
cout<<" P"<<l;
}
}
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.
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
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 :";
25
cin>>n;
cout<<endl;
for(i = 0;i < n;i++)
for(j = 0;j < n;j++)
b[i][j] = -1;
for(i = 0;i < n;i++)
{
cout<<"Enter the Page Frame ["<<i+1<<"] :";
cin>>a[i];
}
}
void CopyPrevious(int r)
{
for(int i = 0;i < m;i++)
b[r][i] = b[r-1][i];
}
int Check(int r,int t)
{
for(int i = 0;i < m;i++)
if(b[r][i] == t)
return i;
return -1;
}
void Print(int r)
{
cout<<"nReference String ";
cout<<"<"<<a[r]<<">"<<"t";
for(int i = 0;i < m;i++)
if(b[r][i] == -1)
cout<<"-t";
26
else
cout<<b[r][i]<<"t";
}
void Replace()
{
count = -1;
int set = 0;
for(int i = 0;i < n;i++)
{
set = 0;
if(count == m-1)
count = -1;
if(i == 0)
{
pf++;
b[i][++count] = a[i];
set = 1;
}
else
{
CopyPrevious(i);
if(Check(i,a[i]) == -1)
{
b[i][++count] = a[i];
pf++;
set = 1;
}
}
Print(i);
if(set == 1)
cout<<"Page Fault";
}
cout<<"nnNumber of Page Fault :"<<pf;
}
};
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.
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
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++)
30
{
rnk[i]=0;
fr[i]=-1;
}
cout<<"nn";
j=0;
set=0;
int r=0;
for(i=0;i<n;i++)
{
flag=0;flag1=0;
for(k=0;k<nf;k++)
{
if(fr[k]==pg[i])
flag=1;
}
if(flag==0)
{
if(set>0)
{
tmp=rnk[0];
for(k=1;k<nf;k++)
{
if(rnk[k]<tmp)
tmp=rnk[k];
}
for(k=0;k<nf;k++)
{
if(rnk[k]==tmp)
flag1=k;
}
fr[flag1]=pg[i];rnk[j]=r;
r++;
}
else
31
{
fr[j]=pg[i];rnk[j]=r;
r++;
}
display(fr,nf);
}
else
{
cout<<"n";
rnk[j]=r;r++;
}
j++;
if(j>=nf)
{
set=1;
j=0;
}
}
getch();
}
Result:
Thus the program for the simulation of Page Replacement - LRU has
been developed and executed successfully.
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
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 : ";
34
cin>>n;
cout<<endl;
for(i = 0;i < n;i++)
{
cout<<"Process Size ["<<i+1<<"] : ";
cin>>pro[i];
dup_pro[i] = pro[i];
}
}
void MakeNull()
{
for(int i = 0;i < 100;i++)
e[i] = flag[i] = 0;
}
void UnAllocated(int *disp)
{
for(int i = 0;i < n;i++)
if(flag[i] == 0)
cout<<"n"<<disp[i]<<" is Unallocated";
}
void FirstFit()
{
cout<<"ntttt* First Fit *n";
MakeNull();
for(int i = 0,k = 0;i < n;i++)
for(int j = 0;j < m;j++)
if(pro[k] <= hol[j] && e[j] == 0 && k < n)
{
cout<<"nProcess "<<pro[k]<<" is mapped with Hole :
"<<hol[j];
e[j] = 1;
flag[k] = 1;
k++;
35
}
UnAllocated(pro);
}
void Sort(int *h,int Size)
{
int t;
for(int i = 0;i < Size;i++)
for(int j = 0;j < Size;j++)
if(h[i] < h[j])
{
t = h[i];
h[i] = h[j];
h[j] = t;
}
}
void BestFit()
{
cout<<"ntttt* Best Fit *n";
Sort(dup_hol,m);
Sort(dup_pro,n);
MakeNull();
for(int i = 0;i < n;i++)
for(int j = 0;j < m;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 = m;
flag[i] = 1;
}
UnAllocated(dup_pro);
}
void WorstFit()
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.
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
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)
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
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

More Related Content

What's hot

Basic MIPS implementation
Basic MIPS implementationBasic MIPS implementation
Basic MIPS implementationkavitha2009
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithmssathish sak
 
isa architecture
isa architectureisa architecture
isa architectureAJAL A J
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factorsNancyBeaulah_R
 
Shortest job first Scheduling (SJF)
Shortest job first Scheduling (SJF)Shortest job first Scheduling (SJF)
Shortest job first Scheduling (SJF)ritu98
 
Unit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingUnit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingvishal choudhary
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquesSiva Priya
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computingVajira Thambawita
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process ModelsHassan A-j
 
Pipes in Windows and Linux.
Pipes in Windows and Linux.Pipes in Windows and Linux.
Pipes in Windows and Linux.Junaid Lodhi
 
halstead software science measures
halstead software science measureshalstead software science measures
halstead software science measuresDeepti Pillai
 

What's hot (20)

Basic MIPS implementation
Basic MIPS implementationBasic MIPS implementation
Basic MIPS implementation
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithms
 
isa architecture
isa architectureisa architecture
isa architecture
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factors
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Shortest job first Scheduling (SJF)
Shortest job first Scheduling (SJF)Shortest job first Scheduling (SJF)
Shortest job first Scheduling (SJF)
 
Unit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingUnit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processing
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniques
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computing
 
System calls
System callsSystem calls
System calls
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
Memory management
Memory managementMemory management
Memory management
 
Pipes in Windows and Linux.
Pipes in Windows and Linux.Pipes in Windows and Linux.
Pipes in Windows and Linux.
 
halstead software science measures
halstead software science measureshalstead software science measures
halstead software science measures
 
mano.ppt
mano.pptmano.ppt
mano.ppt
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 

Similar to Operating System Lab Manual

Document 14 (6).pdf
Document 14 (6).pdfDocument 14 (6).pdf
Document 14 (6).pdfRajMantry
 
Processes And Job Control
Processes And Job ControlProcesses And Job Control
Processes And Job Controlahmad bassiouny
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)Make Mannan
 
Operations Research_18ME735_module 5 sequencing notes.pdf
Operations Research_18ME735_module 5 sequencing notes.pdfOperations Research_18ME735_module 5 sequencing notes.pdf
Operations Research_18ME735_module 5 sequencing notes.pdfRoopaDNDandally
 
SCHEDULING RULES DONE.pptx
SCHEDULING  RULES DONE.pptxSCHEDULING  RULES DONE.pptx
SCHEDULING RULES DONE.pptxAgnibhaBanerjee1
 
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...IRJET Journal
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxstilliegeorgiana
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxdenneymargareta
 
Scheduling and Scheduler's Process and Premptive
Scheduling and Scheduler's Process and PremptiveScheduling and Scheduler's Process and Premptive
Scheduling and Scheduler's Process and Premptivechoudharyxdevansh
 
Unit 2 process Management
Unit 2 process ManagementUnit 2 process Management
Unit 2 process Managementzahid7578
 
Cpm module iii reference
Cpm module iii referenceCpm module iii reference
Cpm module iii referenceahsanrabbani
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
First Come First Serve
First Come First ServeFirst Come First Serve
First Come First ServeKavya Kapoor
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 

Similar to Operating System Lab Manual (20)

Document 14 (6).pdf
Document 14 (6).pdfDocument 14 (6).pdf
Document 14 (6).pdf
 
Os
OsOs
Os
 
Process management
Process managementProcess management
Process management
 
Processes And Job Control
Processes And Job ControlProcesses And Job Control
Processes And Job Control
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
COCOMO MODEL
COCOMO MODELCOCOMO MODEL
COCOMO MODEL
 
Operations Research_18ME735_module 5 sequencing notes.pdf
Operations Research_18ME735_module 5 sequencing notes.pdfOperations Research_18ME735_module 5 sequencing notes.pdf
Operations Research_18ME735_module 5 sequencing notes.pdf
 
SCHEDULING RULES DONE.pptx
SCHEDULING  RULES DONE.pptxSCHEDULING  RULES DONE.pptx
SCHEDULING RULES DONE.pptx
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...
A Heterogeneous Static Hierarchical Expected Completion Time Based Scheduling...
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Scheduling and Scheduler's Process and Premptive
Scheduling and Scheduler's Process and PremptiveScheduling and Scheduler's Process and Premptive
Scheduling and Scheduler's Process and Premptive
 
Unit 2 process Management
Unit 2 process ManagementUnit 2 process Management
Unit 2 process Management
 
Cpm module iii reference
Cpm module iii referenceCpm module iii reference
Cpm module iii reference
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Compiler design
Compiler designCompiler design
Compiler design
 
First Come First Serve
First Come First ServeFirst Come First Serve
First Come First Serve
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 

More from Bilal Mirza

Android Operating System
Android Operating SystemAndroid Operating System
Android Operating SystemBilal Mirza
 
Seminar Report on Android OS
Seminar Report on Android OSSeminar Report on Android OS
Seminar Report on Android OSBilal Mirza
 
Android Operating System
Android Operating SystemAndroid Operating System
Android Operating SystemBilal Mirza
 
Learn JavaScript HTML & CSS
Learn JavaScript HTML & CSSLearn JavaScript HTML & CSS
Learn JavaScript HTML & CSSBilal Mirza
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
Environment Problems
Environment ProblemsEnvironment Problems
Environment ProblemsBilal Mirza
 

More from Bilal Mirza (7)

Android Operating System
Android Operating SystemAndroid Operating System
Android Operating System
 
Seminar Report on Android OS
Seminar Report on Android OSSeminar Report on Android OS
Seminar Report on Android OS
 
Android Operating System
Android Operating SystemAndroid Operating System
Android Operating System
 
Learn JavaScript HTML & CSS
Learn JavaScript HTML & CSSLearn JavaScript HTML & CSS
Learn JavaScript HTML & CSS
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Environment Problems
Environment ProblemsEnvironment Problems
Environment Problems
 
Counters
Counters Counters
Counters
 

Recently uploaded

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Recently uploaded (20)

Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 

Operating System Lab Manual

  • 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";
  • 3. 2 for(int i = 0;i < n_jobs;i++) { cout<<"nJob "<<i+1<<": "; cin>>b_time[i]; } } void Wait_Time(void) { 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() { FCFS F;
  • 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)
  • 8. 7 avg = avg + st[i] - com[i]; else avg = avg + st[i]; wt = wt + tq; com[i] = wt; brust[i] = brust[i] - tq; flag = 1; executed[i] = 1; cout<<"nProcess "<<i<<"t "<<st[i]<<"ttt"<<com[i]; } else if(brust[i] > 0) { st[i] = wt; if(executed[i] == 1) avg = avg + st[i] - com[i]; else avg = avg + st[i]; wt = wt + brust[i]; com[i] = wt; cout<<"nProcess "<<i<<"t "<<st[i]<<"ttt"<<com[i]; brust[i] = 0; executed[i] = 1; } } } avg = avg/n; cout<<"nnThe Average Waiting Time is : "<<avg<<" ms"; } int main() { clrscr(); Round R; cout<<"nnttt Round Robin Scheduling";
  • 9. 8 R.GetData(); R.Calculate(); getch(); } Result: Thus the program for Simulation of Round-Robin Scheduling has been developed successfully and executed
  • 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;
  • 12. 11 float avg; public: SJF() { n_jobs = 0; avg = 0; } void Job_Entry(int n) { n_jobs = n; cout<<"nEnter the Burst Time of each Job :"; cout<<"n"; for(int i = 0;i < n_jobs;i++) { cout<<"nJob "<<i+1<<": "; cin>>b_time[i]; } Sorting(b_time,n); } void Sorting(int b_time[],int n) { for(int i = 0;i < n; i++) { for(int j = i+1; j < n; j++) { if(b_time[j] < b_time[i]) { int temp = b_time[i]; b_time[i] = b_time[j]; b_time[j] = temp; } } } }
  • 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];
  • 17. 16 } else { wait[i]=wait[i-1]+temp; temp=ser[j]; awt +=wait[i]; tat +=ser[i]+wait[i]; } } } awt=awt/n; tat=tat/n; } void Put() { wait[0]=0; cout<<"nnScheduling Order"; cout<<"nPID"<<"tPriority"<<"tService Time"<<"tWait time"; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(pri[i] == pri1[j]) cout<<"n"<<p[j]+1<<"t"<<pri[i]<<"tt"<<ser[j] <<"tt"<<wait[i]; } } cout<<"nnAverage Waiting Time : "<<awt<<" ms"; cout<<"nAverage Turnaround Time : "<<tat<<" ms"; } };
  • 18. 17 void main() { Priority P; clrscr(); P.Get(); P.Calculate(); P.Put(); getch(); } Result: Thus the program for simulation of Priority Scheduling has been developed successfully and executed
  • 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;
  • 21. 20 cout<<"nAllocation Matrix ["<<m<<" X "<<n<<"]n"; for(i = 0;i < m;i++) for(j = 0;j < n;j++) cin>>allocated[i][j]; cout<<"nMaximum Matrix ["<<m<<" X "<<n<<"]n"; for(i = 0;i < m;i++) for(j = 0;j < n;j++) cin>>max[i][j]; cout<<"nAvailablen"; for(i = 0;i < n;i++) { cin>>available[i]; } } void MakeNull() { for(i = 0;i < 50;i++) flag[i] = 0; } void Need() { cout<<"nNeed Matrix ["<<m<<" X "<<n<<"]n"; for(i = 0;i < m;i++) { for(j = 0;j < n;j++) { need[i][j] = max[i][j] - allocated[i][j]; cout<<need[i][j]<<"t"; } cout<<"n"; } }
  • 22. 21 int Check(int column) { for(i = 0;i < n;i++) if(need[column][i] > available[i]) return 0; return 1; } void AddResource(int column) { for(int i = 0;i < n;i++) available[i] = available[i] + allocated[column][i]; } void Process() { int t,k,l; int temp = 0; MakeNull(); cout<<"nSafety Sequence :"; for(k = 0;k < 3;k++) { temp = 0; for(l = 0;l < m;l++) { if(flag[l] == 0) { temp = 1; if(Check(l) == 1) { AddResource(l); flag[l] = 1; cout<<" P"<<l; } }
  • 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 :";
  • 26. 25 cin>>n; cout<<endl; for(i = 0;i < n;i++) for(j = 0;j < n;j++) b[i][j] = -1; for(i = 0;i < n;i++) { cout<<"Enter the Page Frame ["<<i+1<<"] :"; cin>>a[i]; } } void CopyPrevious(int r) { for(int i = 0;i < m;i++) b[r][i] = b[r-1][i]; } int Check(int r,int t) { for(int i = 0;i < m;i++) if(b[r][i] == t) return i; return -1; } void Print(int r) { cout<<"nReference String "; cout<<"<"<<a[r]<<">"<<"t"; for(int i = 0;i < m;i++) if(b[r][i] == -1) cout<<"-t";
  • 27. 26 else cout<<b[r][i]<<"t"; } void Replace() { count = -1; int set = 0; for(int i = 0;i < n;i++) { set = 0; if(count == m-1) count = -1; if(i == 0) { pf++; b[i][++count] = a[i]; set = 1; } else { CopyPrevious(i); if(Check(i,a[i]) == -1) { b[i][++count] = a[i]; pf++; set = 1; } } Print(i); if(set == 1) cout<<"Page Fault"; } cout<<"nnNumber of Page Fault :"<<pf; } };
  • 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 : ";
  • 35. 34 cin>>n; cout<<endl; for(i = 0;i < n;i++) { cout<<"Process Size ["<<i+1<<"] : "; cin>>pro[i]; dup_pro[i] = pro[i]; } } void MakeNull() { for(int i = 0;i < 100;i++) e[i] = flag[i] = 0; } void UnAllocated(int *disp) { for(int i = 0;i < n;i++) if(flag[i] == 0) cout<<"n"<<disp[i]<<" is Unallocated"; } void FirstFit() { cout<<"ntttt* First Fit *n"; MakeNull(); for(int i = 0,k = 0;i < n;i++) for(int j = 0;j < m;j++) if(pro[k] <= hol[j] && e[j] == 0 && k < n) { cout<<"nProcess "<<pro[k]<<" is mapped with Hole : "<<hol[j]; e[j] = 1; flag[k] = 1; k++;
  • 36. 35 } UnAllocated(pro); } void Sort(int *h,int Size) { int t; for(int i = 0;i < Size;i++) for(int j = 0;j < Size;j++) if(h[i] < h[j]) { t = h[i]; h[i] = h[j]; h[j] = t; } } void BestFit() { cout<<"ntttt* Best Fit *n"; Sort(dup_hol,m); Sort(dup_pro,n); MakeNull(); for(int i = 0;i < n;i++) for(int j = 0;j < m;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 = m; flag[i] = 1; } UnAllocated(dup_pro); } void WorstFit()
  • 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