C program for Priority Scheduling

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();
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

47 thoughts on “C program for Priority Scheduling

  1. //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. #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. 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. thanks 4r a given such type of program ,due to this we easily able to learn and more program easy so thanku so much once again.

  5. hey this is very correctly code of fcfs premitive

    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int tot=0,wt[10],i,n;
    float avg=0;
    clrscr();
    printf(“enter no of processes:”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("enter process%d name:\n",i+1);
    scanf("%s",&p[i]);
    printf("enter process time");
    scanf("%d",&pt[i]);
    }

    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+et[i-1];
    tot=tot+wt[i];
    }
    avg=(float)tot/n;
    printf("p_name\t P_time\t w_time\n");
    for(i=0;i<n;i++)
    printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  6. primitive__round roubin algo program in c……….

    #include
    #include
    void main()
    {
    char p[10][5],temp[5];
    int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
    float avgwt;
    clrscr();
    printf(“enter no of processes:”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("enter process%d name:",i+1);
    scanf("%s",&p[i]);
    printf("enter process time:");
    scanf("%d",&pt[i]);
    printf("enter priority:");
    scanf("%d",&pr[i]);
    }
    for(i=0;i<n-1;i++)
    {
    for(j=i+1;jpr[j])
    {
    temp1=pr[i];
    pr[i]=pr[j];
    pr[j]=temp1;
    temp1=pt[i];
    pt[i]=pt[j];
    pt[j]=temp1;
    strcpy(temp,p[i]);
    strcpy(p[i],p[j]);
    strcpy(p[j],temp);
    }
    }
    }
    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+et[i-1];
    totwt=totwt+wt[i];
    }
    avgwt=(float)totwt/n;
    printf("p_name\t p_time\t priority\t w_time\n");
    for(i=0;i<n;i++)
    {
    printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
    }
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  7. plz help me.
    I want simple c codes for priority,SRTF , multilevel queue cpu scheduling algorithms.

  8. good day.. i need a program in borland c that is priority scheduling algorithm non preemptive..
    input values is the process,arrival time, burst time and priority number dn calculate the waiting time and turn around time..
    pls help me… i need it badly..
    Thanks for helping me..

  9. can any body give the source code for combination of 2 algorithms like fcfs+round robin or priority or sjf,,,,,,,,,

  10. haloo.. Good day..

    we need a simple non-preemptive Priority scheduling,,can anybody please help us.?.
    Please help us..!! this is for our final project..just a simple program will do..
    It input how many process,burst time and priority number then calculate the average time and waiting time..and print it’s gantt chart.
    Please do help us..we need it badly..
    Thank you very much..Godbless us always.

  11. #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(); } – See more at: http://studentprojects.in/source-codes/software-programs/c/c-advanced-programs/c-program-for-priority-scheduling/#sthash.GuPikOWE.dpuf

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

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

  14. #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

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in