document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*--------------------------------------------------------------------------------
  2.  Title: Write a program to implement Priority (Non-Preemptive) [With arrival time] scheduling algorithm.
  3. ---------------------------------------------------------------------------------*/
  4.  
  5. #include<stdio.h>
  6.  
  7. int n;
  8.  
  9. struct proc
  10. {
  11.     int atime,btime,flag,pid,stime,ftime,wt,tt,prio;
  12.     char name[20];
  13.     }p[50];
  14.    
  15. void main()
  16. {
  17.     struct proc temp;
  18.     int i,j,time=0,t,tt,var,sel,value,temp1=0,temp2=0,high,sel1;
  19.     float avgt=0,avgw=0;
  20.     int arr[50];
  21.    
  22.     printf("\\n\\t\\t No of Process: ");
  23.     scanf("%d",&n);
  24.     tt=0;
  25.     for(i=0;i<n;i++)
  26.     {
  27.         p[i].pid=i+1;
  28.         printf("\\n\\t\\t\\t enter process name :");
  29.        
  30.         scanf("%s",&p[i].name);
  31.         printf("\\n\\t\\t\\t enter process arrival Time :");
  32.         scanf("%d",&p[i].atime);
  33.    
  34.         printf("\\n\\t\\t\\t enter process Burst Time :");
  35.         scanf("%d",&p[i].btime);
  36.    
  37.         printf("\\n\\t\\t\\t enter process priority :");
  38.         scanf("%d",&p[i].prio);
  39.  
  40.         p[i].flag=0;
  41.         tt=tt+p[i].btime;
  42.         }
  43.  
  44.      for(i=1;i<n;i++)
  45.     {
  46.         for(j=0;j<n;j++)
  47.         {
  48.             if(p[j].atime>p[i].atime)
  49.             {
  50.                 temp=p[i];
  51.                 p[i]=p[j];
  52.                 p[j]=temp;
  53.                 }
  54.             }
  55.         }
  56.  
  57.     var=p[0].atime;
  58.     tt=tt+var;
  59.     time=var;
  60.     value=var;
  61.    
  62.     while(time<tt)
  63.     {
  64.         for(i=0;i<n;i++)
  65.         {
  66.             if(p[i].btime==0)
  67.             {
  68.                 p[i].flag=-1;
  69.                 }
  70.             }
  71.  
  72.         for(i=0;i<n;i++)
  73.         {
  74.             if(p[i].atime<=time&&p[i].flag!=-1)
  75.             {
  76.                 p[i].flag=1;
  77.                 }
  78.             }
  79.  
  80.         t=tt+1;
  81.         high=-1;
  82.    
  83.         for(i=0;i<n;i++)
  84.         {
  85.             if(p[i].flag==1)
  86.             {
  87.                  if(p[i].prio>high)
  88.                  {
  89.                     high=p[i].prio;
  90.                     sel1=i;
  91.                     }
  92.                 else if(p[i].prio==high)
  93.                 {
  94.                     if(p[i].btime<p[sel1].btime)
  95.                     {
  96.                         sel1=i;
  97.                             }
  98.                     }
  99.                 }
  100.             }
  101.  
  102.         printf("--%d--%s",value,p[sel1].name);
  103.         p[sel1].stime=value;
  104.         value=value+p[sel1].btime;
  105.         p[sel1].ftime=value;
  106.         p[sel1].btime=0;
  107.         time=value;
  108.         }
  109.    
  110.     printf("--%d",value);
  111.     // printf("\\n\\t p id \\t stime \\t ftime");
  112.     for(i=0;i<n;i++)
  113.     {
  114.         p[i].wt=p[i].stime-p[i].atime;
  115.         temp1=temp1+p[i].wt;
  116.         p[i].tt=p[i].ftime-p[i].atime;
  117.         temp2=temp2+p[i].tt;
  118.         }
  119.  
  120.     printf("\\n proc\\tstart\\tfinish\\twait\\t tt");
  121.     for(i=0;i<n;i++)
  122.     {
  123.         printf("\\n\\t p%s\\t%d\\t%d\\t%d\\t%d\\n",p[i].name,p[i].stime,p[i].ftime,p[i].wt,p[i].tt);
  124.         }
  125.  
  126.     avgw=(float)temp1/n;
  127.     avgt=(float)temp2/n;
  128.     printf("\\n\\t average wating time is=%f\\n\\t average turn around time=%f\\n",avgw,avgt );
  129.     printf("\\n\\t Throughput =%f",(n/(float)time));
  130.  
  131.     }
  132.  
  133. // END OF THE PROGRAM
  134.  
  135. /*
  136. //------------------
  137. //  OUTPUT
  138. //------------------
  139.  
  140. gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a6_PRIO_NonP_W_AT.c
  141.  
  142. gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
  143.  
  144.                  No of Process: 4
  145.  
  146.                          enter process name :P1
  147.  
  148.                          enter process arrival Time :0
  149.  
  150.                          enter process Burst Time :2
  151.  
  152.                          enter process priority :1
  153.  
  154.                          enter process name :P2
  155.  
  156.                          enter process arrival Time :0
  157.  
  158.                          enter process Burst Time :1
  159.  
  160.                          enter process priority :3
  161.  
  162.                          enter process name :P3
  163.  
  164.                          enter process arrival Time :0
  165.  
  166.                          enter process Burst Time :4
  167.  
  168.                          enter process priority :1
  169.  
  170.                          enter process name :P4
  171.  
  172.                          enter process arrival Time :0
  173.  
  174.                          enter process Burst Time :3
  175.  
  176.                          enter process priority :2
  177.  
  178.  
  179.  
  180. GANTT CHART
  181. -------------------------------------------------------
  182.  
  183. --0--P2--1--P4--4--P1--6--P3--10
  184.  
  185.  
  186. ----------------------------------------------------------------
  187.         proc   start   finish  wait     tt
  188. -----------------------------------------------------------------
  189.          pP1    4       6       4       6
  190.  
  191.          pP2    0       1       0       1
  192.  
  193.          pP3    6       10      6       10
  194.  
  195.          pP4    1       4       1       4
  196.  
  197. -----------------------------------------------------------------
  198.  
  199.          Average Wating time is=2.750000
  200.          average Turn around time=5.250000
  201.          Throughput =0.4
  202.  
  203. // EXIT
  204. */
');