1
votes

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
     

*/```
1
Just as a side note: It is unsafe to use scanf without checking the return value. See this page for further information: A beginners' guide away from scanf()Andreas Wenzel
Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: What is a debugger and how can it help me diagnose problems? You may also want to read this: How to debug small programs?.Andreas Wenzel
I have been debugging it for almost 4 weeks, but finally I got the answer today.ABDELRAHMAN NASSAR

1 Answers

0
votes

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

*/```