please help

1
2
int pid_front = 0;
int pid_back = 0;


So this is code for process arriving:
1
2
3
case 'A':
       cout << "A process with PID " << ++pid_front << " has arrived!"<<endl<<endl;
       break;


So this is code for process dying:
1
2
3
4
5
6
7
8
case 't':
 if(pid_back < pid_front){
            cout << "A process with PID " << ++pid_back << " has died!"<<endl<<endl;
       }
	   else if (pid_back >= pid_front){
		    cout<<"Cannot terminate- CPU, and therefore Ready Queue, are empty!"<<endl<<endl;
	   }
       break; 


So, I want this table to be like
1
2
3
4
|   Position|   PID|
|          0|     1|
|          1|     2|
|          2|     3|



I'm trying to print out the table, but not working at all.
1
2
3
4
5
6
7
8
9
10
if (choice == 'r'){
	   	 int pos = 0;
	     cout<<"|       Position|        PID|"<<endl;
	     cout<<"RQ: ------------------------|"<<endl;
	     while (pid_front > pid_back){
	     cout<<"|              "<< pos++ <<"|          "<< pid_back<<"|"<<endl;
	     break;
	    }
	   }
}



So when it process arrives it should continue to add on to the list, but when the process dies, like if PID 2 dies, then pos 1 will be PID 3. How to do this? Any suggestions?
Last edited on
Anyone? Maybe I need a for loop? Please help!
I'd say that the best appoach would be a list. How do you determine which process dies?
can I do this?

1
2
3
list <int> pid;
pid.push_front;
pid.push_back;
Last edited on
I can use a queue right?

1
2
3
queue <int> pid;
pid.push_front = 0;
pid.push_back - 0;
how to make a queue which process increments when arriving and dying?
Last edited on
Wrap the queue in a class and have the class provide the arriving and dying functions.
1
2
3
4
5
6
7
8
 class Queue
{
private:
Node* front;
Node* rear;
public:
~Queue();
};


so I can have something like this in header file where Node* front is the arriving processes and Node*back is the dying processes?
Edit: When I wrote this, your immediately previous entry was not yet available.

I assume you are talking about a std::queue

http://www.cplusplus.com/reference/queue/queue/

If you want access to all of the elements, you probably don't want to use a queue. The std::queue only gives you access to the first and last elements in the queue. To get access to all of them, you probably want to use another structure like a deque. Then you can iterate through all of the elements.

http://www.cplusplus.com/reference/deque/deque/

Whatever standard class you use, you don't need to worry about the implementation details like you seem to be trying to do in lines 2 and 3 of you second-to-last post. You only need to worry about the member functions to add and remove elements.
Last edited on
How about I enqueue and dequeue to access all the elements?
So I need to use FIFO queue.
the arriving processes will increment from 1,2,3,.....
the dying processes will increment from 1,2,3,...... upto the number of arriving processes.
but at the same time I need to keep track track of them to print the queue like in a table format.


1
2
3
4
|   Position|   PID|
|          0|     1|
|          1|     2|
|          2|     3|

Last edited on
You seem to be a bit scattered. You started with two integers without context, and then you asked about a list template class (I assume std::list), and then you asked about a queue template class (I assume std::queue), and then you ask about your home-brewed Queue (possibly template class), and finally ask about two member functions which don't exist in any of those classes.

In order to use enqueue and dequeue to access all of the elements, you would need 2 queues. Dequeue from 1 queue, access the element, and then enqueue on the other queue. That seems like too much work.

You need to figure out what it is you really want to do, and how much you want to write yourself vs. how much to utilize the standard library.

It sounds like you need a class that does the following:
1. Add to the back
2. Remove from the front
3. Iterate through all of the elements in the container
4. Process arrivals and deaths.

I would write a class the has member functions arrive() and die(). My class would contain a deque to handle the data. The deque can easily handle 1 - 3, and my wrapper class can handle 4. My class could also be a template class and pass the template argument down to the contained deque.
hmmmm when arriving and dying its from the front.
so my teacher stated to use a queue since its FIFO.

queue <int> pid;

1
2
3
4
switch(input){
      case 'A':
           cout << "A process with PID " << pid.push_front() << " has arrived!"<<endl<<endl;
       break;


but I'm not sure how the value in a queue will increment?
So I tried this and it doesn't seem to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 while (true){
  	 queue <int> ready_queue;
     int pid_count = 0;
	 char function;
  	 int number,num,size, location;
  	 string filename;
  	 char select;
	 cout<<">>> ";
     cin >>input;
     switch(input){
      case 'A':
           cout << "A process with PID " << ready_queue.push(pid_count) << " has arrived!"<<endl<<endl;
           pid_count ++;
       break;


You spend a lot of time throwing faulty code snippets onto the forum and stating it doesn't work. It would help if we knew what the actual assignment is and to see your entire attempt at a solution. It the code doesn't compile, tell us why (give us the error messages) and we can try to get you through the mess.

Is the queue template class instantiated in line 2 the queue from the standard template library (STL) or a template class of your own design?

Assuming that it is of the STL, push() does not return a value. So line 12 has nothing to print after "PID". If you are writing your own, then I need to see your code for that template class.

With the STL's std::queue, you can push or pop an element, see what the first and last elements are, and get the number of elements currently in the queue. You are not able to easily iterate through the queue and look at each element it contains. Are you sure you are required to print out the contents of the queue? If so, you might really try looking into deque, which stands for "double ended queue". A deque can be used to implement either a queue or a stack.

By the way, since your while loop starts at line 1, ready_queue and pid_count (as well as the other variables) get re-initialized each time through the loop. So each time through you have a brand new queue and a count of 0.
I am still so lost. I know I need to keep track of the arrival and dying processes some way.

Last edited on
Topic archived. No new replies allowed.