Skip to main content

Assignment No:3

 Assignment No:3


Slot-I

 i. Write an OS program to implement FCFS Disk Scheduling algorithm. 

CODE:

๐Ÿ‘‡

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void main()
  4. {
  5. int req[20],cpos,i,n,j,index,dir,temp,headmove=0,size=200;
  6. printf("\nEnter number of requests");
  7. scanf("%d",&n);
  8. printf("\n Enter request ");
  9. for(i=0;i<n;i++)
  10. scanf("%d",&req[i]);
  11. printf("\nEnter curreent position");
  12. scanf("%d",&cpos);
  13. for(i=0;i<n;i++)
  14.   {
  15.    headmove=headmove+abs(cpos-req[i]);
  16.    cpos=req[i];
  17.    }
  18.    printf("\nNumber of head movements=%d",headmove);
  19. }


ii. Write an OS program to implement SSTF algorithm Disk Scheduling algorithm. 

CODE:

๐Ÿ‘‡

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int main()
  4. {
  5.  int req[20],cpos,i,n,headmove=0,index,count=0,min,d;
  6.  printf("Enter the number of requests\n");
  7.  scanf("%d",&n);
  8.  printf("Enter the sequence of requests\n");
  9.  for(i=0;i<n;i++)
  10.  scanf("%d",&req[i]);
  11.  printf("Enter initial head position\n");
  12.  scanf("%d",&cpos);
  13.  while(count!=n)
  14.  {
  15.  int min=1000,d,intex;
  16.  for(i=0;i<n;i++)
  17.    {
  18.      d=abs(req[i]-cpos);
  19.      if(min>d)
  20.       {
  21. min=d;
  22. index=i;
  23.    }
  24.   }
  25.   headmove=headmove+min;
  26.   cpos=req[index];
  27.     req[index]=1000;
  28.     count++;
  29.  }
  30.  
  31.  printf("Total head movements is %d",headmove);
  32.  return 0;
  33. }

Slot-II 

i. Write an OS program to implement SCAN Disk Scheduling algorithm. 

CODE:

๐Ÿ‘‡

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int main()
  4. {
  5.  int req[20],cpos,i,n,headmove=0,index,dir,j,temp,size=20;
  6.  printf("Enter the number of requests\n");
  7.  scanf("%d",&n);
  8.  printf("Enter the sequence of requests\n");
  9.  for(i=0;i<n;i++)
  10.  scanf("%d",&req[i]);
  11.  printf("Enter initial head position\n");
  12.  scanf("%d",&cpos);
  13.  printf("Enter head movement direction 0 for left and 1 for right");
  14.  scanf("%d",&dir);
  15.  req[n]=cpos; 
  16.  n=n+1;  
  17.  
  18.  //sort request array
  19.  
  20.  for(i=0; i<n; i++)
  21.  {
  22.    for(j=i+1; j<n; j++)
  23.     { 
  24.       if(req[i]>req[j])
  25.       {
  26.         temp=req[i];
  27.         req[i]=req[j];
  28.         req[j]=temp;
  29.        }
  30.     }
  31.  }
  32.  for(i=0; i<n; i++)
  33.  {
  34.   if(req[i]==cpos)
  35.   {
  36.     index=i;
  37.     break;
  38.   }
  39.  }
  40. if(dir==0)
  41. {
  42.  for(i=index-1; i>=0; i--)
  43.  {
  44.    headmove=headmove+abs(cpos-req[i]);
  45.    cpos=req[i];
  46.   }
  47.  headmove=headmove+(cpos-0);
  48.  cpos=0;
  49.  for(i=index+1; i<n; i++)
  50.  {
  51.    headmove=headmove+abs(cpos-req[i]);
  52.    cpos=req[i];
  53.   }
  54. }

  55. else if(dir==1)
  56. {
  57.   for(i=index+1; i<n; i++)
  58.    {
  59.     headmove=headmove+abs(cpos-req[i]);
  60.     cpos=req[i];
  61.   }
  62.  headmove=headmove+(cpos-(size-1));
  63.  cpos=(size-1);
  64.  for(i=index-1; i>=0; i--)
  65.  {
  66.    headmove=headmove+abs(cpos-req[i]);
  67.    cpos=req[i];
  68.    }
  69.   }
  70. printf("\nNumber of head movements=%d",headmove);
  71. }

ii. Write an OS program to implement C-SCAN algorithm Disk Scheduling algorithm.

CODE:

๐Ÿ‘‡


Comments