Professional Documents
Culture Documents
OS Experiments New - 1554792526
OS Experiments New - 1554792526
OPERATING SYSTEM
EXPERIMENTS
CHAMELI DEVI GROUP OF INSTITUTION
5 SEMESTER - RGPV
Following Programs are compiled using ‘gcc’ compiler.
Practical: 1
Algorithm:
Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.
Step 5: Calculate the waiting time and turnaround time and display it.
Step 6: Calculate the average waiting time and average turnaround time and display it.
Source Code:
[Step-1]
#include<iostream>
int main()
{ [Step-2]
int n;
cin>>n;
cin>>at[i]>>bt[i];
ct[-1] = 0;
[Step-5]
[Step-6]
avg_wt = total_wt/n;
avg_tat = total_tat/n;
}
OUTPUT:
Practical : 2
Algorithm:
Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.
Source Code:
[Step-1]
#include<iostream>
int main()
[Step-2]
Int n;
cin>>n;
int bt[20],p[20],wt[20],tat[20],total=0,pos,temp;
float avg_wt,avg_tat;
[Step-3]
for(int i=0;i<n;i++)
cin>>bt[i];
[Step-4]
//sorting burst time in ascending order using selection sort (to put shortest burst time first)
for(int i=0;i<n;i++)
pos=i;
for(int j=i+1;j<n;j++)
if(bt[j]<bt[pos])
pos=j;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
[Step-5]
for(int i=1;i<n;i++)
wt[i]=0;
for(int j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
[Step-6]
for(int i=0;i<n;i++)
total+=tat[i];
cout<<"\n P"<<p[i]<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
OUTPUT:
Practical : 3
Algorithm:
Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.
Source Code:
[Step-1]
#include<iostream>
int main()
{ [Step-2]
int n;
cin>>n;
int bt[n],p[n],wt[n],tat[n],pr[n],i,j,pos,temp;
float avg_wt,avg_tat,total=0;
for(i=0;i<n;i++)
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
[Step-5]
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
pos=i;
for(j=i+1;j<n;j++)
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
[Step-6]
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
[Step-7]
for(i=0;i<n;i++)
total+=tat[i];
return 0;
OUTPUT:
Practical : 4
Algorithm:
Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.
Step 4: Read Arrival Time and Burst Time for all process.
Source Code:
[Step-1]
#include<iostream>
int main()
int count,j,n,time,remain,flag=0,tq;
[Step-2]
cin>>n;
remain=n;
int wait_time=0,turnaround_time=0,at[n],bt[n],rt[n];
for(count=0;count<n;count++)
{ [Step-4]
cout<<"Enter Arrival Time and Burst Time for Process No. "<<count+1<<" :";
cin>>at[count];
cin>>bt[count];
}
[Step-3]
cin>>tq;
for(time=0,count=0;remain!=0;)
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=tq;
time+=tq;
remain--;
cout<<"P["<<count+1<<"]\t|\t"<<time-at[count]<<"\t|\t"<<time-at[count]-
bt[count]<<"\n";
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
OUTPUT:
Practical : 5
Write a program to compare various CPU Scheduling Algorithms over different Scheduling Criteria.
Algorithm:
Step 1: Start the program.
Step 2: Define various CPU scheduling functions.
Step 3: Input no. of process ‘n’ and declare array of ‘n’ size.
Step 4: Using switch case select CPU scheduling.
Step 5: End program.
Source Code :
[Step-1]
#include<iostream>
[Step-2]
int main()
{ [Step-3]
Int n,s;
cin>>n;
[Step-4]
cin>>s;
switch(s)
case ‘1’ :
fcfs(int n);
break;
case ‘2’ :
sjf(int n);
break;
case ‘3’ :
priority(int n);
break;
case ‘4’ :
round_robin(int n);
break;
OUTPUT:
Practical : 6
Write a program to implement classical inter process communication problem (producer consumer).
Algorithm:
Step 3: Function producer generate a piece of data (item), put it into buffer and start again.
Step 4: Function consumer consuming the data (i.e. remove item from buffer), one piece at a time.
Source Code:
[Step-1]
#include<stdio.h>
#include<conio.h>
int main()
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("1.Producer\n2.Consumer\n3.Exit\n");
[Step-2]
while(1)
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!\n");
break;
case 3: [Step-5]
break;
return 0;
int wait(int s)
return (--s);
int signal(int s)
return(++s);
[Step-3]
void producer()
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
mutex=signal(mutex);
[Step-4]
void consumer()
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
x--;
mutex=signal(mutex);
OUTPUT:
Practical: 10
Write a program to implement & Compare various Disk & Drum scheduling Algorithms
Aim is to provide total head movement in different disk scheduling algorithms namely FCFS, SSTF,
LOOK, C-LOOK, SCAN, C-SCAN.
Algorithm:
Step 1: Start the program.
Step 2: Input Disk limit.
Step 3: Input total no. of requests.
Step 4: Input initial head location and its current direction of movement.
Step 5: Sort requests in ascending order.
Step 6: Apply different scheduling approaches (FCFS, SSTF, LOOK, C-LOOK, SCAN, C-SCAN).
Step 7: Stop the program.
Source Code:
[Step-1]
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <math.h>
[Step-4]
current_location:
cout<<"\nEnter current location : ";
cin>>request[0];
sorted_request[0]=request[0];
if(request[0]>max||request[0]<0)
goto current_location;
current_direction:
cout<<"\nEnter current direction(0:left / 1:right) : ";
cin>>direction;
if(direction!=0&&direction!=1)
goto current_direction;
for(int i=1;i<=number_of_request;i++)
{
cout<<"\nEnter request number "<<i<<" : ";
cin>>request[i];
sorted_request[i]=request[i];
if(request[i]>max||request[i]<0)
{
cout<<"\nInvalid request !! Enter again!!";
i--;
}
}
[Step-5]
qsort(sorted_request+1,number_of_request,sizeof(int),compare);
}
[Step-6]
int fcfs()
{
int head_movement=0;
for(int i=1;i<=number_of_request;i++)
head_movement+=abs(request[i]-request[i-1]);
return head_movement;
}
[Step-6]
int sstf()
{
int head_movement=0,flag=0,nor=number_of_request;
int request[101];
request[0]=sorted_request[0];
for(int i=1;i<=number_of_request;i++)
{
if(sorted_request[i]>sorted_request[0]&&flag==0)
flag=i;
request[i]=sorted_request[i];
}
while(nor)
{
if(flag==0)
{
head_movement+=request[0]-request[nor];
request[0]=request[nor];
}
else if(flag==1)
{
head_movement+=abs(request[nor]-request[0]);
break;
}
else if((request[flag]-request[0])>(request[0]-request[flag-1]))
{
head_movement+=request[0]-request[flag-1];
request[0]=request[flag-1];
flag--;
shift(request+flag,nor-flag);
}
else
{
head_movement+=request[flag]-request[0];
request[0]=request[flag];
shift(request+flag,nor-flag);
}
nor--;
}
return head_movement;
}
[Step-6]
int SCAN()
{
int head_movement=0,flag=0;
for(int i=1;i<=number_of_request;i++)
if(sorted_request[i]>sorted_request[0]&&flag==0)
flag=i;
if(direction==1)
{
if(flag==1)
head_movement+=sorted_request[number_of_request]-sorted_request[0];
else
{
head_movement+=max-sorted_request[0];
head_movement+=max-sorted_request[1];
}
}
else
{
if(flag==0)
head_movement+=abs(sorted_request[number_of_request]-sorted_request[0]);
else
{
head_movement+=sorted_request[0];
head_movement+=sorted_request[number_of_request];
}
}
return head_movement;
}
[Step-6]
int CSCAN()
{
int head_movement=0,flag=0;
for(int i=1;i<=number_of_request;i++)
if(sorted_request[i]>sorted_request[0]&&flag==0)
flag=i;
if(flag==1)
head_movement+=sorted_request[number_of_request]-sorted_request[0];
else
{
head_movement+=max-sorted_request[0];
head_movement+=max;
head_movement+=max-sorted_request[flag-1];
}
return head_movement;
}
[Step-6]
int LOOK()
{
int head_movement=0,flag=0;
for(int i=1;i<=number_of_request;i++)
if(sorted_request[i]>sorted_request[0]&&flag==0)
flag=i;
if(direction==1)
{
if(flag==1)
head_movement+=sorted_request[number_of_request]-sorted_request[0];
else
{
head_movement+=sorted_request[number_of_request]-sorted_request[0];
head_movement+=sorted_request[number_of_request]-sorted_request[1];
}
}
else
{
if(flag==0)
head_movement+=abs(sorted_request[number_of_request]-sorted_request[0]);
else
{
head_movement+=sorted_request[1];
head_movement+=sorted_request[number_of_request]-sorted_request[1];
}
}
return head_movement;
}
[Step-6]
int CLOOK()
{
int head_movement=0,flag=0;
for(int i=1;i<=number_of_request;i++)
if(sorted_request[i]>sorted_request[0]&&flag==0)
flag=i;
if(flag==1)
head_movement+=sorted_request[number_of_request]-sorted_request[0];
else
{
head_movement+=sorted_request[number_of_request]-sorted_request[0];
head_movement+=sorted_request[number_of_request]-sorted_request[1];
head_movement+=sorted_request[flag-1]-sorted_request[1];
}
return head_movement;
}
~disk(){}
};
int main()
{
disk hdd;
hdd.receive_request();
cout<<"Total head movement in ";
cout<<"FCFS is "<<hdd.fcfs()<<endl;
cout<<"Total head movement in ";
cout<<"SSTF is "<<hdd.sstf()<<endl;
cout<<"Total head movement in ";
cout<<"SCAN is "<<hdd.SCAN()<<endl;
cout<<"Total head movement in ";
cout<<"CSCAN is "<<hdd.CSCAN()<<endl;
cout<<"Total head movement in ";
cout<<"LOOK is "<<hdd.LOOK()<<endl;
cout<<"Total head movement in ";
cout<<"CLOOK is "<<hdd.CLOOK()<<endl;
_getch();
}
OUTPUT:
Practical : 11
Algorithm:
Step 3: Define allocation, max matrix by ‘n’ and available array by ‘r’.
Source Code:
[Step-1]
#include<iostream>
int main()
cout<<"Banker's Algo.\n";
int n,r;
[Step-2]
cin>>n;
cin>>r;
[Step-3]
int allocation[n][r],max[n][r],available[r],need[n][r],work[r];
int i,j,safe[n];
bool finish[n] ;
[Step-4]
for(j=0;j<r;j++)
cin>>allocation[i][j];
for(i=0;i<n;i++)
for(j=0;j<r;j++)
cin>>max[i][j];
for(j=0;j<r;j++)
cin>>available[j];
[Step-5]
for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-allocation[i][j];
cout<<need[i][j]<<" ";
}
cout<<endl;
[Step-6]
for(i=0;i<n;i++)
work[i]=available[i];
[Step-7]
int count = 0;
if (finish[p] == 0)
break;
if (j == r)
work[k] += allocation[p][k];
safe[count++] = p;
finish[p] = 1;
found = true;
if (found == false)
return false;
OUTPUT: