Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Quick Basic Question -- STL

Posted on 2003-11-01
12
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

670 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