Program:
#include <stdio.h> #include<stdlib.h> // Function to Sort Array void sort(int arr[],int n){ for(int i=0;i<n;i++){ for(int j=0;j<n-i-1;j++){ if(arr[j]>arr[j+1]){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } // CLook // In this we go in one direction and after that // comeback to first unvisited of other direction // and goes into increment void CLook() { int n; printf("Enter number of disk requests : \n"); scanf("%d",&n); int req[n]; printf("Enter the sequence of requests : \n"); for(int i=0;i<n;i++) scanf("%d",&req[i]); int initial; printf("Enter the initial position of the head : \n"); scanf("%d",&initial); int m; printf("Enter the direction of movement of the head : 1.High 0.Low \n"); scanf("%d",&m); sort(req,n); int totalSeekTime = 0; // finding intial index int idx; for(idx=0;idx<n;idx++){ if(initial<req[idx]) break; } // Going in High Direction if(m) { // In this we go in one direction from index to end for(int i=idx;i<n;i++) { totalSeekTime += abs(req[i] - initial); printf("seek time between %d and %d is %d\n",req[i],initial,abs(req[i]- initial)); initial = req[i]; } // In this we go from 0 to index for(int i=0;i<idx;i++) { totalSeekTime += abs(req[i] - initial); printf("seek time between %d and %d is %d\n",req[i],initial,abs(req[i]- initial)); initial = req[i]; } } else { for(int i=idx-1;i>=0;i--) { totalSeekTime += abs(req[i] - initial); printf("seek time between %d and %d is %d\n",req[i],initial,abs(req[i]- initial)); initial = req[i]; } for(int i=n-1;i>=idx;i--) { totalSeekTime += abs(req[i] - initial); printf("seek time between %d and %d is %d\n",req[i],initial,abs(req[i]- initial)); initial = req[i]; } } printf("\nTotal seek time is : %d\n", totalSeekTime); printf("\nAverage seek time is : %f\n" , totalSeekTime/(0.1*10*n)); } // SCAN (Elevator) /* We go in One Direction and goes to end of it and while coming back we visit unvisited. */ void Scan() { int n; printf("Enter the number of processes : \n"); scanf("%d",&n); int size; printf("Enter the size of the disk : \n"); scanf("%d",&size); int req[n]; printf("Enter the sequence of requests : \n"); for(int i=0;i<n;i++) scanf("%d",&req[i]); int initial; printf("Enter the initial position of the head : \n"); scanf("%d",&initial); int m; printf("Enter the direction of movement of the head : 1.Right 0.Left \n"); scanf("%d",&m); sort(req,n); int idx; for(idx=0;idx<n;idx++) { if(initial<req[idx]) break; } int totalSeekTime = 0; int i; // 1- Right if(m) { // Going in One Direction for(i=idx;i<n;i++) { totalSeekTime += abs(req[i] - initial); initial = req[i]; } // going to end of the disk totalSeekTime += abs(size - req[i-1] - 1); initial = size - 1; // while coming back visit the unvisited for(i=idx-1;i>=0;i--) { totalSeekTime += abs(req[i] - initial); initial = req[i]; } } else { for(i=idx-1;i>=0;i--) { totalSeekTime += abs(req[i] - initial); initial = req[i]; } totalSeekTime += abs(req[i+1] - 0); initial = 0; for(i=idx;i<n;i++) { totalSeekTime += abs(req[i] - initial); initial = req[i]; } } printf("Total seek time is : %d",totalSeekTime); } // SSTF (Shortest Seek Time First) /* We visit the Track which is shortest to current track */ void Sstf() { int n; printf("Enter number of disk requests : \n"); scanf("%d",&n); int req[n]; printf("Enter the sequence of requests : \n"); for(int i=0;i<n;i++) scanf("%d",&req[i]); int initial; printf("Enter the initial position of the head : \n"); scanf("%d",&initial); int totalSeekTime = 0; int cnt = 0; while(cnt!=n) { int min = 10000, idx; // in this we are finding minimum difference from current head for(int i=0;i<n;i++) { int diff = abs(req[i] - initial); if(min>diff) { min = diff; idx = i; } } totalSeekTime += min; // Updating Inital initial = req[idx]; // Increased time of visited req[idx] = 1000; // counting total number of tracks visited cnt++; } printf("Total seek time : %d\n", totalSeekTime); } int main() { int ch; printf("\n\n Menu:"); printf("\n 1) CLOOK "); printf("\n 2) SSTF "); printf("\n 3) SCAN "); printf("\n\n Enter Choice:"); scanf("%d",&ch); switch (ch) { case 1: CLook(); break; case 2: Sstf(); break; case 3: Scan(); break; default: break; } return 0; }
Output: