This is a list of programs which used in the Operating Systems lab. The course is usually taught in the 4th/5th semester of engineering. All these programs are 100% working and are tested using the Dev C++ IDE. I urge you to first try these algorithms on your own. Try to implement them, as they are the very basic algorithms which you can easily implement. If you are not able to implement these algorithms, then use these programs as a reference. I encourage you to make these programs more effecient by making some here and there tweaks. This is another way of learning.
Thanks for reading it.
Round Robin Scheduling using C++:
This is a 100% working program with the output shown in the command window.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main(void){
cout<<"Enter the number of processes:\n";
int n;
cin>>n;
int arivl[n],burst[n],end[n],remain[n],x,y,temp,time,tq;
for(x=0;x<n;x++){
cout<<"Enter arrival & burst time for process "<<x<<"\n";
cin>>arivl[x]>>burst[x];}
cout<<"Enter the time quantum:\n";
cin>>tq;
for(x=0;x<n-1;x++)
for(y=0;y<n-x-1;y++)
if(arivl[y]>arivl[y+1]){
temp=arivl[y];
arivl[y]=arivl[y+1];
arivl[y+1]=temp;
temp=burst[y];
burst[y]=burst[y+1];
burst[y+1]=temp;
}
for(x=0;x<n;x++)
remain[x]=burst[x];
for(time=arivl[0],x=0;x==0;){
for(y=0;y<n;y++)
if(remain[y]!=0&&arivl[y]<=time)
if(remain[y]<=tq){
time+=remain[y];
remain[y]=0;
end[y]=time;
}
else{
time+=tq;
remain[y]-=tq;
}
for(y=0,x=1;y<n;y++)
if(remain[y]!=0)
x=0;
}
float aw=0,at=0;
cout<<"Process\tArrival\tBurst\tWaiting\tTurnAround\n";
for(x=0;x<n;x++){
aw+=end[x]-arivl[x]-burst[x];
at+=end[x]-arivl[x];
cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t";
cout<<(end[x]-arivl[x]-burst[x])<<"\t"<<(end[x]-arivl[x])<<"\n";
}
cout<<"\nAverage Waiting Time = "<<(aw/n);
cout<<"\nAverage Turn Around Time = "<<(at/n);
getch();
return 0;
}
OUTPUT:
First Come First Serve OR FCFS Scheduling using C++:
This is also a 100% working program with the output shown in the command window.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main(){
cout<<"Enter the number of processes:\n";
int n;
cin>>n;
int arivl[n],burst[n],start[n],x,y,temp;
for(x=0;x<n;x++){
cout<<"Enter arrival & burst time for process "<<x<<"\n";
cin>>arivl[x]>>burst[x];
}
for(x=0;x<n-1;x++)
for(y=0;y<n-x-1;y++)
if(arivl[y]>arivl[y+1]){
temp=arivl[y];
arivl[y]=arivl[y+1];
arivl[y+1]=temp;
temp=burst[y];
burst[y]=burst[y+1];
burst[y+1]=temp;
}
start[0]=arivl[0];
for(x=1;x<n;x++){
start[x]=start[x-1]+burst[x-1];
if(start[x]<arivl[x])
start[x]=arivl[x];
}
float aw=0,at=0;
cout<<"Process\tArrival\tBurst\tStart\tWaiting\tTurnAround\n";
for(x=0;x<n;x++){
aw+=start[x]-arivl[x];
at+=start[x]-arivl[x]+burst[x];
cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t"<<start[x]<<"\t";
cout<<(start[x]-arivl[x])<<"\t"<<(start[x]-arivl[x]+burst[x])<<"\n";
}
cout<<"\nAverage Waiting Time = "<<(aw/n);
cout<<"\nAverage Turn Around Time = "<<(at/n);
getch();
return 0;
}
OUTPUT:
Shortest Remaining Time or SRT Scheduling using C++:
This is a 100% working program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"Enter the number of processes:\n";
int n;
cin>>n;
int arivl[n],burst[n],end[n],remain[n],x,y,loc,temp,time,small;
for(x=0;x<n;x++)
{
cout<<"Enter arrival & burst time for process "<<x<<"\n";
cin>>arivl[x]>>burst[x];
}
for(x=0;x<n-1;x++)
for(y=0;y<n-x-1;y++)
if(arivl[y]>arivl[y+1])
{
temp=arivl[y];
arivl[y]=arivl[y+1];
arivl[y+1]=temp;
temp=burst[y];
burst[y]=burst[y+1];
burst[y+1]=temp;
}
for(x=0;x<n;x++)
remain[x]=burst[x];
for(time=arivl[0],x=0;x==0;time++)
{
for(y=0,small=999,loc=-1;y<n;y++)
if(arivl[y]<=time&&remain[y]!=0&&remain[y]<small)
{
small=remain[y];
loc=y;
}
if(loc!=-1)
{
remain[loc]--;
if(remain[loc]==0)
end[loc]=time+1;
}
for(y=0,x=1;y<n;y++)
if(remain[y]!=0)
x=0;
}
float aw=0,at=0;
cout<<"Process\tArrival\tBurst\tWaiting\tTurnAround\n";
for(x=0;x<n;x++)
{
aw+=end[x]-arivl[x]-burst[x];
at+=end[x]-arivl[x];
cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t";
cout<<(end[x]-arivl[x]-burst[x])<<"\t"<<(end[x]-arivl[x])<<"\n";
}
cout<<"\nAverage Waiting Time = "<<(aw/n);
cout<<"\nAverage Turn Around Time = "<<(at/n);
getch();
return 0;
}
OUTPUT:
Shorest Job First or SJF Scheduling using C++:
100% working program with the output below.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"Enter the number of processes:\n";
int n;
cin>>n;
int arivl[n],burst[n],start[n],x,y,loc,temp,time,small;
for(x=0;x<n;x++)
{
cout<<"Enter arrival & burst time for process "<<x<<"\n";
cin>>arivl[x]>>burst[x];
start[x]=-1;
}
for(x=0;x<n-1;x++)
for(y=0;y<n-x-1;y++)
if(arivl[y]>arivl[y+1])
{
temp=arivl[y];
arivl[y]=arivl[y+1];
arivl[y+1]=temp;
temp=burst[y];
burst[y]=burst[y+1];
burst[y+1]=temp;
}
for(time=arivl[0],x=0;x==0;)
{
for(y=0,small=999,loc=-1;y<n;y++)
if(arivl[y]<=time&&start[y]==-1&&burst[y]<small)
{
small=burst[y];
loc=y;
}
if(loc==-1)
time++;
else
{
start[loc]=time;
time+=burst[loc];
}
for(y=0,x=1;y<n;y++)
if(start[y]==-1)
x=0;
}
float aw=0,at=0;
cout<<"Process\tArrival\tBurst\tStart\tWaiting\tTurnAround\n";
for(x=0;x<n;x++)
{
aw+=start[x]-arivl[x];
at+=start[x]-arivl[x]+burst[x];
cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t"<<start[x]<<"\t";
cout<<(start[x]-arivl[x])<<"\t"<<(start[x]-arivl[x]+burst[x])<<"\n";
}
cout<<"\nAverage Waiting Time = "<<(aw/n);
cout<<"\nAverage Turn Around Time = "<<(at/n);
getch();
return 0;
}
OUTPUT:
Banker's Algorithm using C++:
One of the most complicated of all programs. Rarely you'll find this program online working. But this algorithm is 100% working and is implemented using Dev C++.
PROGRAM:
#include<iostream>
using namespace std;
#include<conio.h>
#define MAX 20
class bankers
{private:
int al[MAX][MAX],m[MAX][MAX],n[MAX][MAX],avail[MAX];
int nop,nor,k,result[MAX],pnum,work[MAX],finish[MAX];
public:
bankers();
void input();
void method();
int search(int);
void display(); };
bankers::bankers(){
k=0;
for(int i=0;i<MAX;i++){
for(int j=0;j<MAX;j++){
al[i][j]=0;
m[i][j]=0;
n[i][j]=0;
}
avail[i]=0;
result[i]=0;
finish[i]=0; }}
void bankers::input(){
int i,j;
cout<<"Enter the number of processes:";
cin>>nop;
cout<<"Enter the number of resources:";
cin>>nor;
cout<<"Enter the allocated resources for each process: "<<endl;
for(i=0;i<nop;i++){
cout<<"\nProcess "<<i;
for(j=0;j<nor;j++){
cout<<"\nResource "<<j<<":";
cin>>al[i][j]; }}
cout<<"Enter the maximum resources that are needed for each process: "<<endl;
for(i=0;i<nop;i++){
cout<<"\nProcess "<<i;
for(j=0;j<nor;j++){
cout<<"\nResouce "<<j<<":";
cin>>m[i][j];
n[i][j]=m[i][j]-al[i][j]; }}
cout<<"Enter the currently available resources in the system: ";
for(j=0;j<nor;j++){
cout<<"Resource "<<j<<":";
cin>>avail[j];
work[j]=-1; }
for(i=0;i<nop;i++)
finish[i]=0;
}
void bankers::method(){
int i=0,j,flag;
while(1){
if(finish[i]==0){
pnum =search(i);
if(pnum!=-1){
result[k++]=i;
finish[i]=1;
for(j=0;j<nor;j++){
avail[j]=avail[j]+al[i][j]; }}}
i++;
if(i==nop){
flag=0;
for(j=0;j<nor;j++)
if(avail[j]!=work[j])
flag=1;
for(j=0;j<nor;j++)
work[j]=avail[j];
if(flag==0)
break;
else
i=0; }}}
int bankers::search(int i){
int j;
for(j=0;j<nor;j++)
if(n[i][j]>avail[j])
return -1;
return 0; }
void bankers::display(){
int i,j;
cout<<endl<<"OUTPUT:";
cout<<endl<<"========";
cout<<endl<<"PROCESS\t ALLOTED\t MAXIMUM\t NEED";
for(i=0;i<nop;i++){
cout<<"\nP"<<i+1<<"\t ";
for(j=0;j<nor;j++){
cout<<al[i][j]<<" "; }
cout<<"\t ";
for (j=0;j<nor;j++){
cout<<m[i][j]<<" "; }
cout<<"\t ";
for(j=0;j<nor;j++ ){
cout<<n[i][j]<<" "; }}
cout<<"\nThe sequence of the safe processes are: \n";
for(i=0;i<k;i++){
int temp = result[i]+1 ;
cout<<"P"<<temp<<" "; }
cout<<"\nThe sequence of unsafe processes are: \n";
int flg=0;
for (i=0;i<nop;i++){
if(finish[i]==0){
flg=1; }
cout<<"P"<<i<<" "; }
cout<<endl<<"RESULT:";
cout<<endl<<"=======";
if(flg==1)
cout<<endl<<"The system is not in safe state and deadlock may occur!!";
else
cout<<endl<<"The system is in safe state and deadlock will not occur!!"; }
int main(){
cout<<" DEADLOCK BANKER’S ALGORITHM "<<endl;
bankers B;
B.input ( );
B.method ( );
B.display ( );
getch ( );
}
OUTPUT:
Least Frequently Used or LFU Page Replacement Algorithm using C++:
100% working program but wasn't able to take a screenshot of the output.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
int n,a,b,c,x,y;
cout<<"Enter the capacity of cache:\n";
cin>>n;
int cac[n],lis[n],fre[n];
cout<<"Enter initial cache pages:\n";
for(b=0;b<n;b++)
{
cin>>cac[b];
lis[b]=cac[b];
}
cout<<"Enter the number of new entries:\n";
cin>>a;
int ent[a];
cout<<"Enter new entries of pages:\n";
for(b=0;b<a;b++)
cin>>ent[b];
for(b=0;b<n;b++)
fre[b]=1;
cout<<"\nInitial cache and LFU list:\n";
for(b=0;b<n;b++)
cout<<cac[b]<<" ";
cout<<"\t";
for(b=0;b<n;b++)
cout<<lis[b]<<" ";
cout<<"\n";
for(b=0;b<a;b++)
{
for(x=0,c=0,y=0;x<n;x++)
if(lis[x]==ent[b])
{
c=1;
y=x;
}
if(c!=0)
fre[y]++;
else
{
for(x=0;x<n;x++)
if(cac[x]==lis[0])
cac[x]=ent[b];
for(x=0;x<n-1;x++)
{
lis[x]=lis[x+1];
fre[x]=fre[x+1];
}
lis[x]=ent[b];
fre[x]=1;
}
for(x=0;x<n;x++)
for(y=0;y<n-x-1;y++)
if(fre[y]>fre[y+1])
{
c=fre[y];
fre[y]=fre[y+1];
fre[y+1]=c;
c=lis[y];
lis[y]=lis[y+1];
lis[y+1]=c;
}
cout<<"\nCache and LFU list at entry "<<ent[b]<<":\n";
for(x=0;x<n;x++)
cout<<cac[x]<<" ";
cout<<"\t";
for(x=0;x<n;x++)
cout<<lis[x]<<" ";
cout<<"\n";
}
getch();
return 0;
}
No comments:
Post a Comment