csenotes12

question 17

Apr 9th, 2019
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. /*Design a scheduling program to implem
  2. ents a Queue with two levels:
  3. Level 1 : Fixed priority preemptive Scheduling
  4. Level 2 : Round Robin Scheduling
  5. For  a  Fixed  priority  preemptive  Scheduling  (Queue  1),  the  Priority  0  is  highest  priority.  If  one
  6. process P1 is scheduled  and running ,  another pro
  7. cess P2 with higher priority  comes. The New
  8. process (high priority) process P2 preempts currently running process P1 and process P1 will go
  9. to  second  level  queue.  Time  for  which  process  will  strictly  execute  must  be  considered  in  the
  10. multiples of 2..
  11. All  t
  12. he  processes  in  second  level  queue  will  complete  their  execution  according  to  round  robin
  13. scheduling.
  14. Consider: 1. Queue 2 will be processed after Queue 1 becomes empty.
  15. 2. Priority of Queue 2 has lower priority than in Queue 1.*/
  16. #include<stdio.h>
  17. #include<string.h>
  18. #include<conio.h>
  19. void main()
  20. {
  21.     char p[10][5],temp[5];
  22.     int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
  23.     float avgwt;
  24.     printf("enter no of processes:");
  25.     scanf("%d",&n);
  26.     for(i=0;i<n;i++)
  27.     {
  28.         printf("enter process%d name:",i+1);
  29.         scanf("%s",&p[i]);
  30.         printf("enter process time:");
  31.         scanf("%d",&pt[i]);
  32.         printf("enter priority:");
  33.         scanf("%d",&pr[i]);
  34.     }
  35.     for(i=0;i<n-1;i++)
  36.     {
  37.         for(j=i+1;j<n;j++)
  38.         {
  39.             if(pr[i]>pr[j])
  40.             {
  41.                 temp1=pr[i];
  42.                 pr[i]=pr[j];
  43.                 pr[j]=temp1;
  44.                 temp1=pt[i];
  45.                 pt[i]=pt[j];
  46.                 pt[j]=temp1;
  47.                 strcpy(temp,p[i]);
  48.                 strcpy(p[i],p[j]);
  49.                 strcpy(p[j],temp);
  50.             }
  51.         }
  52.     }
  53.     wt[0]=0;
  54.     for(i=1;i<n;i++)
  55.     {
  56.         wt[i]=wt[i-1]+wt[i-1];
  57.         totwt=totwt+wt[i];
  58.     }
  59.     avgwt=(float)totwt/n;
  60.     printf("p_name\t p_time\t priority\t w_time\n");
  61.     for(i=0;i<n;i++)
  62.     {
  63.        printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
  64.     }
  65.     printf("total waiting time=%d\n avg waiting time=%f",totwt,avgwt);
  66.    
  67.     int ts,pid[10],need[10],wt1[10],tat[10],i1,j1,n2,n1;
  68.     int bt[10],flag[10],ttat=0,twt=0;
  69.     float awt,atat;
  70.     printf("\nEnter the number of Processors \n");
  71.     scanf("%d",&n);
  72.     n1=n;
  73.     printf("\n Enter the Timeslice \n");
  74.     scanf("%d",&ts);
  75.     for(i=1;i<=n;i++)
  76.     {
  77.         printf("\n Enter the process ID %d",i);
  78.         scanf("%d",&pid[i]);
  79.         printf("\n Enter the Burst Time for the process");
  80.         scanf("%d",&bt[i]);
  81.         need[i]=bt[i];
  82.     }
  83.     for(i=1;i<=n;i++)
  84.     {
  85.         flag[i]=1;
  86.         wt[i]=0;
  87.     }
  88.     while(n!=0)
  89.     {
  90.         for(i=1;i<=n;i++)
  91.         {
  92.             if(need[i]>=ts)
  93.             {
  94.                 for(j=1;j<=n;j++)
  95.                 {
  96.                     if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
  97.                     wt[j]+=ts;
  98.                 }
  99.                 need[i]-=ts;
  100.                 if(need[i]==0)
  101.                 {
  102.                     flag[i]=0;
  103.                     n--;
  104.                 }
  105.             }
  106.             else
  107.             {
  108.                 for(j=1;j<=n;j++)
  109.                 {
  110.                     if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
  111.                     wt[j]+=need[i];
  112.                 }
  113.                 need[i]=0;
  114.                 n--;
  115.                 flag[i]=0;
  116.             }
  117.         }
  118.     }
  119.     for(i=1;i<=n1;i++)
  120.     {
  121.         tat[i]=wt[i]+bt[i];
  122.         twt=twt+wt[i];
  123.         ttat=ttat+tat[i];
  124.     }
  125.     awt=(float)twt/n1;
  126.     atat=(float)ttat/n1;
  127.     printf("\n\n Process \t Process ID  \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
  128.     for(i=1;i<=n1;i++)
  129.     {
  130.         printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
  131.     }
  132.     printf("\n The average Waiting Time=4.2f",awt);
  133.     printf("\n The average Turn around Time=4.2f",atat);
  134. }
Add Comment
Please, Sign In to add comment