Quick Basic Question -- STL

I am a newbie that is currently working on a project to evaluate different operating systems algorithms.

I have a class called Process that holds all the data for the process. I began to write my own Linked List for the processes to make linked list of processes but ran into a problem when trying to remove the bottom Process of one list Ready Queue), run it in my CPU function, then add it to another list (Input/Output Queue). So I decided to use the STL list class instead.

I am able to create a linked list of processes using the STL list and access the data inside the processes. But, I need to further work with the lists. Here is my goal:

-Fill ready queue with processes
-remove first item from ready queue
-"run" the process in a function until the process' int data value is 0 (this simulates the CPU burst time of the process)
-after it is through in hte CPU function, add that process to the top of hte Ready Queue
-decrement all processes in the Ready Queue for each iteration in hte CPU function
-repeat till all process' times are 0

The only issue I am having is that I do not know how to pass the list of processes to the funtion in order to work on it. I can pass individual processes to the functions using their pointers

I would rather not have a huge main funtion with all of hte iterations in there. I just want to break it up so that it is more manageable.

Some of the basic code:


int main(int argc, char* argv[])
{

list<Process> ReadyQ;
list<Process> IOQ;

Process P1(1);
Process P2(2);
Process P3(3);
Process P4(4);

P1.fill_CPU(7,9,10,12,10,13,11,-1);
P1.fill_IO(22,26,17,36,29,20,-1,-1);
P2.fill_CPU(9,11,7,8,13,10,12,-1);
P2.fill_IO(35,36,27,50,28,25,-1,-1);
P3.fill_CPU(8,7,5,6,9,12,9,-1);
P3.fill_IO(22,20,21,20,18,19,-1,-1);
P4.fill_CPU(17,24,25,27,19,18,-1,-1);
P4.fill_IO(23,19,16,14,18,-1,-1,-1);


ReadyQ.push_back(P1);
ReadyQ.push_back(P2);
ReadyQ.push_back(P3);
ReadyQ.push_back(P4);

dmigaAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
You can pass STL lists like regular arguments - I'd suggest a reference to the list, though, e.g.

void SampleFunc ( list<Process>& lst) {
}

and call it like

SampleFunc ( ReadyQ);
0
 
dmigaAuthor Commented:
Thank you for answering my question!

If I may, could I expand on my question?

Once the lists are passed to the function, what is the best way to iterate through and decrement a counter that is in each Process in the list?

For instance, in my Process class I have two simple stacks that can be accessed by their indexes:

int CurrentIO_burst;
int CurrentReady_burst;
IntStack CPU_burst;
IntStack IO_burst;

Using the linked list class that I wrote, I decremented the bursts by:

void List::decrement_all_IObursts()
{
      Process * cur;
      cur = top;
      while (cur!=NULL)
      {
            int i = (*cur).currentIO;
            (* cur).IO_burst.data[i] --;
            cur = (*cur).pNext;            
      }
}

When I access this data after passing to the function, as you described, what will need to be done?

I found the following code somewhere but am unsure if this is the correct approach and how to correctly write the function:

list<Process>::const_iterator it;
for(it=IOQ.begin(); it!=IOQ.end(); ++it)
{
//somehow decrement (* cur).IO_burst.data[i] --;
}
      
Thank you in advance, you guys are great!
0
 
jkrCommented:
>>what is the best way to iterate through and decrement a
>>counter that is in each Process in the list?

Use the iterators the list provides :o)

>>I found the following code somewhere but am unsure if this is the correct approach

It definitely is the correct approach - just use

list<Process>::const_iterator it;
for(it=IOQ.begin(); it!=IOQ.end(); ++it)
{
        int i = (*it).currentIO;

        (*it).IO_burst.data[i] --;
}



0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
dmigaAuthor Commented:
jkr

My apologies for asking another question but since this is the first time I have adapted use of hte STL calss, I'm hung up.

I used the code

 void decrement_IO_bursts(list<Process>& IO)
{
      list<Process>::const_iterator it;
      for(it=IO.begin(); it!=IO.end(); ++it)
      {
      int i = (*it).currentIO;
--->      (*it).IO_burst.data[i] --;
      }
}


 and am getting the error during compile pointing to the line --->:

 error C2166: l-value specifies const object

I know you have better things to do besides hold the hand of a newbie but I caould really use any suggestions you may have.
0
 
jkrCommented:
Ooops, missed that - use

list<Process>::iterator it; // <---!!!
for(it=IOQ.begin(); it!=IOQ.end(); ++it)
{
       int i = (*it).currentIO;

       (*it).IO_burst.data[i] --;
}

The "const" with the iterator implies that you cannot change the object it references.
0
 
bacchus7Commented:
Thank you very much!

I do appreciate it.
0
 
jkrCommented:
???
0
 
bacchus7Commented:
I don't understand --> ???
0
 
jkrCommented:
This Q has been asked by dmiga, so *I* don't understand why bacchus7 is expressing appreciation...
0
 
bacchus7Commented:
My apologies, I know you answer many questions.

I needed to ask another question last night so I set up a second account until I decide whether I want to purchase points or subscribe.

I just wanted to extend a thank you for assisting me. Many of hte concepts and sloutions that you folks have are not discudded in school or difficult to find in books.

Thank you again, you are GREAT!

dmiga
bacchus7
0
 
jkrCommented:
>> needed to ask another question last night so I set up a second account

Multiple accounts are forbidden on EE. Sorry, but you better ask customer service to remove one of them before *they* do that...
0
 
bacchus7Commented:
Did not know that, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.