0

I'm writing an Object Oriented version of FCFS scheduling algorithm, and I've hit a problem. I need to know if there's any way to access an array of objects inside the member function definition, without passing it as a parameter explicitly.

I've tried using "this-pointer", but since the calculation of finish time of current process requires the finish time of the previous, "this" won't work. Or at least I think it won't. I have no idea how to access "previous" object using "this"

void Process :: scheduleProcess(int pid) {
     if(pid == 0) finishTime = burstTime;
     else finishTime = burstTime + 
     this->[pid-1].finishTime;

     turnAroundTime = finishTime - arrivalTime;
     waitingTime = turnAroundTime - burstTime;
}

I can obviously send the array of objects as a parameter and use it directly. I just want to know if there's a better way to do this:

This is the part that's calling the aforementioned function:

 for(int clockTime = 0; clockTime <= maxArrivalTime(process); 
 clockTime++) {
    // If clockTime occurs in arrivalTime, return pid of that 
 process
         int pid = arrivalTimeOf(clockTime, process);
         if(pid >= 0) {            
             process[pid].scheduleProcess(pid);

         } else continue;
 }

Since I'm calling scheduleProcess() using process[pid], which is a vector of objects, I should be able to manipulate the variables pertaining to process[pid] object. How do I access process[pid-1] in the function itself? (Without passing process vector as an argument)

3
  • 1
    Post the definition of Process and a minimal reproducible example. Commented Aug 14, 2019 at 12:45
  • ` enum status { NOT_ARRIVED, ARRIVED, EXECUTING, FINISHED }; struct Process { int id; status stat; float arrivalTime; float burstTime; float finishTime; float turnAroundTime; float waitingTime; //void queueProcess(); void scheduleProcess(int); }; ` Commented Aug 14, 2019 at 13:04
  • Please edit the additional code into the question body. Commented Aug 14, 2019 at 21:37

1 Answer 1

1

Since scheduleProcess is a member of Process, it only knows what the Process object knows. The previous process is unknown at this level. There are ways that use Undefined Behavior and make more assumptions about your code to get around this, but these should be avoided.

One portable solution to avoid all that is to simply pass in the previous process's finish time as a parameter, since you know this value at the point of the call to scheduleProcess. Where there is not a previous process (the first entry in the array), this finish time would be 0.

Sign up to request clarification or add additional context in comments.

5 Comments

This also has the benefit of simplifying the calculation finishTime = burstTime + prevFinishTime
Literally the same thing I wrote :D
Or could you just pass a pointer to the previous process (null if there ain't one)?
@Adrian You could, but passing the previous time is (IMHO) cleaner and more generic, as the time could potentially come from something other than a process.
Or, as you presumably have to store the array of objects (processes) somewhere anyway, why not make that array a static member of the class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.