123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /*
- NAME : RUSHIKESH M FANSE
- ROLL NO : 16
- DIV : H
- */
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #define MAX 20
- using namespace std;
- int tracks,curr_head,h_pos_f,h_pos_s,method,track_traversed,avg_seek_len,seek_length,start;
- int a[MAX],b[MAX];
- void FIFO()
- {
- int i;
- int k=0;
- for(i=0;i<tracks;i++)
- {
- track_traversed+= (a[i]>curr_head)?(a[i]-curr_head):(curr_head-a[i]);
- curr_head =a[i];
- if(curr_head!=start)
- k++;
- }
- cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/k<<"\n";
- }
- void SSTF()
- {
- int i,j,k;
- for(j=0;start>=b[j];j++);
- i=j-1;
- if((start-b[i])<(b[j]-start))
- {
- for(int k=i;k>=0;k--)
- {
- track_traversed+=curr_head-b[k];
- curr_head=b[k];
-
- }
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
- }
- }
- else if((start-b[i])>(b[j]-start))
- {
-
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
- }
-
- for(int k=i;k>=0;k--)
- {
- track_traversed+=curr_head-b[k];
- curr_head=b[k];
- }
- }
- cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
- }
- void SCAN()
- {
- int sc,i,j;
- cout<<"enter the direction in which scanning is to be done\n";
- cout<<"1.left\n2.right\n";
- cin>>sc;
- for(j=0;start>=b[j];j++);
- i=j-1;
- if(sc==1)
- {
- for(int k=i;k>=0;k--)
- {
- track_traversed+=curr_head-b[k];
- curr_head=b[k];
-
- }
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
- }
- }
- else if(sc==2)
- {
-
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
- }
- for(int k=i;k>=0;k--)
- {
- track_traversed+=curr_head-b[k];
- curr_head=b[k];
- }
- }
- cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
-
- }
- void CSCAN()
- {
- int sc,i,j;
- cout<<"enter the direction in which scanning is to be done\n";
- cout<<"1.left\n2.right\n";
- cin>>sc;
- for(j=0;start>=b[j];j++);
- i=j-1;
- if(sc==1)
- {
- for(int k=0;k<=i;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
- }
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
-
- }
- }
- else if(sc==2)
- {
-
- for(int k=j;k<tracks;k++)
- {
- track_traversed+=b[k]-curr_head;
- curr_head=b[k];
-
- }
-
- for(int l=0;l<=i;l++)
- {
- track_traversed+= (b[l]>curr_head)?(b[l]-curr_head):(curr_head-b[l]);
- curr_head=b[l];
- }
- }
- cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
- }
- int main()
- {
- cout<<"Enter the no of tracks\n";
- cin>>tracks;
- cout<<"enter the tracks\n";
- for(int i=0;i<tracks;i++)
- {
- cin>>b[i];
- a[i]=b[i];
- }
- sort(b,b+tracks);
- cout<<"which is current head\n";
- cin>>curr_head;
- start=curr_head;
- for(int i=0;i<tracks;i++)
- if(b[i]==curr_head)
- h_pos_s=i;
- while(method<=4)
- {
- track_traversed=0;
- avg_seek_len=0;
- curr_head=start;
- switch(method)
- {
- case 1:
- //FIFO
- cout<<"FIFO\n";
- FIFO();
- break;
- case 2:
- //SSTF
- cout<<"SSTF\n";
- SSTF();
- break;
- case 3:
- //SCAN
- cout<<"SCAN\n";
- SCAN();
- break;
- case 4:
- //CSCAN
- cout<<"CSCAN\n";
- CSCAN();
- break;
- }
- method++;
- }
-
- }
- /*
- OUTPUT:
- rishi@rishi-PC:~$ ./a.out
- Enter the no of tracks
- 9
- enter the tracks
- 55 58 39 18 90 160 150 38 184
- which is current head
- 100
- FIFO
- track traversed : 498 average seek length : 55.3333
- SSTF
- track traversed : 248 average seek length : 27.5556
- SCAN
- enter the direction in which scanning is to be done
- 1.left
- 2.right
- 2
- track traversed : 250 average seek length : 27.7778
- CSCAN
- enter the direction in which scanning is to be done
- 1.left
- 2.right
- 2
- track traversed : 322 average seek length : 35.7778
- */
|