Thread: Queue While Loop Help

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Queue While Loop Help

    I changed my while loop but Im getting the wrong output. In my while loop for the else statement I want it to perform the subtraction operation and then put that data on the queue pq2. I put a cout statement to make sure that the data is on the pq2 queue. However Im getting weird numbers.

    This is my output:
    PID: 3 Initial Priority: 32 Service Time: 0.52
    Process is done
    PID: 45 Initial Priority: 12 Service Time: 1.1
    Process is done
    PID: 0 Initial Priority: 1 Service Time: 5.51
    Changed Quantum: 1.01
    PID: -1073741821Priority: -1073741821 Changed Service Time: 2.8026e-45
    PID: 1 Initial Priority: 1 Service Time: 6
    Changed Quantum: 1.5
    PID: -1073741821Priority: -1073741821 Changed Service Time: 2.8026e-45


    The output should be:
    PID: 3 Initial Priority: 32 Service Time: 0.52
    Process is done
    PID: 45 Initial Priority: 12 Service Time: 1.1
    Process is done
    PID: 0 Initial Priority: 1 Service Time: 5.51
    Changed Quantum: 1.01
    PID: 0 Priority: 1 Changed Service Time: 1.01
    PID: 1 Initial Priority: 1 Service Time: 6
    Changed Quantum: 1.5
    PID: 1 Priority: 1 Changed Service Time: 1.5

    This is what I have for my main:
    Code:
    int main(){
    priority_queue<Event, vector<Event>, ComparePriority> pq;
    
    	float quantum = 4.5;
    
    Event events[4]= { {0, 1, 5.51}, {1, 1, 6.00}, {45, 12, 1.10}, {3, 32, 0.52}};
    
    for (int i = 0; i < 4; ++i)
    	pq.push(events[i]);
    	
    while (! pq.empty()) {
    	Event events2=pq.top();
    	cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    	if (events2.service<quantum){
    	cout<<"Process is done"<<endl;
    		//pq.pop();
    	}else{
    		events2.service-=quantum;
    		cout<<"Changed Quantum: "<<events2.service<<endl;
    		priority_queue<Event, vector<Event>, ComparePriority> pq2;
    		Event events2=pq2.top();
    		cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
    		//cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    //put at the end of the line to go back through the loop next cycle
    	}
    	pq.pop();
    }
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you declare a brand new queue (pq2), ask for the top element of it, and then expect that top element to be meaningful? Why?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bananasplit View Post
    In my while loop for the else statement I want it to perform the subtraction operation and then put that data on the queue pq2.
    That is not what pop() does.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I know that push puts it onto the queue and pop removes it. I tired to use the push but I did not know how I would declare push(); so that it pushes the line of data onto the new queue pq2.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would it be different than
    Code:
    pq.push(events[i]);
    which you already have? (Other than the name "events[i]", of course.)

    Also, you do realize that your queue pq2 would exist for approximately 0.00001 seconds, since once you hit the close curly brace of the else, that local variable would cease to exist?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I had that at first but I changed it because I didnt not how to declare the value on the inside. I declared it as pq.push(events2.pid, events2.priority, events2.service). How would I declare the value on the inside? I was not aware of this. Should I declare it when I declare the other queue to fix this problem?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not
    Code:
    pq.push(events2);
    since that is what you want to push onto the queue?

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    thank you, after I tried to implement the second part of the while loop and Im not getting the correct input. What I want is for after the subtraction it is added onto the second queue. The loop continues to run and the data is either popped off after the comparison or it is added onto the second queue. After all the data is popped off the first queue. It does the same thing with the second queue, instead this time putting the data back on the first queue. Basically it is flip-flopping data between two queues. Can you show me where in this while loop Im going wrong.

    This is my while loop in main:
    Code:
    while (! pq.empty()) {
    	while(! pq2.empty()){
    	Event events2=pq.top();
    	cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    	if (events2.service<quantum){
    	cout<<"Process is done"<<endl;
    		//pq.pop();
    	}else{
    		events2.service-=quantum;
    		cout<<"Changed Quantum: "<<events2.service<<endl;
    		pq2.push(events2);
    		cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
    		//cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    //put at the end of the line to go back through the loop next cycle
    	}
    	pq.pop();
    }
    		Event events2=pq2.top();
    		cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    		if (events2.service<quantum){
    			cout<<"Process is done"<<endl;
    			//pq.pop();
    		}else{
    			events2.service-=quantum;
    			cout<<"Changed Quantum: "<<events2.service<<endl;
    			pq.push(events2);
    			cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
    			
    }
    	pq.pop();
    }
    This is my output
    PID: 0 Initial Priority: 0 Service Time: 0
    Process is done
    PID: 0 Initial Priority: 0 Service Time: 0
    Process is done
    PID: 0 Initial Priority: 0 Service Time: 0
    Process is done
    PID: 0 Initial Priority: 0 Service Time: 0
    Process is done

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It may not hurt, just for a start, to actually indent the code the way the code actually works.
    Code:
    while (! pq.empty()) {
        while(! pq2.empty()){
            Event events2=pq.top();
            cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
            if (events2.service<quantum){
                cout<<"Process is done"<<endl;
                //pq.pop();
            }else{
                events2.service-=quantum;
                cout<<"Changed Quantum: "<<events2.service<<endl;
                pq2.push(events2);
                cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
                //cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
                //put at the end of the line to go back through the loop next cycle
            }
            pq.pop();
        }
        Event events2=pq2.top();
        cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
        if (events2.service<quantum){
            cout<<"Process is done"<<endl;
            //pq.pop();
        }else{
            events2.service-=quantum;
            cout<<"Changed Quantum: "<<events2.service<<endl;
            pq.push(events2);
            cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
        }
        pq.pop();
    }
    You can see, just for a start, that "Event events2=pq2.top();" is a big pile of trouble, as the only way to get to that line is for pq2 to be empty.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I see what u mean, so I changed it so the main while loop is while pq and pq2 are not empty that you go through the loop. So then the inner loop is while pq is not empty you go through the while loop. When that is empty I have it still is in the while loop because now pq2 is not empty. So then it goes to the next inner while loop which is while pq2 is not empty. This time it does nothing, meaning it is not going through the while loop and I dont know why.

    Here is the changed while loop:
    Code:
    while(! pq.empty() && ! pq2.empty()){
    	while(! pq.empty()){
    	Event events2=pq.top();
    	cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    	if (events2.service<quantum){
    	cout<<"Process is done"<<endl;
    		//pq.pop();
    	}else{
    		events2.service-=quantum;
    		cout<<"Changed Quantum: "<<events2.service<<endl;
    		pq2.push(events2);
    		cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
    		//cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    //put at the end of the line to go back through the loop next cycle
    	}
    	pq.pop();
    }
    	while (! pq2.empty()){
    		Event events2=pq2.top();
    		cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
    		if (events2.service<quantum){
    			cout<<"Process is done"<<endl;
    			//pq.pop();
    		}else{
    			events2.service-=quantum;
    			cout<<"Changed Quantum: "<<events2.service<<endl;
    			pq.push(events2);
    			cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
    			
    }
    	pq.pop();
    }
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Doesn't your pq2 queue start out empty? Therefore your while test will fail, and nothing will happen. (Maybe you want while one OR the other is not empty.)

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Im sorry thats what I meant to type. But my input is still wrong and I am getting a segmentation fault. I dont know why? Any suggestions what to fix. I would like to thank you so much. I definitely appreciate it. So far its been little things that Im not seeing.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be popping from pq2 in your second loop, instead of pq.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks, I just copied the loop since its the same and forgot to change it. My next question is one of the things I have to account for is waiting time. Right now the data is hard coded but eventually it will the data will be read from a file, but waiting time will not be in the file. My question is can I add a waiting time category when each line of the file is read into the queue. Also when I read into a queue is this how I would implement it as:

    #include <fstream.h>
    ifstream OpenFile("file.dat")
    while(!OpenFile.eof())
    {
    pq.push(events[i]);
    }
    OpenFile.close();

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the waiting time does not appear in the file, then you'll have to make one up (presumably you know the mechanism for doing so). You'll have to push an Event object onto your queue; the usual mechanism is to create one inside the loop that reads your data. Something like
    Code:
    while (Still_More_Data) {
        //read into variables
        //make up waiting time
        Event temp(PID, priority, time);
        pq.push(temp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM