Modified Lab 1 IS
This commit is contained in:
parent
fdc634b589
commit
933a52b3a8
8 changed files with 166 additions and 384 deletions
|
@ -1,234 +1,11 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
void sstf(int requests[], int n, int head) {
|
||||
int total_seek = 0;
|
||||
int completed = 0;
|
||||
int visited[100] = {0};
|
||||
int current = head;
|
||||
|
||||
printf("\nSSTF Disk Scheduling\n");
|
||||
printf("Seek Sequence: %d", head);
|
||||
|
||||
while (completed < n) {
|
||||
int min_distance = INT_MAX;
|
||||
int min_index = -1;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!visited[i]) {
|
||||
int distance = abs(requests[i] - current);
|
||||
if (distance < min_distance) {
|
||||
min_distance = distance;
|
||||
min_index = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
visited[min_index] = 1;
|
||||
current = requests[min_index];
|
||||
total_seek += min_distance;
|
||||
completed++;
|
||||
printf(" -> %d", current);
|
||||
}
|
||||
|
||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
||||
}
|
||||
|
||||
void scan(int requests[], int n, int head, int disk_size) {
|
||||
int total_seek = 0;
|
||||
int direction = 1; // 1 for moving right, 0 for moving left
|
||||
int current = head;
|
||||
|
||||
printf("\nSCAN Disk Scheduling\n");
|
||||
printf("Seek Sequence: %d", head);
|
||||
|
||||
// Sort requests
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (requests[j] > requests[j + 1]) {
|
||||
int temp = requests[j];
|
||||
requests[j] = requests[j + 1];
|
||||
requests[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find position of head in sorted array
|
||||
int index;
|
||||
for (index = 0; index < n; index++) {
|
||||
if (requests[index] >= head)
|
||||
break;
|
||||
}
|
||||
|
||||
// Move right
|
||||
for (int i = index; i < n; i++) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
// Move to the end of disk
|
||||
printf(" -> %d", disk_size - 1);
|
||||
total_seek += abs(disk_size - 1 - head);
|
||||
head = disk_size - 1;
|
||||
|
||||
// Move left
|
||||
for (int i = index - 1; i >= 0; i--) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
||||
}
|
||||
|
||||
void cscan(int requests[], int n, int head, int disk_size) {
|
||||
int total_seek = 0;
|
||||
int current = head;
|
||||
|
||||
printf("\nC-SCAN Disk Scheduling\n");
|
||||
printf("Seek Sequence: %d", head);
|
||||
|
||||
// Sort requests
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (requests[j] > requests[j + 1]) {
|
||||
int temp = requests[j];
|
||||
requests[j] = requests[j + 1];
|
||||
requests[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find position of head in sorted array
|
||||
int index;
|
||||
for (index = 0; index < n; index++) {
|
||||
if (requests[index] >= head)
|
||||
break;
|
||||
}
|
||||
|
||||
// Move right
|
||||
for (int i = index; i < n; i++) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
// Move to the end of disk
|
||||
printf(" -> %d", disk_size - 1);
|
||||
total_seek += abs(disk_size - 1 - head);
|
||||
|
||||
// Move to the beginning
|
||||
printf(" -> 0");
|
||||
total_seek += disk_size - 1;
|
||||
head = 0;
|
||||
|
||||
// Move right again
|
||||
for (int i = 0; i < index; i++) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
||||
}
|
||||
|
||||
void clook(int requests[], int n, int head) {
|
||||
int total_seek = 0;
|
||||
int current = head;
|
||||
|
||||
printf("\nC-LOOK Disk Scheduling\n");
|
||||
printf("Seek Sequence: %d", head);
|
||||
|
||||
// Sort requests
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (requests[j] > requests[j + 1]) {
|
||||
int temp = requests[j];
|
||||
requests[j] = requests[j + 1];
|
||||
requests[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find position of head in sorted array
|
||||
int index;
|
||||
for (index = 0; index < n; index++) {
|
||||
if (requests[index] >= head)
|
||||
break;
|
||||
}
|
||||
|
||||
// Move right
|
||||
for (int i = index; i < n; i++) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
// Move to first request
|
||||
for (int i = 0; i < index; i++) {
|
||||
current = requests[i];
|
||||
printf(" -> %d", current);
|
||||
total_seek += abs(current - head);
|
||||
head = current;
|
||||
}
|
||||
|
||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int requests[100], n, head, disk_size, choice;
|
||||
|
||||
printf("Enter the number of disk requests: ");
|
||||
scanf("%d", &n);
|
||||
|
||||
printf("Enter the disk requests: ");
|
||||
for (int i = 0; i < n; i++) {
|
||||
scanf("%d", &requests[i]);
|
||||
}
|
||||
|
||||
printf("Enter the initial head position: ");
|
||||
scanf("%d", &head);
|
||||
|
||||
printf("Enter the disk size (0 to size-1): ");
|
||||
scanf("%d", &disk_size);
|
||||
|
||||
do {
|
||||
printf("\n\nDisk Scheduling Algorithms\n");
|
||||
printf("1. SSTF (Shortest Seek Time First)\n");
|
||||
printf("2. SCAN\n");
|
||||
printf("3. C-SCAN\n");
|
||||
printf("4. C-LOOK\n");
|
||||
printf("5. Exit\n");
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
sstf(requests, n, head);
|
||||
break;
|
||||
case 2:
|
||||
scan(requests, n, head, disk_size);
|
||||
break;
|
||||
case 3:
|
||||
cscan(requests, n, head, disk_size);
|
||||
break;
|
||||
case 4:
|
||||
clook(requests, n, head);
|
||||
break;
|
||||
case 5:
|
||||
printf("Exiting program...\n");
|
||||
break;
|
||||
default:
|
||||
printf("Invalid choice!\n");
|
||||
}
|
||||
} while (choice != 5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#include <math.h>
|
||||
#define MAX 100
|
||||
void sstf(int a[],int n,int h){int v[MAX]={0},t=0,c=h;printf("\nSSTF:\n%d ",c);for(int i=0;i<n;i++){int x=-1,m=1e9;for(int j=0;j<n;j++){if(!v[j]){int d=abs(a[j]-c);if(d<m){m=d;x=j;}}}v[x]=1;t+=m;c=a[x];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||
void sortAsc(int a[],int n){for(int i=0;i<n-1;i++)for(int j=i+1;j<n;j++)if(a[i]>a[j]){int t=a[i];a[i]=a[j];a[j]=t;}}
|
||||
void sortDesc(int a[],int n){for(int i=0;i<n-1;i++)for(int j=i+1;j<n;j++)if(a[i]<a[j]){int t=a[i];a[i]=a[j];a[j]=t;}}
|
||||
void scan(int a[],int n,int h,int d){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortDesc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nSCAN:\n%d ",c);for(int i=0;i<x;i++){t+=abs(c-l[i]);c=l[i];printf("->%d ",c);}if(c){t+=c;c=0;printf("->%d ",c);}for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||
void cscan(int a[],int n,int h,int d){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortAsc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nCSCAN:\n%d ",c);for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}if(c!=d-1){t+=abs(d-1-c);c=d-1;printf("->%d ",c);}t+=d-1;c=0;printf("->%d ",c);for(int i=0;i<x;i++){t+=abs(l[i]-c);c=l[i];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||
void clook(int a[],int n,int h){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortAsc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nCLOOK:\n%d ",c);for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}if(x){t+=abs(c-l[0]);c=l[0];printf("->%d ",c);for(int i=1;i<x;i++){t+=abs(l[i]-c);c=l[i];printf("->%d ",c);}}printf("\nTotal:%d\n",t);}
|
||||
int main(){int c,n,h,d,a[MAX];printf("1.SSTF\n2.SCAN\n3.CSCAN\n4.CLOOK\nChoice:");scanf("%d",&c);printf("Requests:");scanf("%d",&n);printf("Queue:");for(int i=0;i<n;i++)scanf("%d",&a[i]);printf("Head:");scanf("%d",&h);if(c==2||c==3){printf("Size:");scanf("%d",&d);}switch(c){case 1:sstf(a,n,h);break;case 2:scan(a,n,h,d);break;case 3:cscan(a,n,h,d);break;case 4:clook(a,n,h);break;default:printf("Invalid\n");}return 0;}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue