Advertisement
manthanthakker40

non-pre-emptive priority scheduling using sjf

Jul 29th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. //precedence(arrivaltime-priority-execution)(non -pre-emptive priority scheduling using sjf[shortest job first])
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. void main()
  6. {
  7.     int f=0,e=0,pri[100],c=0,d=0,n=0,pid[100],temp=0,at[100],et[100],ct[100],b=0,wt[100],tt[100],i=0,choice=1,ct1[100];
  8.     int temp1,temp2,counti=0,count=1;
  9.     float avgwt=0.0,avgtt=0.0;
  10.     clrscr();
  11.     while(choice==1)
  12.     {
  13.     pid[i]=count++;
  14.     printf("\nenter arrival time:-");
  15.     scanf("%d",&at[i]);
  16.     printf("\nenter execution time:-");
  17.     scanf("%d",&et[i]);
  18.         printf("\nenter priority:-");
  19.     scanf("%d",&pri[i]);
  20.  
  21.  
  22.  
  23.     i++;
  24.     counti++;
  25.     printf("more processes 1. yes 2. no ");
  26.     scanf("%d",&choice);
  27.     }
  28.  
  29.         printf("*********data entered******\n");
  30.         printf("pid\tAT\tpri\tET\n");
  31.     while(n!=i)
  32.     {
  33.         printf("%d\t",pid[n]);
  34.         printf("%d\t",at[n]);
  35.         printf("%d\t",pri[n]);
  36.         printf("%d\n",et[n]);
  37.  
  38.  
  39.         n++;
  40.     }
  41.      n=0;
  42.  
  43.     //sorting arrival time
  44.     for(f=0;f<=i-2;f++)
  45.     for(e=0;e<=i-2;e++)
  46.     {
  47.         if(at[e]>at[e+1])
  48.         {
  49.             temp=at[e];
  50.             at[e]=at[e+1];
  51.             at[e+1]=temp;
  52.                 temp=pri[e];
  53.                     pri[e]=pri[e+1];
  54.                     pri[e+1]=temp;
  55.                     temp=et[e];
  56.                     et[e]=et[e+1];
  57.                     et[e+1]=temp;
  58.  
  59.                     temp2=pid[e];
  60.                     pid[e]=pid[e+1];
  61.                     pid[e+1]=temp2;
  62.  
  63.         }
  64.         if(at[e]==at[e+1])
  65.         {
  66.             //sorting priority
  67.  
  68.  
  69.                 c=e;
  70.  
  71.                 if(pri[c]>pri[c+1])
  72.                 {
  73.                     temp=pri[c];
  74.                     pri[c]=pri[c+1];
  75.                     pri[c+1]=temp;
  76.                     temp=et[c];
  77.                     et[c]=et[c+1];
  78.                     et[c+1]=temp;
  79.                     temp1=at[c];
  80.                     at[c]=at[c+1];
  81.                     at[c+1]=temp1;
  82.                     temp2=pid[c];
  83.                     pid[c]=pid[c+1];
  84.                     pid[c+1]=temp2;
  85.  
  86.                 }
  87.                 if(pri[c]==pri[c+1])
  88.                 {
  89.  
  90.                 //sorting et
  91.  
  92.                     if(et[c]>et[c+1])
  93.                     {
  94.  
  95.                         temp=et[c];
  96.                         et[c]=et[c+1];
  97.                         et[c+1]=temp;
  98.                         temp1=at[c];
  99.                         at[c]=at[c+1];
  100.                         at[c+1]=temp1;
  101.                         temp2=pid[c];
  102.                         pid[c]=pid[c+1];
  103.                         pid[c+1]=temp2;
  104.                         temp2=pri[c];
  105.                         pri[b]=pri[b+1];
  106.                         pri[b+1]=temp2;
  107.  
  108.                     }
  109.  
  110.  
  111.  
  112.  
  113.                 }
  114.  
  115.                 else
  116.                 {
  117.  
  118.                 }
  119.  
  120.  
  121.  
  122.         }
  123. }
  124.            /*        while(cti!=i)
  125.             {
  126.                     if(cti==0)
  127.                     ct[cti]=et[0];
  128.                     else
  129.                     {
  130.                     ct[cti]=ct[cti-1]+ct[cti];
  131.                     cti++;
  132.                      }
  133.                      } */
  134.                      d=0;
  135.                      while(d<counti)
  136.                      {
  137.                         if(d==0)
  138.                         ct[d]=et[d];
  139.                         else
  140.                         ct[d]=ct[d-1]+et[d];
  141.  
  142.                         wt[d]=ct[d]-(et[d]+at[d]);
  143.                         tt[d]=ct[d]-at[d];
  144.                         d++;
  145.                         }
  146.  
  147.     printf("data after evaluation \n");
  148.     printf("pid\tAT\tpri\tET\tCT\twt\tTT\n");
  149.     while(n!=i)
  150.     {
  151.         printf("%d\t",pid[n]);
  152.         printf("%d\t",at[n]);
  153.         printf("%d\t",pri[n]);
  154.         printf("%d\t",et[n]);
  155.         printf("%d\t",ct[n]);
  156.         printf("%d\t",wt[n]);
  157.         printf("%d\n",tt[n]);
  158.  
  159.         n++;
  160.     }
  161.     n=0;
  162.     while(n!=i)
  163.     {
  164.  
  165.         avgwt=wt[n]+avgwt;
  166.         avgtt=tt[n]+avgtt;
  167.         n++;
  168.     }
  169.  
  170.     printf("\n*******average waiting time is %f******\n",(avgwt/n));
  171.     printf("*******average turnaound time is %f******\n",(avgtt/n));
  172.  
  173.     getch();
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement