Solved

Quick Basic Question -- STL

Posted on 2003-11-01
12
491 Views
Last Modified: 2013-12-14
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);

0
Comment
Question by:dmiga
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9663883
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
 

Author Comment

by:dmiga
ID: 9664087
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
 
LVL 86

Expert Comment

by:jkr
ID: 9664224
>>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
 

Author Comment

by:dmiga
ID: 9664281
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
 
LVL 86

Expert Comment

by:jkr
ID: 9664783
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
 

Expert Comment

by:bacchus7
ID: 9666670
Thank you very much!

I do appreciate it.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

by:jkr
ID: 9666696
???
0
 

Expert Comment

by:bacchus7
ID: 9666744
I don't understand --> ???
0
 
LVL 86

Expert Comment

by:jkr
ID: 9666750
This Q has been asked by dmiga, so *I* don't understand why bacchus7 is expressing appreciation...
0
 

Expert Comment

by:bacchus7
ID: 9666759
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
 
LVL 86

Expert Comment

by:jkr
ID: 9666774
>> 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
 

Expert Comment

by:bacchus7
ID: 9666792
Did not know that, thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now