0% found this document useful (0 votes)
3 views9 pages

New Os 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

New Os 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Akash Edake

Roll No: 17

Assignment no.: 08: Implement the C program for Disk Scheduling Algorithms: SSTF, SCAN,
C-Look
considering the initial head position moving away from the spindle.

CODE:
SCAN:

#include<stdio.h>
int absoluteValue(int);
void main()
{
int queue[25],n,headposition,i,j,k,seek=0, maxrange,
difference,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float averageSeekTime;
printf("Enter the maximum range of Disk: ");
scanf("%d",&maxrange);
printf("Enter the number of queue requests: ");
scanf("%d",&n);
printf("Enter the initial head position: ");
scanf("%d",&headposition);
printf("Enter the disk positions to be read(queue): ");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>headposition)
{
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;

}
}
//SORTING array queue2[] in descending order
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;
}
}
}
//Copying first array queue1[] into queue[]
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
}
queue[i]=maxrange;
for(i=temp1+2,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
}
queue[i]=0;
queue[0]=headposition;
for(j=0; j<=n; j++)
{
difference = absoluteValue(queue[j+1]-queue[j]);
seek = seek + difference;
printf("Disk head moves from position %d to %d with Seek %d \n", queue[j], queue[j+1],
difference);
}
averageSeekTime = seek/(float)n;
}
printf("Total Seek Time= %d\n", seek);
printf("Average Seek Time= %f\n", averageSeekTime);
}
int absoluteValue(int x)
{
if(x>0)
{
return x;

else
{
return x*-1;
}
}

}
OUTPUT:

Enter the maximum range of Disk: 200


Enter the number of queue requests: 7
Enter the initial head position: 50
Enter the disk positions to be read(queue): 82 170 43 140 24 16 190
Disk head moves from position 50 to 82 with Seek 32
Disk head moves from position 82 to 140 with Seek 58
Disk head moves from position 140 to 170 with Seek 30
Disk head moves from position 170 to 190 with Seek 20
Disk head moves from position 190 to 200 with Seek 10
Disk head moves from position 200 to 43 with Seek 157
Disk head moves from position 43 to 24 with Seek 19
Disk head moves from position 24 to 16 with Seek 8
Disk head moves from position 16 to 0 with Seek 16
Total Seek Time= 350
Average Seek Time= 50.000000

}
SSTF:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int queue[100], queue2[100], q_size, head, seek=0, temp; float avg;
printf("%s\n", "-----SSTF Disk Scheduling Algorithm ----- ");
printf("%s\n", "Enter the size of the queue");
scanf("%d", &q_size);
printf("%s\n", "Enter queue elements");
for(int i=0; i<q_size; i++){
scanf("%d",&queue[i]);
}
printf("%s\n","Enter initial head position");
scanf("%d", &head);
for(int i=0; i<q_size; i++){
queue2[i] = abs(head-queue[i]);
}
for(int i=0; i<q_size; i++){
for(int j=i+1; j<q_size;j++){
if(queue2[i]>queue2[j]){
temp = queue2[i];
queue2[i]=queue[j];
queue2[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(int i=1; i<q_size; i++){
seek = seek+abs(head-queue[i]);
head = queue[i];
}
printf("\nTotal seek time is %d\t",seek);
avg = seek/(float)q_size;
printf("\nAverage seek time is %f\t", avg);
return 0;
}
OUTPUT:

-----SSTF Disk Scheduling Algorithm-----


Enter the size of the queue
5
Enter queue elements
98 183 37 122 14
Enter initial head position
53

Total seek time is 236


Average seek time is 47.200001
C-LOOK:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int queue[100], queue2[100], q_size, head, seek=0, temp;
float avg;
printf("%s\n", "-----SSTF Disk Scheduling Algorithm ----- ");
printf("%s\n", "Enter the size of the queue");
scanf("%d", &q_size);
printf("%s\n", "Enter queue elements");
for(int i=0; i<q_size; i++){
scanf("%d",&queue[i]);
}
printf("%s\n","Enter initial head position");
scanf("%d", &head);
for(int i=0; i<q_size; i++){
queue2[i] = abs(head-queue[i]);
}
for(int i=0; i<q_size; i++){
for(int j=i+1; j<q_size;j++){
if(queue2[i]>queue2[j]){
temp = queue2[i];
queue2[i]=queue[j];
queue2[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(int i=1; i<q_size; i++){
seek = seek+abs(head-queue[i]);
head = queue[i];
}
printf("\nTotal seek time is %d\t",seek);
avg = seek/(float)q_size;
printf("\nAverage seek time is %f\t", avg);
return 0;
}
OUTPUT:

-----SSTF Disk Scheduling Algorithm----_


Enter the size of the queue
5
Enter queue elements
82 170 43 140 24
Enter initial head position
50
Total seek time is 258
Average seek time is 51.600000

You might also like