SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Architecture of operating system
Architecture of operating systemArchitecture of operating system
Architecture of operating systemSupriya Kumari
 
cpu scheduling
cpu schedulingcpu scheduling
cpu schedulinghashim102
 
2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software conceptsPrajakta Rane
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memorysgpraju
 
Distributed data processing
Distributed data processingDistributed data processing
Distributed data processingAyisha Kowsar
 
Distributed design alternatives
Distributed design alternativesDistributed design alternatives
Distributed design alternativesPooja Dixit
 
An IP datagram has arrived with the following information in the heade.docx
An IP datagram has arrived with the following information in the heade.docxAn IP datagram has arrived with the following information in the heade.docx
An IP datagram has arrived with the following information in the heade.docxshawnk7
 
Inter-Process Communication in distributed systems
Inter-Process Communication in distributed systemsInter-Process Communication in distributed systems
Inter-Process Communication in distributed systemsAya Mahmoud
 
Introduction to Software Project Management
Introduction to Software Project ManagementIntroduction to Software Project Management
Introduction to Software Project ManagementSaadi Jadoon
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronizationShakshi Ranawat
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 

Tendances (20)

Architecture of operating system
Architecture of operating systemArchitecture of operating system
Architecture of operating system
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Linux commands
Linux commandsLinux commands
Linux commands
 
cpu scheduling
cpu schedulingcpu scheduling
cpu scheduling
 
2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Distributed data processing
Distributed data processingDistributed data processing
Distributed data processing
 
Distributed design alternatives
Distributed design alternativesDistributed design alternatives
Distributed design alternatives
 
An IP datagram has arrived with the following information in the heade.docx
An IP datagram has arrived with the following information in the heade.docxAn IP datagram has arrived with the following information in the heade.docx
An IP datagram has arrived with the following information in the heade.docx
 
Inter-Process Communication in distributed systems
Inter-Process Communication in distributed systemsInter-Process Communication in distributed systems
Inter-Process Communication in distributed systems
 
Spm unit2
Spm unit2Spm unit2
Spm unit2
 
Introduction to Software Project Management
Introduction to Software Project ManagementIntroduction to Software Project Management
Introduction to Software Project Management
 
BANKER'S ALGORITHM
BANKER'S ALGORITHMBANKER'S ALGORITHM
BANKER'S ALGORITHM
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
PAC Learning
PAC LearningPAC Learning
PAC Learning
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus de 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
 

Dernier

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Dernier (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

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