C Program for Priority CPU Scheduling Algorithm

Saturday, September 19th, 2009

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<conio.h>
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;j<n;j++)
  {
   if(pr[i]>pr[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();
   }

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

Author Name :
Ranjith

Total : 37 Comments


37 Responses to “C Program for Priority CPU Scheduling Algorithm”

  1. soujanya says:

    really thanks a lotttttttt …..
    dey r quite helpful to me

  2. Pabon says:

    Very Very Thanks

  3. Aashutosh says:

    Thankyou

  4. thank u very very much for this solution

  5. LB's says:

    But WHAT if processes having different arrival time????

  6. vijay says:

    its pt[i-1] n’t et[i-1]

  7. subash says:

    very very thanks

  8. rayan daou says:

    //i wrote this better program it considers different arrival times and prints a verbal gannt daigram for priroty scheduling with preemtion without preemtion and with aging any one is welcome to use it just thank me if u like it

    #include
    #include
    typedef struct{
    int arrival;
    int burst;
    int priorty;
    int index;
    int status;
    }process;

    void swap(process *x,process *y)
    {
    process temp;
    temp = *x;
    *x = *y;
    *y = temp;

    }

    void bublesort(process p[], int n)

    {
    int i,j;
    for(i=0;i<(n-1);i++)
    for(j=0;j p[j+1].arrival||(p[j].arrival==p[j+1].arrival&&p[j].priorty>p[j+1].priorty))
    swap(&p[j],&p[j+1]);

    }

    void sortpr(process p[],int n)
    {
    int i,j;
    for(i=0;i<(n-1);i++)
    for(j=0;j p[j+1].priorty)
    swap(&p[j],&p[j+1]);

    }
    void deletep(process p[],int size,int index)
    { int i=index;
    while(i<size-1){
    p[i]=p[i+1];
    i++;}
    return;
    }

    bool nonew(process p[],int size)
    { for(int i=0;i<size;i++){
    if(p[i].status==0 )return false;
    }
    return true;
    }
    bool noready(process p[],int size)
    {for(int i=0;i<size;i++)
    if(p[i].status==1)
    return false;
    return true;
    }

    int ready(process p[],int size)
    { int min=0;
    for(int i=0;i<size;i++)
    if(p[i].status==1&&p[i].priorty<p[min].priorty)
    min=i;
    return min;
    }

    void NonPreemtive(process p[],int size)
    { int i,j,minpr,current=p[0].arrival;
    for(i=0;icurrent){
    current=p[i+1].arrival;}
    else{ j=i+1;
    minpr=p[j].priorty;
    for(j=i+1;j<size;j++)
    { if(p[j].arrival<current){
    if (p[j].priorty<minpr){
    minpr=p[j].priorty;
    swap(&p[i+1],&p[j]);
    }}

    }
    }
    }
    }
    printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
    }

    void NonPreemtiveAging(process p[],int size)
    {int i,j,minpr,current=p[0].arrival;

    for(i=0;icurrent){
    current=p[i+1].arrival;}
    else{ j=i+1;
    minpr=p[j].priorty-(current-p[j].arrival)/5;
    for(j=i+1;j<size;j++)
    { if(p[j].arrival<current){
    if (p[j].priorty-(current-p[j].arrival)/5<minpr){
    minpr=p[j].priorty-(current-p[j].arrival)/5;
    swap(&p[i+1],&p[j]);
    }
    }

    }
    }
    }
    }
    printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
    }

    void Preemtive(process p[],int size)
    {
    int i=0,current=p[0].arrival,j,test;
    while(size!=0)
    {
    printf("p%d from %d to ",p[i].index,current);
    if(nonew(p,size)){
    printf("%d\n",current+p[i].burst);
    current=current+p[i].burst;
    deletep(p,size,i);
    size–;
    if(size==0) break;
    i=ready(p,size);

    continue;

    }
    else{test=0;
    for(j=0;j<size;j++)
    {
    if(p[j].arrival<current+p[i].burst)
    { if(p[j].status==1)continue;

    if(p[j].priorty<p[i].priorty)
    {
    printf(" %d\n",p[j].arrival);
    p[i].burst=p[i].burst-(p[j].arrival-current);
    p[i].status=1;
    current=p[j].arrival;
    test=1;
    i=j;
    break;
    }else p[j].status=1;
    }else if(p[j].arrival==current+p[i].burst)p[j].status=1;
    }
    if(test==1)continue;
    printf("%d\n",current+p[i].burst);
    current=current+p[i].burst;
    deletep(p,size,i);
    size–;
    if (!noready(p,size)){
    i=ready(p,size);}
    else {
    current=p[i].arrival;}
    }

    }
    return;
    }

    void main()
    {
    process p[5];
    int i,c;
    printf("Welcome\n");
    printf( "Choose the mode of scedueling:\npress 1 for non preemtive\npress 2 for non preemtive with aging\npress 3 for preemptive\n");
    scanf("%d",&c);
    for (i=0;i<5;i++)
    {
    printf("\nenter the arrival time of p %d:",i);
    scanf("%d",&p[i].arrival);
    printf("enter the burst time of p %d:",i);
    scanf("%d",&p[i].burst);
    printf("enter the priorty of p %d:",i);
    scanf("%d",&p[i].priorty);
    p[i].status=0;
    p[i].index=i;
    }
    bublesort(p,5);
    printf("\nsorted:\nprocess arrival time burst time priorty\n");
    for (i=0;i<5;i++)
    {

    printf( " p%d %d %d %d\n",p[i].index,p[i].arrival,p[i].burst,p[i].priorty);
    }
    if(c==1)
    NonPreemtive(p,5);
    else{
    if(c==2) NonPreemtiveAging(p,5);
    else if (c==3)Preemtive(p,5);
    }
    getch();

    }

  9. ankur says:

    thanks a lot….

  10. sravanthi says:

    really very helpful

  11. swati agrawal says:

    thank’s ”””””””””’

  12. saranya sri says:

    hw headerfile will come in c program

  13. jesmar says:

    wow,,,really excellent and easy to understand……tnx

  14. ramya says:

    thanks alot realy very helpfull it is

  15. swathi says:

    thank u..

  16. Sajedur Rahman says:

    Thanks

  17. Flavah says:

    Great job… You prevented me from getting a zero on my end of year project. Got this to code but was not up to it. I actually coded the fcfs, sjf, rr and the none-pree-priority in least than a week but got exhausted and was about to call it quits. Then there you were, I owe you one bro!

    Will post my code shortly along with the disk scheduling algorithms also……

    THANX

  18. Flavah says:

    Check out this fcfs. I think there is a few bugs but nothing you guy can;t resolve….

    Let me know the errors you encountered!

    //First Come First Serve
    #include
    #include
    #include
    #include
    #include

    //Global Variables
    int result;
    int quan = 0;
    float avg_tat;
    float avg_wt;
    float total_wt = 0;
    float total_tat = 0;
    time_t n, later;

    //Global Data Structure
    struct cProcess
    {
    int id;
    int priority;
    int start_time;
    int end_time;
    int arrive_time;
    int burst_time;
    int wait_time;
    int turn_time;
    struct cProcess *next;
    };

    //Menu and Sub-Menu Function Prototype
    void Menu(); //Function that displays menu options
    void cpuProcess(); //Function that controll cpu processes

    //Function that displays menu options
    //Process Function Prototype
    void FCFS(); //Function that Perform FCFS Scheduling

    /***********************SLEEP**************************/
    //Function that pause system for a selected period
    void sleep(int delay)
    {
    n=time(NULL);
    later=n+delay;
    while(nnul”);
    return 0;
    } //End Mian Function

    /****************MENU TABLE******************************/
    //Function Menu That Display GUI
    void Menu()
    {
    char type;

    Logo(); //Call logo function to display program logo
    printf(“\n\t Select the type of process you require!\n”);
    printf(“\t —————————————-\n\n”);
    printf(“\t PRESS: [C] – CPU Processes\n”);
    printf(“\t [D] – DISK Processes\n\n”);
    printf(“\t [E] – Exit & Close Scheduler\n\n”);
    printf(“\t SELECT:[ ]\b\b”);
    scanf(“%c”,&type);
    switch(type)
    {

    case ‘c’:
    case ‘C’: Loading();
    cpuProcess();
    break;

    case ‘d’:
    case ‘D’: Loading();
    diskProcess();
    break;

    case ‘e’:
    case ‘E’: fflush(stdin);
    Exit();
    break;

    case ‘\n’: printf(“\a\n\t Invalid entry. Press any key to try again…\n”);
    getch();
    fflush(stdin);
    system(“cls”);
    Menu();

    default: printf(“\a\n\t Invalid entry. Press any key to try again…\n”);
    getch();
    fflush(stdin);
    system(“cls”);
    Menu();
    }
    }//End Meu Function

    /******************FCFS Scheduling*************************/
    //First come First Server Scheduler
    void FCFS()
    {
    int i=1;
    int type=0;
    int num, result;
    char* name;

    retry:;
    Logo();

    struct cProcess *head;
    struct cProcess *node,*ptr;

    name = “FCFS”;
    printf(“\n\t Schedule Type: %s”, name);
    printf(“\n\t ——————-\n”);
    printf(“\n\t Enter the number of processes: “);
    scanf(“%d”,&num);

    result = Error(num);

    if(result == 0)
    {
    printf(“\n\a\t Invalid process entry! Press any key to try again…\n”);
    getch();
    goto retry;
    }

    head = (struct cProcess*)malloc(sizeof(struct cProcess));
    head->id = i;

    printf(“\n\t Enter P%d Arrival Time: “,i);
    scanf(“%d”, &head->arrive_time); //Add process arrival time

    printf(“\n\t Enter P%d Burst Time: “,i);
    scanf(“%d”, &head->burst_time); //Add process bust time

    printf(“\n\t Enter P%d Priority: “,i);
    scanf(“%d”, &head->priority); //Add process priority number where 1 is the highest or significantly urgent

    head->start_time = 0;
    head->end_time = head->start_time + head->burst_time;
    head->turn_time = head->end_time – head->arrive_time;
    head->wait_time = head->start_time – head->arrive_time;

    total_wt += head->wait_time;
    total_tat += head->turn_time;

    head->next = NULL;
    ptr = head;

    for(i=2;iid = i;

    printf(“\n\t Enter P%d Arrival Time: “,i);
    scanf(“%d”, &node->arrive_time); //Add process arrival time

    printf(“\n\t Enter P%d Burst Time: “,i);
    scanf(“%d”, &node->burst_time); //Add process bust time

    printf(“\n\t Enter P%d Priority: “,i);
    scanf(“%d”, &node->priority); //Add process priority number where 1 is the highest or significantly urgent

    node->start_time = ptr->end_time;
    node->end_time = node->start_time + node->burst_time;
    node->turn_time = node->end_time – node->arrive_time;
    node->wait_time = node->start_time – node->arrive_time;

    total_wt += node->wait_time;
    total_tat += node->turn_time;

    node->next = NULL;
    ptr->next = node;
    ptr = node;
    }

    result = Check(head); //Call result funtion to compare user input to system requirements

    if(result == 0)
    {
    printf(“\a\n\t Error – At least one process MUST arrive at zero time!”);
    printf(“\n\t Press any key to re-enter…”);
    getch();
    free(head);
    FCFS();
    }

    Sort(head); //Call arrange function to arrange processes in arrival time order

    avg_wt = total_wt/num; //Calculate process average wait time
    avg_tat = total_tat/num; //Calculate process average turn arounf time

    cDisplay(head,name,type); //Call display functon to display process results
    } //End FCFS() Function

    /*************************DISPLAY***************************/
    //Display Function
    void cDisplay(struct cProcess *start,char* name,int type)
    {
    Logo(); //Call Program logo
    struct cProcess *temp; //Create pointer of type object
    printf(“\t Schedule Type: %s”, name);

    if(quan > 0) //Do if condition is satisfied
    printf(“\n\t Quantum Slice: %d”,quan); //Show quantum slice for Round Robin scheduler
    printf(“\n\t Process Result Table\n”);
    printf(“\t ——————–\n\n”);
    printf(“\t| ID | Ar.Time | CPU.Time | Start | End | Priority | WT | TAT |”);
    printf(“\n\t ————————————————————-\n”);
    while(start != NULL)
    {
    printf(“\t P%d\t %d\t %d\t %d\t %d\t %d\t %d\ %d\n”, start->id,start->arrive_time,start->burst_time,start->start_time,start->end_time,start->priority,start->wait_time,start->turn_time);
    temp = start->next; //Set temp pointer to next object inlist
    free(start); //Free start memory to prevent memory leak
    start = temp; //Set temp pinter to start
    sleep(1); //Call sleep function and pause system for 1 second
    }
    printf(“\n\t Avg Waiting Time: %.1f”, avg_wt);
    printf(“\n\t Avg Turn Around Time: %.1f”, avg_tat);
    printf(“\n\n\t Table Completed!!!”);
    printf(“\n\t Press any key to continue…”);

    //Set global values to default
    avg_tat = 0;
    avg_wt = 0;
    total_wt = 0;
    total_tat = 0;
    quan = 0;

    free(start); //Free memory within the list!
    free(temp); //Free memory within the list!
    getch();

    if(type == 0)
    cpuProcess(); //Call cpuProcess Menu Function
    else
    diskProcess(); //Call diskProcess Menu Function

    }//End Display() Function

    /*********************LOADING*******************************/
    //Loading Function
    void Loading()
    {
    int x;
    int random;
    srand(time(NULL)); //Initialize srand

    random = rand()%4 + 3; //Randomize number
    printf(“\n\t Please Wait”);
    sleep(1);
    for(x=0;x=0){
    printf(” “);
    }
    }
    sleep(1); //Call sleep function to pause system for 1 second
    }//End Loading() function

    /*******************Error*****************************/
    //Function Error
    int Error(int a)
    {
    int j=1,x=2;
    if(a < x || a == '\n'){
    j = 0;
    }
    return j;

    }//End Error() Function

  19. nilesh shah says:

    thanks a lotttt
    once again thank u very much very helpful progaram for us……………..

  20. macou says:

    thank you

  21. dipti says:

    its really a helpful programs for me .Thanks

  22. sarra says:

    please can you give me the code of “partitioning with fixed priority” on JAVA

  23. manoj says:

    r u people writing programs or something else…………
    what is use of making a such big programs ?

  24. rohan says:

    all the programs have error

  25. spacefree says:

    please give me a simple code for priority preemptive scheduling having different arrival time for each process…

  26. fadia jabeen says:

    Implemented in Dev c++

    Priority.cpp

    #include
    #include
    #include
    #include “process.h”
    #include “pfunctions.h”

    using namespace std;

    int main(int argc, char *argv[])
    {
    process Queue[20];
    int num;
    cout<>num;
    //process* Queue = new process[num];
    cout<<"Enter the following details"<<endl;
    Get_Details(Queue,num);
    Sort_Processes(Queue,num);
    makeQueue(Queue,num);
    make_Gantt_Chart(Queue,num);
    make_Table(Queue,num);
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    process.h

    #include
    #include
    #include

    using namespace std;

    struct process
    {
    string name;
    int arrival;
    int burst;
    int prior;
    int tat;
    int wait;
    int serve_time;
    };

    pfunctions.h

    #include
    #include
    #include

    void Get_Details(process Q[],int n);
    void Sort_Processes(process Q[],int n);
    void swap(process Q[],int i,int j);
    void makeQueue(process Q[],int n);
    void make_Gantt_Chart(process Q[],int n);
    void make_Table(process Q[],int n);

    using namespace std;

    void Get_Details(process Queue[],int n)
    {
    for(int i=0;i<n;i++)
    {
    cout<>Queue[i].name >> Queue[i].arrival >> Queue[i].burst >> Queue[i].prior;
    //cout<<endl;
    }

    }
    void Sort_Processes(process Q[],int n)
    {
    int current_time=0,i,j;

    //Insertion Sort w.r.t Arrival time
    for(i=0;i<n;i++)
    for(j=i+1;jQ[j].arrival)
    swap(Q,i,j);
    // Isertion sort w.r.t priority+arrival+burst
    for(i=0;i current_time)
    current_time = Q[i].arrival;
    for(j=i+1;j<n;j++)
    {
    if(Q[j].arrivalQ[j].prior)
    swap(Q,i,j);
    else if(Q[i].prior==Q[j].prior && Q[i].arrival>Q[j].arrival)
    swap(Q,i,j);
    else if(Q[i].prior==Q[j].prior && Q[i].arrival==Q[j].arrival && Q[i].burst>Q[j].burst)
    swap(Q,i,j);
    }
    }
    Q[i].serve_time = current_time;
    Q[i].tat = (current_time+Q[i].burst)-Q[i].arrival;
    Q[i].wait = current_time – Q[i].arrival;
    current_time = current_time + Q[i].burst;
    }
    }
    void swap(process Q[],int i,int j)
    {
    process p;
    p.arrival=Q[i].arrival;
    Q[i].arrival=Q[j].arrival;
    Q[j].arrival=p.arrival;

    p.name=Q[i].name;
    Q[i].name=Q[j].name;
    Q[j].name=p.name;

    p.burst=Q[i].burst;
    Q[i].burst=Q[j].burst;
    Q[j].burst=p.burst;

    p.prior=Q[i].prior;
    Q[i].prior=Q[j].prior;
    Q[j].prior=p.prior;
    }
    void makeQueue(process Q[],int n)
    {
    int i;
    cout<<"\n Queue:-\n";
    cout<<"—————————————————————————–\n";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].name<<" |";
    cout<<"\n—————————————————————————–\n";
    }
    void make_Gantt_Chart(process Q[],int n)
    {
    int i;
    cout<<"\nGantt chart:-\n";
    cout<<"—————————————————————————–\n";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].name<<" |";
    cout<<"\n—————————————————————————–\n";
    cout<<Q[0].arrival<<" ";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].serve_time+Q[i].burst<<" ";
    }
    void make_Table(process Q[],int n)
    {
    int i;
    float wait=0,tat=0;
    cout<<"\n\nCalculation:-\n\nName\tBurst\tArrival\tPriority\tWait\tTAT\n";
    for(i=0;i<n;i++)
    {
    cout<<"\n"<<Q[i].name<<"\t"<<Q[i].burst<<"\t";
    cout<<Q[i].arrival<<" \t "<<Q[i].prior<<"\t\t"<<Q[i].wait<<"\t"<<Q[i].tat;
    wait = wait+Q[i].wait ;
    tat = tat + Q[i].tat;
    }
    cout<<"\n——————————————————";
    cout<<"\n\nSUM=\t-\t-\t -\t\t"<<wait<<"\t"<<tat<<endl;
    cout<<"AVERAGE=-\t-\t -\t\t"<<wait/n<<"\t"<<tat/n<<"\n\n";
    }

  27. janet says:

    pls……… i need your help…… can u write out the algorithm of the program………i would really appreciate it.thanks

  28. mounika says:

    i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat

  29. mounika says:

    pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  30. mounika says:

    pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  31. mounika says:

    pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  32. vamshikrishna says:

    I had 1 doubt in 32 line, in function strcpy hw the prototype will be writtern

  33. vamshikrishna says:

    Rply me soon pls

  34. vamhsikrishna says:

    got it i have forgot ot write header file string.h

  35. mounika says:

    i want roundrobin &priority with arrivaltimes c program

  36. Harsh Patel says:

    i want cpu scheduling , process scheduling guaranteed algorithm…
    so can u plz..send me that one !!!!

Leave a Reply

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

Free email signup

Email: