Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- int q[10], noq, cp, i, j, temp;
- void fcfs();
- void scan();
- void cscan();
- void main()
- {
- int n, x;
- //printf("Enter the maximum number of cylinders : ");
- //scanf("%d", &n);
- printf("Enter the number of queues : ");
- scanf("%d", &noq);
- printf("Enter the position of queues :-\n");
- for(i=0;i<noq;i++)
- {
- scanf("%d", &q[i]);
- }
- printf("Enter the current position of the head : ");
- scanf("%d", &cp);
- do
- {
- printf("Choose disc scheduling type :-\n");
- printf("1. FCFS\n");
- //printf("2. SSTF\n");
- printf("2. SCAN\n");
- printf("3. CSCAN\n");
- printf("4. Exit\n");
- scanf("%d", &x);
- switch(x)
- {
- case 1 : fcfs();
- break;
- //case 2 : sstf();
- // break;
- case 2 : scan();
- break;
- case 3 : cscan();
- break;
- /*case 4 : look();
- break;
- case 5 : clook();
- break;*/
- case 4 : exit(0);
- default : printf("Enter a valid option : ");
- break;
- }
- }while(1);
- }
- void fcfs()
- {
- int i, t=0, cpp;
- cpp=cp;
- //printf("%d-->",cp);
- for(i=0;i<noq;i++)
- {
- t += abs(cpp-q[i]);
- printf("%d-->",cpp);
- cpp = q[i];
- }
- printf("%d\n",cpp);
- printf("Seek time : %d\n", t);
- }
- /*void sstf()
- {
- int min=999, temp
- for(i=0;i<noq;i++)
- {
- if(visit[i]==0)
- {
- if(min>abs(st-q[i])
- {
- min = abs(st-q[i]);
- p = i;
- }
- }
- /*for(i=0;i<noq;i++)
- {
- for(j=i+1;j<noq;j++)
- {
- if(q[i]>q[j])
- {
- temp=q[i];
- q[i] = q[j];
- q[j] = temp;
- }
- }
- }
- }*/
- void scan()
- {
- //printf("test\n");
- int st, e, t=0, max, dloc;
- /*for(i=0;i<noq;i++)
- {
- if(q[i]>q[i+1])
- {
- temp=q[i];
- q[i] = q[i+1];
- q[i+1] = temp;
- }
- }*/
- //printf("test\n");
- printf("Enter the disc movement range -\n");
- printf("Starting : ");
- scanf("%d",&st);
- printf("To : ");
- scanf("%d",&e);
- for(i=0;i<noq;i++);
- q[noq]=cp;
- noq+=1;
- for(i=0;i<noq;i++)
- {
- for(j=i;j<noq;j++)
- {
- if(q[i]>q[j])
- {
- temp=q[i];
- q[i] = q[j];
- q[j] = temp;
- }
- }
- }
- max = q[noq-1];
- //printf("test\n");
- for(i=0;i<noq;i++) // to find loc of disc in array
- {
- if(cp==q[i])
- {
- dloc=i;
- break;
- }
- }
- //printf("test\n");
- for(i=dloc;i>=0;i--)
- {
- //printf("test1\n");
- printf("%d-->",q[i]);
- }
- //printf("test\n");
- //if(q[i]!=st)
- printf("0-->");
- //printf("test\n");
- for(i=dloc+1;i<noq-1;i++)
- {
- printf("%d-->",q[i]);
- }
- //printf("test\n");
- printf("%d\n",q[i]);
- t=q[dloc]+max;
- printf("\nSeek time = %d\n",t);
- /*do
- {
- printf("%d-->",cp);
- for(i=cp;i!=e;i++)
- {
- for(j=0;j<noq;j++)
- {
- if(i==q[j])
- {
- if(v[j]==0)
- {
- printf("%d-->",i);
- v[j]==1;
- t+=abs(cp-i);
- cp=i;
- }
- }
- }
- }
- if(e!=cp)
- printf("%d-->",e);
- t+=abs(cp-e);
- cp=e;
- for(i=0;i<noq;i++)
- if(v[i]!=1)
- f=0;
- if(f==0)
- {
- for(i=e;i!=st;i--)
- {
- for(j=0;j<noq;j++)
- {
- if(i==q[j])
- {
- if(v[j]==0)
- {
- printf("%d-->",i);
- v[j]==1;
- t+=abs(cp-i);
- cp=i;
- }
- }
- }
- }
- t+=abs(cp-i);
- if(st!=cp)
- printf("%d",st);
- t+=abs(cp-st);
- printf("\n");
- }
- printf("Seek time : %d\n", t);
- break;
- }while(1);*/
- //printf("%d\n",t);*/
- }
- void cscan()
- {
- int st, e, t=0, f=1, max, dloc;
- int c1=0,c2=0,q1[10],q2[10];
- printf("Enter the disc movement range -\n");
- printf("Starting : ");
- scanf("%d",&st);
- printf("To : ");
- scanf("%d",&e);
- for(i=0;i<noq;i++)
- {
- for(j=i;j<noq;j++)
- {
- if(q[i]>q[j])
- {
- temp=q[i];
- q[i] = q[j];
- q[j] = temp;
- }
- }
- }
- for(i=0;i<noq;i++)
- {
- if(q[i]>=cp)
- {
- q1[c1++] = q[i];
- }
- else
- {
- q2[c2++] = q[i];
- }
- }
- if(cp!=q1[0])
- printf("%d-->",cp);
- for(i=0;i<c1;i++)
- {
- printf("%d-->",q1[i]);
- }
- if(e!=cp)
- printf("%d-->",e);
- t += (e-cp);
- if(st!=q2[0])
- printf("%d-->",st);
- for(i=0;i<c2-1;i++)
- {
- printf("%d-->",q2[i]);
- }
- t += (e-st)+q2[i];
- printf("%d\n",q2[i]);
- printf("Seek time : %d\n", t);
- /*for(i=0;i<noq;i++)
- {
- for(j=i;j<noq;j++)
- {
- if(q[i]>q[j])
- {
- temp=q[i];
- q[i] = q[j];
- q[j] = temp;
- }
- }
- }
- max = q[noq];
- for(i=0;i<noq;i++)
- {
- if(cp==q[i])
- {
- dloc=i;
- break;
- }
- }
- for(i=dloc;i<noq;i++)
- {
- printf("%d-->",q[i]);
- }
- if(q[i]!=e)
- printf("%d-->",e);
- printf("%d-->",st);
- for(i=0;i<dloc;i++)
- {
- printf("%d-->",q[i]);
- }
- printf("%d\n",q[i]);
- t=cp+max;
- printf("\nSeek time = %d\n",t);*/
- }
Add Comment
Please, Sign In to add comment