C program for Priority Scheduling

Monday, March 29th, 2010

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();
}
Avatar Image

Author Name :
Ranjith

Total : 26 Comments


26 Responses to “C program for Priority Scheduling”

  1. Priyanka says:

    //non -preemptive priority

    #include
    #include
    main()
    {
    int p[10],wt[10],bt[10],tat[10],ttat=0,twt=0,i,j,temp,n,pri[10];
    float awt,atat;
    clrscr();
    printf(“enter the no. of process=>\n”);
    scanf(“%d”,&n);
    printf(“enter process=>\n”);
    for(i=0;i\n”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&bt[i]);
    }
    printf("Enter the priority\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&pri[i]);
    }

    for(i=0;i<n;i++)
    {
    for(j=0;jpri[j+1])
    {
    temp=p[j];
    p[j]=p[j+1];
    p[j+1]=temp;

    temp=bt[j];
    bt[j]=bt[j+1];
    bt[j+1]=temp;

    temp=pri[j];
    pri[j]=pri[j+1];
    pri[j+1]=temp;
    }
    }
    }
    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+bt[i-1];
    twt=twt+wt[i];
    }
    for(i=0;i<n;i++)
    {
    tat[i]=wt[i]+bt[i];
    ttat=ttat+tat[i];
    }
    awt=(float)twt/n;
    atat=(float)ttat/n;
    printf("p_no.\tP_bt\t p_wt\t p_tat\n");
    for(i=0;i<n;i++)
    printf("%d \t%d\t %d\t %d\n",p[i],bt[i],wt[i],tat[i]);
    printf("total waiting time=%d\n avg waiting time=%f",twt,awt);
    printf("\ntotal turnedaroundtime=%d\n avg turnedaround time=%f",ttat,atat);
    getch();
    }

  2. Priyanka says:

    #include
    #include
    void main()
    {
    int i,j,b[10],temp1,wt[10],tut[10],awt,atut,n,swt=0,stut=0,temp,pt[10];
    clrscr();
    printf(“Enter the no of jobs:”);
    scanf(“%d”,&n);
    printf(“Enter the burst time:”);
    for(i=1;i<=n;i++)
    scanf("%d",&b[i]);
    printf("Enter the priorty of jobs:");
    for(i=1;i<=n;i++)
    scanf("%d",&pt[i]);
    for(i=1;i<n;i++)
    {
    for(j=i;jpt[j])
    {
    temp=b[i];
    b[i]=b[j];
    b[j]=temp;
    temp1=pt[i];
    pt[i]=pt[j];
    }
    }
    }
    wt[1]=0;
    for(i=2;i<=n;i++)
    wt[i]=wt[i-1]+b[i-1];
    for(i=1;i<=n;i++)
    tut[i]=wt[i]+b[i];
    for(i=1;i<=n;i++)
    {
    swt=swt+wt[i];
    stut=stut+tut[i];
    }
    atut=stut/n;
    awt=swt/n;
    printf("Avg waiting time=%d\n",awt);
    printf("Avg turn arround time=%d\n",atut);
    getch();
    }

  3. Dinesh sharma says:

    sir i think ur source code is not purely correct
    #include
    #include
    #include
    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();
    }

  4. visha says:

    sir i want program of priority preemptive case…..
    plz if u can help me as soon as possible…

  5. piya says:

    We want sjf preemptive c program.
    plz sir help us as soon as possible.

  6. sangram nalawade says:

    i want prirority shedulling which is compatible for linux operating system
    please………………..as fast as u can sir

  7. m.karthik says:

    sir i want fcfs progam in c using arrival time,burst time

  8. vishal uec ujjain says:

    sir, actully this program are not so deficult but no body want to try…don’t post program…just post the idea of the program..like which logic will work..

  9. vishal uec ujjain says:

    //fcfs program
    #include
    #include

    main()
    {
    float avgwt,avgtt;
    char pname[10][10],c[10][10];
    int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
    printf(“\n\nEnter the number of processes:”);
    scanf(“%d”,&n);
    printf(“\n\n Enter the NAME, BURST TIME and ARRIVAL TIME of thr process”);
    for(i=0;i<n;i++)

    {
    printf("\n\nNAME : ");
    scanf("%s",&pname[i]);
    printf("\n\nBURST TIME : ");
    scanf("%d",&bt[i]);
    printf("\n\n ARRIVAL TIME :");
    scanf("%d",&at[i]);
    }
    for(i=0;i<n;i++)
    for(j=i+1;jat[j])

    {
    t=at[i];
    at[i]=at[j];
    at[j]=t;
    q=bt[i];
    bt[i]=bt[j];
    bt[j]=q;
    strcpy(c[i],pname[i]);
    strcpy(pname[i],pname[j]);
    strcpy(pname[j],c[i]);
    }
    }
    wt[0]=0;
    for(i=0;i<n;i++)

    {
    wt[i+1]=wt[i]+bt[i];
    sum=sum+(wt[i+1]-at[i]);
    tt[i]=wt[i]+bt[i];
    ss=ss+bt[i];
    }
    avgwt=(float) sum/n;
    avgtt=(float) sbt/n;
    printf("\n\nAverage waiting time=%f",avgwt);
    printf("\n\nAverage turn-around time=%f",avgtt);
    printf("\n\n GANTT CHART\n");
    for(i=0;i<n;i++)

    {printf("|\t%s\t",pname[i]);
    }
    printf("\n");
    for(i=0;i<n;i++)

    {
    printf("%d\t\t",wt[i]);
    }
    printf("%d\n",ss);
    printf("\n");
    }

  10. priya chaudhary says:

    can any body give the source code of preemptive sjf (not non preemptive) as soon as possible….

  11. Prasad says:

    #include
    #include
    void main()
    {

    int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,att,i,j;
    clrscr();
    printf(“Enter the number of process : “);
    scanf(“%d”,&n);

    for(i=0;i<n;i++)
    {
    printf("\nProcess no %d : ",i+1);
    scanf("%d" ,&pt[i]);
    printf("\ntime priorities :");
    scanf("%d",&pp[i]);
    p[i]=i+1;
    }
    for(i=0;i<n-1;i++)
    {
    for(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];
    att=t[0];
    for(i=1;i<n;i++)
    {
    w[i]=t[i-1];
    awt+=w[i];
    t[i]=w[i]+pt[i];
    att+=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;
    att/=n;
    printf("\n Average Wait Time : %d \n",awt);
    printf("\n Average Turn Around Time : %d \n",att);
    getch();
    }

  12. Prasad koranne says:

    hi thank u for giving me your code
    i m from MIT college aurangabad
    this is an re-designed code for the Priority job scheduling program using the C language

    #include
    #include
    void main()
    {

    int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,att,i,j;
    clrscr();
    printf(“Enter the number of process : “);
    scanf(“%d”,&n);

    for(i=0;i<n;i++)
    {
    printf("\nProcess no %d : ",i+1);
    scanf("%d" ,&pt[i]);
    printf("\ntime priorities :");
    scanf("%d",&pp[i]);
    p[i]=i+1;
    }
    for(i=0;i<n-1;i++)
    {
    for(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];
    att=t[0];
    for(i=1;i<n;i++)
    {
    w[i]=t[i-1];
    awt+=w[i];
    t[i]=w[i]+pt[i];
    att+=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;
    att/=n;
    printf("\n Average Wait Time : %d \n",awt);
    printf("\n Average Turn Around Time : %d \n",att);
    getch();
    }

  13. shanu says:

    we need fcfs sjf round robin n priority scheduling pgm tat should be v simple……make it as earlier as possible………………pls

  14. praneetha says:

    I need a easy implementation of LRU(least recently used) page replacement algorithm

  15. /*The correct one is this for priority cpu sheduling
    by :anaghvj*/
    #include
    #include
    #include
    void main()
    {
    int temp1,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i,j;
    clrscr();
    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(j=i+1;jpp[j])
    {
    temp1=pp[i];
    pp[i]=pp[j];
    pp[j]=temp1;
    temp1=pt[i];
    pt[i]=pt[j];
    pt[j]=temp1;
    temp1=p[i];
    p[i]=p[j];
    p[j]=temp1;
    }
    }
    }
    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();
    }

  16. shadab says:

    sir,
    i want a program for priority scheduling by which we can solve response time also.
    i want it asap…its really urgent.
    if smebody knws please post it or send me a copy to my ,ail.

  17. Randhir Singh says:

    #include
    #include
    void main()
    {
    int n,p[10],b[10],i,j,temp,temp1,pr[10],q[10];
    float c,s=0.0;
    clrscr();
    printf(“Enter the no. of process\n”);
    scanf(“%d”,&n);
    printf(“Enter processes\n”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&p[i]);
    }
    printf("Enter brust time\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    }
    printf("Enter the priority\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&pr[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=1;j<n;j++)
    {
    temp=pr[i];
    temp1=p[i];
    pr[i]=pr[j];
    p[i]=p[j];
    pr[j]=temp;
    p[j]=temp1;
    }
    }
    for(i=0;i<n;i++)
    {
    printf("process is %d and it's Priority is %d\n",p[i],pr[i]);
    }
    p[0]=0;
    for(i=0;i<n;i++)
    {
    p[i+1]=p[i]+b[i];
    printf("Waiting time=%d\n",p[i]);
    }
    for(i=0;i<n;i++)
    {
    s=s+p[i];
    }
    c=s/n;
    printf("Average waiting time=%f",c);
    getch();

  18. honey says:

    i want simple code for sjf preemptive

  19. anish kamadana says:

    chaaaaaaaa

  20. jaya pandey says:

    kal mera birthday hai……..

  21. rani says:

    // non preemitive algorithm

    #include
    #include
    void main()
    {
    int n,p[10],b[10],i,j,temp,temp1,pr[10],q[10];
    float c,s=0.0;
    clrscr();
    printf(“Enter the no. of process\n”);
    scanf(“%d”,&n);
    printf(“Enter processes\n”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&p[i]);
    }
    printf("Enter brust time\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    }
    printf("Enter the priority\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&pr[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=1;j<n;j++)
    {
    temp=pr[i];
    temp1=p[i];
    pr[i]=pr[j];
    p[i]=p[j];
    pr[j]=temp;
    p[j]=temp1;
    }
    }
    for(i=0;i<n;i++)
    {
    printf("process is %d and it's Priority is %d\n",p[i],pr[i]);
    }
    p[0]=0;
    for(i=0;i<n;i++)
    {
    p[i+1]=p[i]+b[i];
    printf("Waiting time=%d\n",p[i]);
    }
    for(i=0;i<n;i++)
    {
    s=s+p[i];
    }
    c=s/n;
    printf("Average waiting time=%f",c);
    getch();
    }

  22. Sweety says:

    can any body give the source code of preemptive sjf (not non preemptive) as soon as possible… . . .
    plzzzzzzzz………………..

  23. Sweety Shinde says:

    plz post preemptive code(not non preemptive) for all type of scheduling…..
    hurry up…………..plzzzz……..:)

  24. suneel says:

    plz post preemptive code in c language

  25. sara says:

    Hello!!

    Thank you for your help. I actually am building a simulator that takes processes which has 2 CPU’s and swaps them out after regular intervals. It uses fcfs and round robin algorithms, please can you help me with that.

  26. Raaghavee says:

    Hi…
    I want the code for preemptive priority scheduling algorithm using linked list(i.e., using structure) which includes arrival time for each process..
    can anyone please post it if u have ASAP?

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup