The algorithm explanation:
Non-preemptive Priority scheduling Each process has (arrival time, priority, and burst(execution) time) the process with first arrival time (less arrival time process) will be executed first, if two processes have same arrival time, then compare to priorities (highest process first). Also, if two processes have same priority then compare to process number (less process number first). This process is repeated while all process get executed.
I used the code below but I did not get the correct answer. I have been trying to solve for 2 weeks it but unfortunately I do not know where the error is (it is a logical error but I could not Identify it). I tried to debug it many times but still I could not find what causes it.
Thanks.
#include <stdio.h>
void main()
{
int pn = 0; //Processes Number
int CPU = 0; //CPU Current time
int allTime = 0; // Time neded to finish all processes
printf("Enrer Processes Count: ");
scanf("%d",&pn);
int AT[pn];
int ATt[pn];
int NoP = pn;
int PT[pn]; //Processes Time
int PP[pn]; //Processes piriorty
int waittingTime[pn];
int turnaroundTime[pn];
//Scanning Time and Piriorty
for(int i=0 ;i<pn ;i++){
printf("\nProcessing time for P%d: ",i+1);
scanf("%d",&PT[i]);
printf("Piriorty for P%d: ",i+1);
scanf("%d",&PP[i]);
printf("Arrival Time for P%d: ",i+1);
scanf("%d",&AT[i]);
ATt[i] = AT[i];
}
int LAT = 0; //LastArrivalTime
for(int i = 0; i < pn; i++)
if(AT[i] > LAT)
LAT = AT[i];
int ATv = AT[0]; //Pointing to Arrival Time Value
int ATi = 0; //Pointing to Arrival Time indix
int P1 = PP[0]; //Pointing to 1st piriorty Value
int P2 = PP[0]; //Pointing to 2nd piriorty Value
//findding the First Arrival Time and Highst piriorty Process
while(NoP > 0 && CPU <= 1000){
for(int i = 0; i < pn; i++){
if(ATt[i] < ATv){
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
else if(ATt[i] == ATv || ATt[i] <= CPU){
if(PP[i] != (pn+1))
P2 = PP[i];
if(P2 < P1){
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
}
}
if(CPU < ATv){
CPU = CPU+1;
continue;
}else{
waittingTime[ATi] = CPU - ATt[ATi];
CPU = CPU + PT[ATi];
turnaroundTime[ATi] = CPU - ATt[ATi];
ATt[ATi] = LAT +10;
ATv = LAT +10; //Pointing to Arrival Time Value
ATi = 0; //Pointing to Arrival Time indix
PP[ATi] = pn + 1;
P1 = PP[0]; //Pointing to 1st piriorty Value
P2 = PP[0]; //Pointing to 2nd piriorty Value
printf("Iam in");
NoP = NoP - 1;
}
}
printf("\nPN\tPT\tPP\tWT\tTT\n\n");
for(int i = 0; i < pn; i++){
printf("P%d\t%d\t%d\t%d\t%d\n",i+1,PT[i],PP[i],waittingTime[i],turnaroundTime[i]);
}
int AvgWT = 0;
int AVGTaT = 0;
for(int i = 0; i < pn; i++){
AvgWT = waittingTime[i] + AvgWT;
AVGTaT = turnaroundTime[i] + AVGTaT;
}
printf("AvgWaittingTime = %d\nAvgTurnaroundTime = %d\n",AvgWT/pn,AVGTaT/pn);
}
/*
Test Cases:
PT: Processing Time
PP: Process priority
WT Waitting Time
TaT: Turnaround Time
Arrival time for 1st 2 cases is 0
PN PT PP WT TaT
P1 10 3 6 16
P2 1 1 0 1
P3 2 4 16 18
P4 1 5 18 19
P5 5 2 1 6
PN PT PP WT TaT
P1 1 1 0 1
P2 2 2 1 3
P3 3 3 3 6
P4 4 4 6 10
P5 5 5 10 15
PN PP AT PT WT TaT
1 2 0 3 0 3
2 6 2 5 11 16
3 3 1 4 2 6
4 5 4 2 7 9
5 7 6 9 12 21
6 4 5 4 2 6
7 10 7 10 18 30
*/```