[英]non-preemptive priority scheduling in C
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).我已经尝试解决了 2 周,但不幸的是我不知道错误在哪里(这是一个逻辑错误,但我无法识别它)。 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
*/```
The correct code is:正确的代码是:
#include <stdlib.h>
#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 PPt[pn];
int waittingTime[pn];
int turnaroundTime[pn];
int i=0;
//Scanning Time and Piriorty
for(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]);
PPt[i] = PP[i];
printf("Arrival Time for P%d: ",i+1);
scanf("%d",&AT[i]);
ATt[i] = AT[i];
}
int LAT = 0; //LastArrivalTime
for(i = 0; i < pn; i++)
if(AT[i] > LAT)
LAT = AT[i];
int MAX_P = 0; //Max Piriorty
for(i = 0; i < pn; i++)
if(PPt[i] > MAX_P)
MAX_P = PPt[i];
int ATi = 0; //Pointing to Arrival Time indix
int P1 = PPt[0]; //Pointing to 1st piriorty Value
int P2 = PPt[0]; //Pointing to 2nd piriorty Value
//findding the First Arrival Time and Highst piriorty Process
int j = -1;
while(NoP > 0 && CPU <= 1000){
for(i = 0; i < pn; i++){
if((ATt[i] <= CPU) && (ATt[i] != (LAT+10))){
if(PPt[i] != (MAX_P+1)){
P2 = PPt[i];
j= 1;
if(P2 < P1){
j= 1;
ATi = i;
P1 = PPt[i];
P2 = PPt[i];
}
}
}
}
if(j == -1){
CPU = CPU+1;
continue;
}else{
waittingTime[ATi] = CPU - ATt[ATi];
CPU = CPU + PT[ATi];
turnaroundTime[ATi] = CPU - ATt[ATi];
ATt[ATi] = LAT +10;
j = -1;
PPt[ATi] = MAX_P + 1;
ATi = 0; //Pointing to Arrival Time indix
P1 = MAX_P+1; //Pointing to 1st piriorty Value
P2 = MAX_P+1; //Pointing to 2nd piriorty Value
NoP = NoP - 1;
}
}
printf("\nPN\tPT\tPP\tAT\tWT\tTT\n\n");
for(i = 0; i < pn; i++){
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",i+1,PT[i],PP[i],AT[i],waittingTime[i],turnaroundTime[i]);
}
int AvgWT = 0;
int AVGTaT = 0;
for(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 PT PP AT WT TT
P1 3 2 0 0 3
P2 5 6 2 11 16
P3 4 3 1 2 6
P4 2 5 4 7 9
P5 9 7 6 12 21
P6 4 4 5 2 6
P7 10 10 7 20 30
PN PT PP AT WT TT
P1 4 2 0 0 4
P2 3 3 1 3 6
P3 1 4 2 5 6
P4 5 5 3 5 10
P5 2 5 4 9 11
*/```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.