Assignment No:3
Slot-I
i. Write an OS program to implement FCFS Disk Scheduling algorithm.
CODE:
๐
- #include<stdio.h>
- #include<stdlib.h>
- void main()
- {
- int req[20],cpos,i,n,j,index,dir,temp,headmove=0,size=200;
- printf("\nEnter number of requests");
- scanf("%d",&n);
- printf("\n Enter request ");
- for(i=0;i<n;i++)
- scanf("%d",&req[i]);
- printf("\nEnter curreent position");
- scanf("%d",&cpos);
- for(i=0;i<n;i++)
- {
- headmove=headmove+abs(cpos-req[i]);
- cpos=req[i];
- }
- printf("\nNumber of head movements=%d",headmove);
- }
ii. Write an OS program to implement SSTF algorithm Disk Scheduling algorithm.
CODE:
๐
- #include<stdio.h>
- #include<stdlib.h>
- int main()
- {
- int req[20],cpos,i,n,headmove=0,index,count=0,min,d;
- printf("Enter the number of requests\n");
- scanf("%d",&n);
- printf("Enter the sequence of requests\n");
- for(i=0;i<n;i++)
- scanf("%d",&req[i]);
- printf("Enter initial head position\n");
- scanf("%d",&cpos);
- while(count!=n)
- {
- int min=1000,d,intex;
- for(i=0;i<n;i++)
- {
- d=abs(req[i]-cpos);
- if(min>d)
- {
- min=d;
- index=i;
- }
- }
- headmove=headmove+min;
- cpos=req[index];
- req[index]=1000;
- count++;
- }
- printf("Total head movements is %d",headmove);
- return 0;
- }
Slot-II
i. Write an OS program to implement SCAN Disk Scheduling algorithm.
CODE:
๐
- #include<stdio.h>
- #include<stdlib.h>
- int main()
- {
- int req[20],cpos,i,n,headmove=0,index,dir,j,temp,size=20;
- printf("Enter the number of requests\n");
- scanf("%d",&n);
- printf("Enter the sequence of requests\n");
- for(i=0;i<n;i++)
- scanf("%d",&req[i]);
- printf("Enter initial head position\n");
- scanf("%d",&cpos);
- printf("Enter head movement direction 0 for left and 1 for right");
- scanf("%d",&dir);
- req[n]=cpos;
- n=n+1;
- //sort request array
- for(i=0; i<n; i++)
- {
- for(j=i+1; j<n; j++)
- {
- if(req[i]>req[j])
- {
- temp=req[i];
- req[i]=req[j];
- req[j]=temp;
- }
- }
- }
- for(i=0; i<n; i++)
- {
- if(req[i]==cpos)
- {
- index=i;
- break;
- }
- }
- if(dir==0)
- {
- for(i=index-1; i>=0; i--)
- {
- headmove=headmove+abs(cpos-req[i]);
- cpos=req[i];
- }
- headmove=headmove+(cpos-0);
- cpos=0;
- for(i=index+1; i<n; i++)
- {
- headmove=headmove+abs(cpos-req[i]);
- cpos=req[i];
- }
- }
- else if(dir==1)
- {
- for(i=index+1; i<n; i++)
- {
- headmove=headmove+abs(cpos-req[i]);
- cpos=req[i];
- }
- headmove=headmove+(cpos-(size-1));
- cpos=(size-1);
- for(i=index-1; i>=0; i--)
- {
- headmove=headmove+abs(cpos-req[i]);
- cpos=req[i];
- }
- }
- printf("\nNumber of head movements=%d",headmove);
- }
ii. Write an OS program to implement C-SCAN algorithm Disk Scheduling algorithm.
CODE:
๐
Comments
Post a Comment