2018IMT-024
2018IMT-024
2018IMT-024
3.Scan Scheduling
#include <bits/stdc++.h>
using namespace std;
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;
int main()
{
cout<<"Initial Head Position: ";
cin>>head;
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 :
}
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];
}