C program for Priority Scheduling

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

C program for Priority Scheduling

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
 {
   clrscr();
   int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
   printf("Enter the number of process : ");
   scanf("%d",&n);
   printf("\n Enter process : time priorities \n");
   for(i=0;i<n;i++)
    {
      printf("\nProcess no %d : ",i+1);
      scanf("%d  %d",&pt[i],&pp[i]);
      p[i]=i+1;
    }
  for(i=0;i<n-1;i++)
   {
     for(int j=i+1;j<n;j++)
     {
       if(pp[i]<pp[j])
       {
         x=pp[i];
         pp[i]=pp[j];
         pp[j]=x;
         x=pt[i];
         pt[i]=pt[j];
         pt[j]=x;
         x=p[i];
         p[i]=p[j];
         p[j]=x;
      }
   }
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
 {
   w[i]=t[i-1];
   awt+=w[i];
   t[i]=w[i]+pt[i];
   atat+=t[i];
 }
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time   Priority \n");
for(i=0;i<n;i++)
  printf("\n %d \t\t %d  \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

47 Responses to “C program for Priority Scheduling”

  1. hi
    i want implementation of priority scheduling in c for xv6 perating system, can anyone kelp me?

    Reply
  2. plz give me code of round robin priorty base scheduling in c.reply as soon as possible

    Reply
    • rizwan

      #include
      main()
      {int n,i,j,at[10],bt[10],p[10],temp,T,st[10],tat[10],wt[10],S,TBT=0,exe[25],k,BT[10],ft[10];float awt=0.0,atat=0.0;
      //———Reading values—————
      printf(“enter the number of process\n”);
      scanf(“%d”,&n); //Accept number of process
      //–Initializing————————
      for(i=0;i<n;i++)
      {p[i]=-1;
      at[i]=-1;
      bt[i]=-1;
      st[i]=-1;
      wt[i]=0;
      tat[i]=0;}
      //———————————————-
      printf("enter the arrival time & burst time for each process \n");
      for(i=0;i<n;i++)
      {printf("process%d: \n",i);p[i]=i;
      printf(" arrival time:");scanf("%d",&at[i]);//Read the arrival time for each process.
      printf(" burst time:");scanf("%d",&bt[i]); BT[i]=bt[i]; //Read the burst time for each process.
      }
      printf("\n enter the time slot:");scanf("%d",&T); //Accepts the time slot to be given.
      //——-Sorting according to arrival time—
      for(i=0;i<n;i++)
      for(j=0;jat[j+1])
      {temp=p[j];p[j]=p[j+1];p[j+1]=temp;
      temp=at[j];at[j]=at[j+1];at[j+1]=temp;
      temp=bt[j];bt[j]=bt[j+1];bt[j+1]=temp;
      temp=BT[j];BT[j]=BT[j+1];BT[j+1]=temp;
      }
      }
      //——-Printing process after sorting according to at——-
      for(i=0;i%d ->%d”,p[i],at[i],bt[i]);
      }
      S=at[0]; //S=arrival time of first process.
      //———Calculating the total burst time————-
      for(i=0;i<n;i++)
      TBT=bt[i]+TBT;
      printf("\n Total Burst Time(TBT)=%d \n",TBT);
      //—–Scheduling the process———–
      k=0;
      printf("Scheduling……\n");
      i=0;
      while(S<TBT)
      {printf("\nS=%d",S);

      if((at[i]=T)
      {bt[i]=bt[i]-T;S=S+T;ft[i]=S;printf(” FT=%d”,S);printf(” rBT=%d”,bt[i]);}
      else
      {S=S+bt[i];bt[i]=0;ft[i]=S;printf(” FT=%d”,S);printf(” rBT=%d”,bt[i]);}

      }
      if(i=n)i=0;

      }//while
      printf(“\n process execution order:\n”);
      for(i=0;i<k;i++)
      printf("%d,",exe[i]);
      //for(i=0;i FT=%d,”,p[i],ft[i]);
      //or(i=0;i ST=%d,”,p[i],st[i]);
      for(i=0;i<n;i++)
      tat[i]=ft[i]-at[i];
      for(i=0;i<n;i++)
      wt[i]=tat[i]-BT[i];
      //——–CALCULATING AV.WT & AV.TAT—–
      for(i=0;i<n;i++)
      {atat=atat+tat[i];awt=awt+wt[i];
      }
      atat=atat/n;
      awt=awt/n;
      //————-DISPLAY————–
      printf("\n");
      printf("process start time wait time turn around time finish time burst time\n");
      for(i=0;i<n;i++)
      printf("process %d %d %d %d %d %d\n",p[i],st[i],wt[i],tat[i],ft[i],BT[i]);
      printf("average wait time=%f \naverage turn around time=%f \n",awt,atat);

      }//main

      Reply
  3. aqeel

    i want simple code for preemptive round robin in c++ as early as possible thanks in advance

    Reply
  4. Vishakha

    can you please give me the code for non preemptive priority scheduling??

    Reply

Leave a Reply