document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*--------------------------------------------------------------------------------
  2.  Title: Write a program to implement Shortest Job First (Non-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];
  17.  
  18. struct tprocess
  19. {
  20.     char prc[30];
  21.     int at,bt;
  22.     }tp1[30];
  23.  
  24. void main()
  25. {
  26.     char ta[10];
  27.  
  28.     int wt[10],tat[10];
  29.     int temp,temp1=0,flg=0,flag=0,cnt;
  30.     int i,t,j;
  31.     float awt,sum=0,x,awt1;
  32.  
  33.     printf("\\n SJF (Non-Preempitve) \\n");
  34.     printf("\\n Enter Number of Process : ");
  35.     scanf("%d",&n);
  36.  
  37.     flag=n;
  38.  
  39.     for(i=0; i<n; i++)
  40.     {
  41.         printf("\\n Enter Process Name : ");
  42.         scanf("%s",p[i].prc);
  43.  
  44.         printf("\\n Enter Arrival Time : ");
  45.         scanf("%d",&p[i].at);
  46.  
  47.         printf("\\n Enter Burst Time : ");
  48.         scanf("%d",&p[i].bt);
  49.  
  50.         }
  51.                 // display data in table format
  52.     printf("\\n Process Name \\t Arrival Time \\t Burst Time \\n");
  53.     for(i=0; i<n; i++)
  54.     {
  55.         printf("\\n %s \\t\\t %d \\t\\t %d ",p[i].prc,p[i].at,p[i].bt);
  56.         }
  57.  
  58.                 // sorting logic based on BT
  59.     for(i=0; i<n; i++)
  60.     {
  61.         for(j=i; j<n; j++)
  62.         {
  63.             if(p[i].bt>p[j].bt)
  64.             {
  65.                 temp = p[i].bt;
  66.                 t = p[i].at;
  67.                 strcpy(ta,p[i].prc);
  68.  
  69.                 p[i].bt = p[j].bt;
  70.                 p[i].at = p[j].at;
  71.                 strcpy(p[i].prc,p[j].prc);
  72.  
  73.                 p[j].bt = temp;
  74.                 p[j].at = t;
  75.                 strcpy(p[j].prc,ta);
  76.                 }
  77.             }
  78.         }
  79.  
  80.     // print the sorted table
  81.     printf("\\n\\n\\t\\t Sorted Sructure \\n");
  82.     printf("\\n Process Name \\t Arrival Time \\t Burst Time \\n");
  83.     for(i=0; i<n; i++)
  84.     {
  85.         printf("\\n %s \\t\\t %d \\t\\t %d ",p[i].prc,p[i].at,p[i].bt);
  86.         }
  87.  
  88.     printf("\\n\\n Gaunt Chart : ");
  89.     // set WT and TAT as \'zero\' , its compulsory
  90.     for(i=0; i<n; i++)
  91.     {
  92.         wt[i] = 0;
  93.         tat[i]=0;
  94.         }
  95.     t=0;
  96.     cnt=0;
  97.  
  98.     printf("0");
  99.  
  100. again:      if(p[flg].at<=temp1)
  101.         {
  102.             wt[cnt] = temp1 - p[flg].at;
  103.             cnt++;
  104.             temp1 = temp1 + p[flg].bt;
  105.             tat[t] = temp1 - p[flg].at;
  106.             t++;
  107.  
  108.             printf(" %s-->%d ",p[flg].prc,temp1);
  109.             goto delfrmstruct;
  110.             }
  111.         else
  112.         {
  113.             flg++;
  114.             goto again;
  115.             }
  116.  
  117. delfrmstruct:   for(i=flg; i<flag; i++)
  118.         {
  119.             p[i] = p[i+1];
  120.             }
  121.         flag--;
  122.         if(flag==0)
  123.         {
  124.             awt1=0;
  125.             x=temp1;
  126.             awt1= n/x; // calculate throughput : dividing total no. of process by last execution time
  127.             goto ext;  // goto exit and then calculation of Avg WT & Avg. TAT and throughput
  128.             }
  129.         else
  130.         {
  131.             flg=0;
  132.             goto again;   // else goto label \'again\' for next calculation
  133.             }
  134.  
  135. ext:        sum = 0;
  136.         for(i=0; i<n; i++)
  137.         {
  138.             sum = sum + wt[i];
  139.             }
  140.  
  141.         awt = 0 ;
  142.         awt = sum/n;
  143.         printf("\\n\\n Wating Time : %f ",sum);
  144.         printf("\\n Average Waiting Time : %f",awt);
  145.  
  146.         sum = awt = 0 ;
  147.         for(i=0; i<n; i++)
  148.         {
  149.             sum=sum + tat[i];
  150.             }
  151.  
  152.         awt=sum/n;
  153.         printf("\\n\\n Turn around Time : %f ",sum);
  154.         printf("\\n Average Turn around Time : %f",awt);
  155.  
  156.         printf("\\n\\n Throughput : %f ",awt1);
  157.     }
  158.    
  159. //end of the program
  160.  
  161. /*
  162. //------------------
  163. //  OUTPUT
  164. //------------------
  165.  
  166. gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a6_SJF_NonP.c
  167.  
  168. gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
  169.  
  170.  SJF (Non-Preempitve)
  171.  
  172.  Enter Number of Process : 4
  173.  
  174.  Enter Process Name : p1
  175.  
  176.  Enter Arrival Time : 0
  177.  
  178.  Enter Burst Time : 8
  179.  
  180.  Enter Process Name : p2
  181.  
  182.  Enter Arrival Time : 0
  183.  
  184.  Enter Burst Time : 5
  185.  
  186.  Enter Process Name : p3
  187.  
  188.  Enter Arrival Time : 1
  189.  
  190.  Enter Burst Time : 2
  191.  
  192.  Enter Process Name : p4
  193.  
  194.  Enter Arrival Time : 2
  195.  
  196.  Enter Burst Time : 7
  197.  
  198.  Process Name    Arrival Time    Burst Time
  199.  
  200.  p1              0               8
  201.  p2              0               5
  202.  p3              1               2
  203.  p4              2               7
  204.  
  205.                  Sorted Structure
  206.  
  207.  Process Name    Arrival Time    Burst Time
  208.  
  209.  p3              1               2
  210.  p2              0               5
  211.  p4              2               7
  212.  p1              0               8
  213.  
  214.  Gaunt Chart : 0 p2-->5  p3-->7  p4-->14  p1-->22
  215.  
  216.  Wating Time : 23.000000
  217.  Average Waiting Time : 5.750000
  218.  
  219.  Turn around Time : 45.000000
  220.  Average Turn around Time : 11.250000
  221.  
  222.  Throughput : 0.181818
  223.  
  224. // EXIT
  225.  
  226. */
');