document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*--------------------------------------------------------------------------------
  2.  Title: Write a program to implement Shortest Job First (Preemptive) scheduling algorithm.
  3. ---------------------------------------------------------------------------------*/
  4.  
  5. #include<stdio.h>
  6. //#include<string.h>
  7.  
  8. int n;
  9.  
  10. struct process
  11. {
  12.     int arr[30];
  13.     char prc[30];
  14.     int at,bt,pt;
  15.     int flags,l,id;
  16.     }p[30],tem;
  17.  
  18. struct tprocess
  19. {
  20.     char prc[30];
  21.     int at,bt;
  22.     }tp1[30];
  23.  
  24. int main()
  25. {
  26.     int temp1=0;
  27.     int i,t,j;
  28.     int temp2=0,temp3=0,time=0;
  29.     int tt,var,sel,same,status=0;
  30.     float avgt=0,avgw=0;
  31. //  struct process tem;
  32.  
  33.     printf("\\n SJF (Preempitve) \\n");
  34.     printf("\\n Enter Number of Process : ");
  35.     scanf("%d",&n);
  36.     tt=0;
  37.  
  38.     for(i=0;i<n;i++)
  39.     {
  40.         p[i].id=i+1;
  41.  
  42.         printf("\\n For Process %d : ",i+1);
  43.         printf("\\n Enter Burst Time : ");
  44.         scanf("%d",&p[i].bt);
  45.  
  46.         printf("\\n Enter Arrival Time : ");
  47.         scanf("%d",&p[i].at);
  48.  
  49.         p[i].flags=0;
  50.         p[i].l=0;
  51.         tt=tt+p[i].bt;
  52.         }
  53.  
  54.     for(i=1;i<n;i++)
  55.     {
  56.         for(j=0;j<n-i;j++)
  57.         {
  58.             if(p[j].at>p[j+1].at)
  59.             {
  60.                 tem=p[j];
  61.                 p[j]=p[j+1];
  62.                 p[j+1]=tem;
  63.                 }
  64.             }
  65.         }
  66.  
  67.     var=p[0].at;
  68.     tt=tt+var;
  69.     time=var;
  70.    
  71.     printf("\\n\\n Gaunt Chart : \\n\\n");
  72.     while(time<tt)
  73.     {
  74.         for(i=0;i<n;i++)
  75.         {
  76.             if(p[i].bt==0)
  77.             {
  78.                 p[i].flags=-1;
  79.                 }
  80.             }
  81.  
  82.         for(i=0;i<n;i++)
  83.         {
  84.             if(p[i].at==time&&p[i].flags!=-1)
  85.             {
  86.                 p[i].flags=1;
  87.                 }
  88.             }
  89.  
  90.         t=tt+1;
  91.  
  92.         for(i=0;i<n;i++)
  93.         {
  94.             if(p[i].flags==1)
  95.             {
  96.                 if(t>p[i].bt)
  97.                 {
  98.                     t=p[i].bt;
  99.                     sel=i;
  100.                     }
  101.                 }
  102.             }
  103.  
  104.         if(p[same].id!=p[sel].id&&status==1)
  105.         {
  106.             p[sel].arr[p[sel].l]=var;
  107.             p[sel].l++;
  108.             p[same].arr[p[same].l]=var;
  109.             p[same].l++;
  110.             //printf("--%d %d--",p[same].id,p[same].l);
  111.             }
  112.  
  113.         if(status==0)
  114.         {
  115.             for(i=0;i<n;i++)
  116.             {
  117.                 p[i].arr[p[i].l]=p[i].at;
  118.                 p[i].l++;
  119.                 }
  120.  
  121.             p[sel].arr[p[sel].l]=var;
  122.             p[sel].l++;
  123.             }
  124.  
  125.         var=var+1;
  126.         p[sel].bt=p[sel].bt-1;
  127.         printf("..p%d..%d..",p[sel].id,var);
  128.         same=sel;
  129.         status=1;
  130.  
  131.         time++;
  132.         }
  133.  
  134.     p[sel].arr[p[sel].l]=var;
  135.     p[sel].l++;
  136.  
  137.     printf("\\n\\n Array of Process : \\n\\n");
  138.     printf("\\n");
  139.  
  140.     for(i=0;i<n;i++)
  141.     {
  142. //      printf("\\n Process %d :\\n",p[i].id);
  143.         for(j=0;j<p[i].l;j++)
  144.         {
  145.             printf("  --p%d %d-- ",p[i].id,p[i].arr[j]);
  146.             }
  147.  
  148.         printf("\\n");
  149.         }
  150.  
  151.     for(i=0;i<n;i++)
  152.     {
  153.         for(j=p[i].l-2;j>=0;j=j-2)
  154.         {
  155.             temp1=temp1+((p[i].arr[j])-(p[i].arr[j-1]));
  156.             }
  157.  
  158.         temp2=temp2+temp1;
  159.         temp1=0;
  160.         temp3=temp3+(p[i].arr[p[i].l-1]-p[i].arr[0]);
  161.         }
  162.  
  163.     avgw=(float)temp2/n;
  164.     avgt=(float)temp3/n;
  165.  
  166.     printf("\\n Throughput : %f  ",(n/(float)time));
  167.     printf("\\n\\n Wating Time : %d ",temp2);
  168.     printf("\\n Average Waiting Time : %f",avgw);
  169.  
  170.     printf("\\n\\n Turn around Time : %d",temp3);
  171.     printf("\\n Average Turn around Time : %f",avgt);
  172.    
  173.     return 0;
  174.     }
  175.  
  176. //end of the program
  177.  
  178. /*
  179. //--------------------
  180. //  OUTPUT
  181. //--------------------
  182.  
  183. gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a6_SJF_P.c
  184.  
  185. gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
  186.  
  187.  SJF (Preempitve)
  188.  
  189.  Enter Number of Process : 4
  190.  
  191.  For Process 1 :
  192.  Enter Burst Time : 8
  193.  
  194.  Enter Arrival Time : 0
  195.  
  196.  For Process 2 :
  197.  Enter Burst Time : 5
  198.  
  199.  Enter Arrival Time : 0
  200.  
  201.  For Process 3 :
  202.  Enter Burst Time : 2
  203.  
  204.  Enter Arrival Time : 1
  205.  
  206.  For Process 4 :
  207.  Enter Burst Time : 7
  208.  
  209.  Enter Arrival Time : 2
  210.  
  211.  
  212.  Gaunt Chart :
  213.  
  214.  
  215. ..p2..1....p3..2....p3..3....p2..4....p2..5....p2..6....p2..7....p4..8....p4..9.
  216. ...p4..10....p4..11....p4..12....p4..13....p4..14....p1..15....p1..16....p1..17.
  217. ...p1..18....p1..19....p1..20....p1..21....p1..22..
  218.  
  219.  Array of Process :
  220.  
  221.  
  222.   --p1 0--   --p1 14--   --p1 22--
  223.   --p2 0--   --p2 0--   --p2 1--   --p2 3--   --p2 7--
  224.   --p3 1--   --p3 1--   --p3 3--
  225.   --p4 2--   --p4 7--   --p4 14--
  226.  
  227.  Process 1 :
  228.  
  229.  Process 2 :
  230.  
  231.  Process 3 :
  232.  
  233.  Process 4 :
  234.  
  235.  Thorughput : 0.181818
  236.  Wating Time : 21
  237.  Average Waiting Time : 5.250000
  238.  
  239.  Turn around Time : 43
  240.  Average Turn around Time : 10.750000
  241.  
  242. // EXIT
  243. */
');