2018IMT-024

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

OS LAB ASSIGNMENT

DISK SCHEDULING CODES:


1.FCFS (First Come First Serve) Scheduling:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
Output :

2.SSTF (Shortest Seek Time First) Scheduling:


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}
Output :

3.Scan Scheduling
#include <bits/stdc++.h>
using namespace std;

const int N=100005;

int n;
int head;
int direction;
int done[N];
int positions[N];

void scan(void)
{
int movement,best,index,complete;

movement = 0;
complete = 0;
while(complete < n)
{
index = -1;
if(direction == 0) best = INT_MIN;
if(direction == 1) best = INT_MAX;

for(int i=0; i<n; i++)


{
if(!done[i])
{
if(direction == 0 && positions[i] > best && positions[i] < head)
{
index = i;
best = positions[i];
}
if(direction == 1 && positions[i] < best && positions[i] > head)
{
index = i;
best = positions[i];
}
}
}
if(index >= 0)
{
complete++;
done[index] = true;
movement += abs(head - best);
head = positions[index];
}
else
{
direction = 1 - direction;
}
}
cout<<"Total Head Movement "<<movement<<" Cylinders\n";
return;
}

int main()
{
cout<<"Initial Head Position: ";
cin>>head;

cout<<"Initial Direction [0/1]: ";


cin>>direction;

cout<<"Queue Size: ";


cin>>n;

cout<<"Queue:\n";
for(int i=0; i<n; i++) cin>>positions[i];
scan();

return 0;
}

Output :

4.Cscan Scheduling:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

Output :

5.Look Scheduling:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),l;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
a.push_back(h);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
l.push_back(a[i]);
}
for(i=k-1;i>=0;i--){
l.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
l.push_back(a[i]);
}
for(i=k+1;i<a.size();i++){
l.push_back(a[i]);
}
}
int temp=l[0];
cout<<temp;
for(i=1;i<l.size();i++){
cout<<" -> "<<l[i]<<' ';
sum+=abs(l[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
return 0;
}
Output :

6.C-look Scheduling:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),l;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
a.push_back(h);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
l.push_back(a[i]);
}
for(i=0;i<k;i++){
l.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
l.push_back(a[i]);
}
for(i=a.size()-1;i>k;i--){
l.push_back(a[i]);
}
}
int temp=l[0];
cout<<temp;
for(i=1;i<l.size();i++){
cout<<" -> "<<l[i]<<' ';
sum+=abs(l[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
return 0;
}
Output :

CPU SCHEDULING CODES:


1.FCFS (First Come First Serve) Scheduling:
: #include<stdio.h>
void main()
{
int bt[10],ct[10],at[10]={0},turnaroundtime[10],i,j,n,wt[10];
float atat=0,awt=0;
printf("\nEnter number of processes:");
scanf("%d",&n);
printf("\nEnter burst time of processes:");
for(i=0;i<n;i++)
{
printf("\nP%d:",i+1);
scanf("%d",&bt[i]);
}
printf("\nEnter arrival time of processes:");
for(i=0;i<n;i++)
{
printf("\nP%d:",i+1);
scanf("%d",&at[i]);
}
ct[0]=bt[0]+at[0];
for(i=1;i<n;i++)
{
ct[i]=ct[i-1]+bt[i];
}
for(i=0;i<n;i++)
{
turnaroundtime[i]=ct[i]-at[i];
wt[i]=turnaroundtime[i]-bt[i];
awt=awt+wt[i];
atat=atat+turnaroundtime[i];
}
printf("Process Arrival Burst_Time Turn_Around_Time Waiting_Time\n");
for(i=0;i<n;i++)
{
printf("P%d %d %d %d %d\n",i+1,at[i],bt[i],turnaroundtime[i],wt[i]);
}
awt=awt/n;
atat=atat/n;
printf("Average waiting time:%.2f\nAverage Turn around time:%.2f",awt,atat);

}
Output :

2.SJF (Shortest Job First) Scheduling:


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& execution time:");
//flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
Output :

3.Priorty Scheduling:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
//flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{

if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

Output :
4.Round Robin Scheduling:
#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);

printf("Arrival Time:\t");

scanf("%d", &arrival_time[i]);

printf("Burst Time:\t");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];
}

printf("\nEnter Time Quantum:\t");


scanf("%d", &time_quantum);
printf("\nProcess ID\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t%d\t %d\t %d", i + 1, burst_time[i], total - arrival_time[i], total -
arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}

average_wait_time = wait_time * 1.0 / limit;


average_turnaround_time = turnaround_time * 1.0 / limit;
printf("\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy