Thread: Non-Preemptive Priority Scheduling Problem!

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    Question Non-Preemptive Priority Scheduling Problem!

    input file text :

    Code:
    6
    1 0 yellow
    2 1 red
    3 1 green
    4 2 urgent
    5 3 green
    6 4 red


    my program until now:

    Code:
    #include <iostream>
    #include <fstream> 
    #include <string>
    #include <sstream> 
    #include <vector>
    #include <time.h>       /* time */
    #include <stdlib.h>     /* srand, rand */      
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    
    struct  process
    {
       public:
          int pid;  
          int arrival;  
          int burst;  
          int priority; 
          int status; 
    };
    
    bool sortbyarrivel(const process & s1, const process & s2)
    {
       if (s1.arrival != s2.arrival) 
       return s1.arrival < s2.arrival;
    }
    bool sortbypriority(const process & s1, const process & s2)
    {
       if (s1.priority == s2.priority) {
        if (s1.arrival > s2.arrival ){
            return s1.priority < s2.priority;
        }
       }
       else if (s1.priority != s2.priority) {
        return s1.priority < s2.priority;
       }
    
    }
    
    int main() {
    
        process prs;
        vector <process> pr;
        vector <process> r1;
    
        ifstream infile("inputfile.txt");  // ifstream is used to read the input file
        string line,btime,numofprocesses,prid,arraiveltime;
        int n;
    
    
    
        int current_line = 0;  
        int randNum;
    
        while (getline(infile, line)) //get the lines inside the file, one by one until the end of the file
        {
            istringstream iss(line); //create string stream and read current line
            if ( current_line ==0){ // first line always contains the number of processes
                if (iss >> numofprocesses){ // store the contents of the stream into numofprocesses
                n =atoi(numofprocesses.c_str());
                        cout<< "number of processes = "<<numofprocesses<<endl<<endl;    
                }
            }
            else {
                if (iss >> prid >>arraiveltime >>btime) { // store the contents of the stream into pid,arraiveltime 
    
                prs.pid = atoi(prid.c_str());
                prs.arrival = atoi(arraiveltime.c_str());
    
                if (btime == "green"){ // if btime , which is the priority type is == regular 
                    //srand (time(NULL)); 
                    //prs.burst = (rand()%(15-10))+ 10; // give this process a random burst time from 10 to 15
                    prs.burst = 1;
                    prs.priority = 2; // give this process priority = 2 if its regular
    
                }
                else if(btime == "yellow"){ 
                //  srand (time(NULL));
                    //prs.burst = (rand()%(30-20))+ 20;
                    prs.burst = 2;
                    prs.priority = 1;
    
    
                }
                else if(btime == "red"){ 
                //  srand (time(NULL));
                    //prs.burst = (rand()%(60-40))+ 40;
                        prs.burst = 3;
                    prs.priority = 0;
    
                }
                prs.status =0;
                pr.push_back(prs);
    
                }
            }
              current_line++;              // next line ++1
        }
    
    
        //sorting by arrivel time
        sort(pr.begin(), pr.end(), sortbyarrivel);
    
    
        cout<<endl; cout<<endl; cout<<endl;
        cout<<"The patients are scheduled as follow."<<endl;
        cout<<"In physician room:"<<endl;
        cout<<"Time "<< pr[0].arrival<<" : patient "<<pr[0].pid<<" is coming"<<endl;
        cout<<"Time "<< pr[0].burst<<" : patient "<<pr[0].pid<<" is leaving"<<endl;
        static int currentbursttime=pr[0].burst;
        pr[0].status =1;
    
    
    int count = 0 ;
        int complated_process = 0 ;
    
        while (complated_process <pr.size()){
    
            for(int i=0;i<pr.size();i++){
                if( pr[i].arrival <= currentbursttime &&  pr[i].status ==0 ){
                    r1.push_back(pr[i]);
                    count++;
                }
            }
            //sorting by priority 
            sort(r1.begin(), r1.end(), sortbypriority);
    
    
            for(int i=0;i<count;i++){
                if (i == 0){
                    currentbursttime = currentbursttime+1;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is entring"<<endl;
                    currentbursttime =r1[i].burst+currentbursttime;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is going"<<endl;
                    pr[r1[i].pid].status =1;
                    complated_process = complated_process +1;
                }           
            }
    
            r1.clear(); 
    
    
        }
    return 0 ;
    }

    this program until now is giving me this results:
    Code:
    number of processes = 6
    
    The patients are scheduled as follow.
    In physician room:
    Time 0 : patient 1 is coming
    Time 2 : patient 1 is leaving
    At Time 3 : process 2 is entring
    At Time 6 : process 2 is going
    At Time 7 : process 6 is entring
    At Time 10 : process 6 is going
    At Time 11 : process 6 is entring
    At Time 14 : process 6 is going
    At Time 15 : process 6 is entring
    At Time 18 : process 6 is going
    At Time 19 : process 6 is entring
    At Time 22 : process 6 is going
    At Time 23 : process 6 is entring
    At Time 26 : process 6 is going
    and the result is wrong , its repeating process #6 over and over again .. the result I want is something like this:

    Code:
    At Time 0 : process 1 is entring
    At Time 2 : process 1 is going
    At Time 3 : process 2 is entring
    At Time 6 : process 2 is going
    At Time 7 : process 6 is entring
    At Time 10 : process 6 is going
    At Time 11 : process 4 is entring
    At Time 13: process 4 is going
    At Time 14: process 3 is entring
    At Time 15: process 3 is going
    At Time 16: process 5 is entring
    At Time 17 : process 5 is going

    The problem has to do with the loops inside while loop. Tried to find a solution but I couldn't.
    Hope someone can look at this and show me what I did wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Consider these warnings.
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:118:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    foo.cpp:120:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    foo.cpp:49:7: warning: variable ‘n’ set but not used [-Wunused-but-set-variable]
    foo.cpp:54:7: warning: unused variable ‘randNum’ [-Wunused-variable]
    foo.cpp: In function ‘bool sortbypriority(const process&, const process&)’:
    foo.cpp:39:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.cpp: In function ‘bool sortbyarrivel(const process&, const process&)’:
    foo.cpp:27:1: warning: control reaches end of non-void function [-Wreturn-type]
    Consider, for example, how a sort function is going to behave when the comparison function is potentially returning garbage, because "control reaches end of non-void function"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by rakk92 View Post

    Code:
    pr[r1[i].pid].status =1;
    Above will eventually try to access an index that's not present. Remember indices start from 0 and your processes start with pid = 1

    My understanding is that the only reason you're sorting the processes by arrival time is to get the first process to execute in which case you need to make a copy of the processes instead and keep the original vector instance to be able to do things like:
    Code:
    pr[r1[i].pid].status =1;
    If you sort the processes by arrival time the above will not address the process with the desired pid. Initially the processes sorted by the process id _but_ they are not currently sorted by the process id which mean you cannot use the process id as an index.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You don't need this loop
    Code:
       for(int i=0;i<count;i++){
                if (i == 0){
                    currentbursttime = currentbursttime+1;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is entring"<<endl;
                    currentbursttime =r1[i].burst+currentbursttime;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is going"<<endl;
                    pr[r1[i].pid].status =1;
                    complated_process = complated_process +1;
                }           
            }
    I assume you want to execute the first process in the queue which should be just

    Code:
                   currentbursttime = currentbursttime+1;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is entring"<<endl;
                    currentbursttime =r1[0].burst+currentbursttime;
                    cout<<"At Time "<< currentbursttime<<" : process "<<r1[0].pid<<" is going"<<endl;
                    pr[r1[0].pid].status =1;
                    complated_process = complated_process +1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Priority Queues C problem
    By Cprogrammes in forum C Programming
    Replies: 11
    Last Post: 01-04-2014, 12:24 PM
  2. Problem with Priority Non-Preemptive Scheduling
    By primeG in forum C Programming
    Replies: 1
    Last Post: 03-13-2013, 06:31 AM
  3. SJF Scheduling Problem
    By IT_ in forum C Programming
    Replies: 12
    Last Post: 12-08-2011, 10:40 AM
  4. Exam Scheduling Problem
    By itsacezon in forum C Programming
    Replies: 4
    Last Post: 10-19-2011, 09:52 AM