SHARE
TWEET

Untitled

a guest Apr 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* input
  2. 5
  3. 63
  4. 23 34 98 12 123
  5. */
  6. #include<iostream>
  7. #include<queue>
  8. #include<algorithm>
  9.  
  10. #define rep(i,a,b) for(int i=a;i<b;i++)
  11. #define NC 200
  12. #define pb push_back
  13. #define mp make_pair
  14.  
  15. using namespace std;
  16.  
  17. double FCFS(int hp, int* req, int n){
  18.     int time=0, pos=hp;
  19.     rep(i,0,n){
  20.         time += abs(req[i]-pos);
  21.         pos = req[i];
  22.         cout<<"\t"<<pos<<" seeked\n";
  23.     }
  24.  
  25.     double Total_time = time ;
  26.     return Total_time;
  27. }
  28.  
  29. double SSTF(int hp, int* req, int n){
  30.     int time=0, pos=hp;
  31.  
  32.     vector<int> request;
  33.     rep(i,0,n) request.pb(req[i]);
  34.  
  35.     while(request.size()){
  36.  
  37.         int len = request.size();
  38.         int minimum=1000,min_index;
  39.         rep(i,0,len){
  40.             if (minimum>abs(pos-request[i])){
  41.                 minimum = abs(pos-request[i]);
  42.                 min_index = i;
  43.             }
  44.         }
  45.  
  46.         pair<int,int> x = mp(minimum,min_index);
  47.  
  48.         time += x.first;
  49.         pos = request[x.second];
  50.         request.erase(request.begin()+x.second);
  51.  
  52.         cout<<"\t"<<pos<<" seeked\n";
  53.    
  54.     }
  55.  
  56.     double Total_time = time ;
  57.     return Total_time;
  58. }
  59.  
  60. double SCAN(int hp, int* req, int n){
  61.     int time=0, pos=hp;
  62.  
  63.     vector<int> request;
  64.     rep(i,0,n) request.pb(req[i]);
  65.  
  66.     request.pb(hp);
  67.  
  68.     int end=NC, start=0, cur=0, hp_index;
  69.  
  70.     sort(request.begin(),request.end());
  71.     while(request[cur] != hp) cur++;
  72.  
  73.     hp_index = cur;
  74.     cur++;
  75.     while(cur<n+1){
  76.         time += request[cur] - pos;
  77.         pos = request[cur];
  78.         cout<<"\t"<<pos<<" seeked\n";
  79.         cur++;
  80.     }
  81.  
  82.     // Seek to end
  83.     time += end - pos;
  84.     pos = end;
  85.  
  86.     cur = hp_index - 1;
  87.  
  88.     // Seek back to start
  89.     while(cur>=0){
  90.         time += pos - request[cur];
  91.         pos = request[cur];
  92.         cout<<"\t"<<pos<<" seeked\n";
  93.         cur--;
  94.     }
  95.  
  96.     double Total_time = time ;
  97.     return Total_time;
  98. }
  99.  
  100. double C_SCAN(int hp, int* req, int n){
  101.     int time=0, pos=hp;
  102.  
  103.     vector<int> request;
  104.     rep(i,0,n) request.pb(req[i]);
  105.  
  106.     request.pb(hp);
  107.  
  108.     int end=NC, start=0, cur=0, hp_index;
  109.  
  110.     sort(request.begin(),request.end());
  111.     while(request[cur] != hp) cur++;
  112.  
  113.     hp_index = cur;
  114.     cur++;
  115.     while(cur<n+1){
  116.         time += request[cur] - pos;
  117.         pos = request[cur];
  118.         cout<<"\t"<<pos<<" seeked\n";
  119.         cur++;
  120.     }
  121.  
  122.     // Seek to end
  123.     time += end - pos;
  124.     pos = end;
  125.  
  126.     // Seek back to start in no time
  127.     pos = 0;
  128.  
  129.     cur = 0;
  130.  
  131.     while(cur<hp_index){
  132.         time -= pos - request[cur];
  133.         pos = request[cur];
  134.         cout<<"\t"<<pos<<" seeked\n";
  135.         cur++;
  136.     }
  137.  
  138.     double Total_time = time ;
  139.     return Total_time;
  140. }
  141.  
  142. double C_LOOK(int hp, int* req, int n){
  143.     int time=0, pos=hp;
  144.  
  145.     vector<int> request;
  146.     rep(i,0,n) request.pb(req[i]);
  147.  
  148.     request.pb(hp);
  149.  
  150.     int cur=0, hp_index;
  151.  
  152.     sort(request.begin(),request.end());
  153.     while(request[cur] != hp) cur++;
  154.  
  155.     hp_index = cur;
  156.     cur++;
  157.     while(cur<n+1){
  158.         time += request[cur] - pos;
  159.         pos = request[cur];
  160.         cout<<"\t"<<pos<<" seeked\n";
  161.         cur++;
  162.     }
  163.  
  164.     // Seek to start
  165.     // we do not add the time required for head movement from last request to first request
  166.     pos = request[0];
  167.     cur = 0;
  168.  
  169.     while(cur<hp_index){
  170.         time += abs(pos - request[cur]);
  171.         pos = request[cur];
  172.         cout<<"\t"<<pos<<" seeked\n";
  173.         cur++;
  174.     }
  175.  
  176.     double Total_time = time ;
  177.     return Total_time;
  178. }
  179.  
  180. double LOOK(int hp, int* req, int n){
  181.     int time=0, pos=hp;
  182.  
  183.     vector<int> request;
  184.     rep(i,0,n) request.pb(req[i]);
  185.  
  186.     request.pb(hp);
  187.  
  188.     int cur=0, t_cur;
  189.  
  190.     sort(request.begin(),request.end());
  191.     while(request[cur] != hp) cur++;
  192.  
  193.     t_cur = cur;
  194.     cur++;
  195.     while(cur<n+1){
  196.         time += request[cur] - pos;
  197.         pos = request[cur];
  198.         cout<<"\t"<<pos<<" seeked\n";
  199.         cur++;
  200.     }
  201.  
  202.     cur = t_cur - 1;
  203.  
  204.     while(cur>=0){
  205.         time += pos - request[cur];
  206.         pos = request[cur];
  207.         cout<<"\t"<<pos<<" seeked\n";
  208.         cur--;
  209.     }
  210.  
  211.     double Total_time = time;
  212.     return Total_time;
  213. }
  214.  
  215. int main(){
  216.     int n,hp;
  217.     cout<<"DISK SCHEDULING\n";
  218.     cout<<"No. of requests : ";
  219.     cin>>n;
  220.     cout<<"Initial Position of Head (Total Cylinder : "<<NC<<" ) : ";
  221.     cin>>hp;
  222.     int err=0;
  223.     cout<<"Provide Positions to Visit : max is 200\n";
  224.     int req[n];
  225.     rep(i,0,n) cin>>req[i];
  226.  
  227.     // Comparing the disk scheduling algorithms:
  228.     cout<<"============================================\n";
  229.     cout<<"--- FCFS ---\n";
  230.     cout<<"Total seek time : \n"<<FCFS(hp, req, n)<<"\n\n";
  231.     cout<<"============================================\n";
  232.     cout<<"--- SSTF ---\n";
  233.     cout<<"Total seek time : \n"<<SSTF(hp, req, n)<<"\n\n";
  234.     cout<<"============================================\n";
  235.     cout<<"--- SCAN ---\n";
  236.     cout<<"Total seek time : \n"<<SCAN(hp, req, n)<<"\n\n";
  237.     cout<<"============================================\n";
  238.     cout<<"--- C_SCAN ---\n";
  239.     cout<<"Total seek time : \n"<<C_SCAN(hp, req, n)<<"\n\n";
  240.     cout<<"============================================\n";
  241.     cout<<"--- LOOK ---\n";
  242.     cout<<"Total seek time : \n"<<LOOK(hp, req, n)<<"\n\n";
  243.     cout<<"============================================\n";
  244.     cout<<"--- C_LOOK ---\n";
  245.     cout<<"Total seek time : \n"<<C_LOOK(hp, req, n)<<"\n\n";
  246.     cout<<"============================================\n";
  247.  
  248.     return 0;
  249. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top