Priority Scheduling with arrival time

I'm trying to implement Priority Scheduling algorithm but with arrival time is giving, that do the first job first but take into consideration the priority of the job and then do the Gantt graph and calculate the waiting time and the average waiting time but I could not sort by the FJS and priority together that's what I reached so far

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
53
#include <iostream>
#include <iomanip>
using namespace std;
struct process{

    int arrival,burst,priority,wating;
};

void addP(int num){
    process p[num];
    cout<<"Process ID"<<setw(15)<<"Arrival time"<<setw(15)<<"Burst time"<<setw(15)<<"Priority"<<endl;

    for(int i=0;i<num;i++){
        cout<<"P"<<i+1;
        cin>>p[i].arrival>>p[i].burst>>p[i].priority;
    }
    int temp=0,x=0;
    for(int i=0;i<num;i++){
        for(int j=i+1;j<num;j++){
            if(p[i].priority>p[j].priority){
                            temp=p[i].priority;
                            p[i].priority=p[j].priority;
                            p[j].priority=temp;

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

                            temp=p[i].arrival;
                            p[i].arrival=p[j].arrival;
                            p[j].arrival=temp;
            }
        }

    }
    cout<<"Process ID"<<setw(15)<<"Arrival time"<<setw(15)<<"Burst time"<<setw(15)<<"Priority"<<endl;
    for(int i=0;i<num;i++){
        cout<<"P"<<i+1<<setw(20)<<p[i].arrival<<setw(20)<<p[i].burst<<setw(20)<<p[i].priority<<endl;
    }

}


int main()
{

    int numOfProcess;
    cout<<"Enter The Number of processes ";
    cin>>numOfProcess;
    addP(numOfProcess);

    return 0;
}
Last edited on
Topic archived. No new replies allowed.