OS Lab Manual
OS Lab Manual
Museb Khalid
[email protected]
LAB MANUAL
CPU Scheduling Algorithm - 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 firstcome, 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";
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;
int main()
{
FCFS F;
int n; clrscr();
cout<<"\n\n\t\t\tFirst Come First Served (FCFS)"; cout<<"\n\nHow 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.
Output:
Job 1: 12
Job 2: 10
Job 3: 4
Gantt chart
Burst Time | 12 | 10 | 4 |
Waiting Time 0 12 22
Aim: To develop a C++ Program for the simulation of Round Robin Scheduling
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.
DESCRIPTION:
To aim is to calculate the average waiting time. There will be a time slice, each process should
be executed within that time-slice and if not it will go to the waiting state so first check
whether the burst time is less than the time-slice. If it is less than it assign the waiting time to
the sum of the total times. If it is greater than the burst-time then subtract the time slot from the
actual burst time and increment it by time-slot and the loop continues until all the processes are
completed.
ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where No. of time slice for
process (n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate a) Waiting time for process
(n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the time difference in
getting the CPU from process(n-1) b) Turnaround time for process(n) = waiting time of
process(n) + burst time of process(n)+ the time difference in getting CPU from
process(n).
Step 7: Calculate c) Average waiting time = Total waiting Time / Number of process d)
Average Turnaround time = Total Turnaround Time / Number of process Step
8: Stop the process
Program:
#include <iostream>
using namespace std;
int main()
{
int i, j, n, bu[10], wa[10], tat[10], t, ct[10], max;
float awt = 0, att = 0, temp = 0;
cout << " Enter the no of processes";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter Burst Time for process";
cin >> bu[i];
ct[i] = bu[i];
}
cout << " Enter the size of time slice ";
cin >> t;
max = bu[0];
for (i = 1; i < n; i++)
if (max < bu[i])
max = bu[i];
for (j = 0; j < (max / t) + 1; j++)
for (i = 0; i < n; i++)
if (bu[i] != 0)
if (bu[i] <= t)
{
tat[i] = temp + bu[i];
temp = temp + bu[i];
bu[i] = 0;
}
else
{
bu[i] = bu[i] - t;
temp = temp + t;
}
for (i = 0; i < n; i++)
{
wa[i] = tat[i] - ct[i];
att += tat[i];
awt += wa[i];
}
cout << "Average Turnaround time is" << att << "\n";
cout << "\nThe Average Waiting time is " << awt << "\n";
cout << "\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n";
for (i = 0; i < n; i++)
{
cout << "\n"
<< "\t" << i + 1 << "\t" << ct[i] << "\t" << wa[i] << "\t" << tat[i];
}
}
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; 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);
}
{
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;
}
}
}
}
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;
int main()
{
SJF S;
int n; clrscr();
cout<<"\n\n\t\tShortest Job First (SJF) - Non Preemptive"; cout<<"\n\nHow many Jobs
going to be Entered : "; cin>>n; S.Job_Entry(n);
S.Wait_Time();
getch();
}
Result:
Thus the program for Simulation of SJF-non-preemptive has been developed successfully and
executed
Output:
Job 1: 10
Job 2: 5
Job 3: 15
Gantt chart
Burst Time | 5 | 10 | 15 |
Waiting Time 0 5 15
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++)
{
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];
}
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<<"\n\nScheduling 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]<<"\t\t"<<ser[j]
<<"\t\t"<<wait[i];
}
}
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
Output:
Scheduling Order
PID Priority Service Time Wait time
3 1 10 11896
2 2 20 11906
4 3 12 11926
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;
cout<<"\nAllocation Matrix ["<<m<<" X "<<n<<"]\n"; for(i = 0;i < m;i++) for(j
= 0;j < n;j++) cin>>allocated[i][j];
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";
}
}
int Check(int column)
{
for(i = 0;i < n;i++)
if(need[column][i] > available[i])
return 0; return 1;
}
void Process()
{
int t,k,l; int temp = 0;
MakeNull();
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.
Output:
Allocation Matrix [3 X 3]
100
010
0 01
Maximum Matrix [3 X 3]
1 11
111
111
Available
123
Need Matrix [3 X 3]
0 1 1
1 0 1
1 1 0
Safety Sequence : P0 P1 P2
System in Safe State
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 :";
cin>>n;
cout<<endl;
for(i = 0;i < n;i++) for(j = 0;j < n;j++) b[i][j] = -1;
void CopyPrevious(int r)
{
for(int i = 0;i < m;i++) b[r][i] = b[r-1][i];
}
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";
else
cout<<b[r][i]<<"\t";
}
void Replace()
{
count = -1; int set = 0;
Result:
Thus the program for the simulation of FIFO Page Replacement has been developed and
executed successfully.
Output:
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 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++)
{
rnk[i]=0; fr[i]=-1;
}
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
{
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.
Output:
128 -1 -1
128 100 -1
128 100 200
PROGRAM
WORST-FIT
#include <iostream>
using namespace std;
#define max 25
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
cout<<"\n\tMemory Management Scheme - First Fit";
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>nf;
cout<<"\nEnter the size of the blocks:-\n";
for (i = 1; i <= nb; i++)
{
cout<<"Block :"<< i;
cin>>b[i];
}
cout<<"Enter the size of the files :-\n";
for (i = 1; i <= nf; i++)
{
cout<<"File :"<< i;
cin>>f[i];
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
ff[i] = j;
break;
}
}
}
frag[i] = temp;
bf[ff[i]] = 1;
}
cout<<"\nFile No\tFile Size \tBlock No\tBlock Size\tFragment";
for (i = 1; i <= nf && ff[i] != 0; i++)
{
cout<<"\n" <<i<<"\t"<<f[i]<<"\t"<<ff[i]<<"\t"<<b[ff[i]]<<"\t"<<frag[i];
}
}
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT File No File Size Block No Block Size Fragment
1115424373
BEST-FIT
#include <iostream>
#define max 25
using namespace std;
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
static int bf[max], ff[max];
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>nf;
cout<<"\nEnter the size of the blocks:\n";
for (i = 1; i <= nb; i++)
{
cout<<"Block "<<":"<< i;
cin>>b[i];
}
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT File No File Size
Block No
Block Size
Fragment 1 1 2 2 1 2 4 1 5 1
FIRST-FIT
#include <iostream>
using namespace std;
#define max 25
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
static int bf[max], ff[max];
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment 1 1 3 7 6 2 4 1 5 1
ALGORITHM:
#include<iostream>
using namespace std;
int main()
cin>>choice;
switch(choice)
case 1: if((in+1)%bufsize==out)
cout<<"\nBuffer is Full";
else
{
cout<<"\nEnter the value: ";
cin>>produce;
buffer[in] = produce;
in = (in+1)%bufsize;
break;
cout<<"\nBuffer is Empty";
else
{
consume = buffer[out];
out = (out+1)%bufsize;
break;
} } }
OUTPUT:
Buffer is Empty