Question

I will start by explaining my issue, and then provide a good portion of the code. I and filling a queue with a custom class called process. Using this line:

ProcessQueue.push(new Process(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]));

This all seems well and good, when the loop is done Process queue is filled with pointers. However upon further inspection I found out they all point to the SAME object?

Finding this curious, I stepped into the constructor on each iteration of the loop.

First iteration: when the constructor is entered all instance variables are null (as expected)

Second iteration: upon entering the constructor, all instance variables contain the values given to the object in the first iteration (ie: the same object)

Furthermore later when I use the queue I have confirmed that every pointer in the queue is referring to the same process object. (I can tell this because process contains a state and if loop through the queue changing the state, I will find the state already changed for the second pointer)

I suspect I must have done something wrong with the creation of me class. so here it is in its entirety.

Process.h

    #pragma once
    class Process
    {
    public:
        Process(int _processId, int _arrivalTime, int _CPUTime,
            int _IOFrequency, int _IODuration);
        ~Process();

        bool HasArrived(int time);
        bool HasCompleted();
        bool HasFinishedBurst();
        bool HasFinishedIO();
        int GetQueueNum();
        int GetID();
        void SetQueueNum(int i);
        void SetToReady();
        void Run();
        void PerformIO();
    };

Process.cpp

    #include "stdafx.h"
    #include "Process.h"
    #include <string>

    using namespace std;

    int processId;
    int arrivalTime;
    int CPUTime;
    int IOFrequency;
    int IODuration;

    int Ticks;
    int CPUConsumption;
    int CPUBurstSize;
    int queueNumber;
    int IOBurstCount;
    string state;


    Process::Process(int _processId, int _arrivalTime, int _CPUTime,
        int _IOFrequency, int _IODuration)
    {
        processId = _processId;
        arrivalTime = _arrivalTime;
        CPUTime = _CPUTime;
        IOFrequency = _IOFrequency;
        IODuration = _IODuration;
        IOBurstCount = 0;
        CPUConsumption = 0;
        Ticks = 0;
        queueNumber = 0;

        state = "None";
        printf("%d: %s\n", processId,state.c_str());

        int excess = CPUTime % IOFrequency;
        if (excess == 0)
        {
            CPUBurstSize = CPUTime / IOFrequency;
        }
        else
        {
            CPUBurstSize = (CPUTime - excess) / (IOFrequency - 1);
        }
    }

    Process::~Process()
    {
    }

    bool Process::HasArrived(int time)
    {
        if (arrivalTime <= time)
        {
            if (state.compare("Newly Arrived") == 0)
            {
                printf("Already arrived!\n");
            }
            state = "Newly Arrived";
            printf("%d: %s\n", processId, state.c_str());
            return true;
        }
        else
        {
            return false;
        }

    }

    bool Process::HasCompleted()
    {
        if (CPUConsumption == CPUTime && IOBurstCount == IOFrequency)
        {
            state = "Finished";
            printf("%d: %s\n", processId, state.c_str());
            return true;
        }
        else
        {
            return false;
        }
    }

    bool Process::HasFinishedBurst()
    {
        if (Ticks == CPUBurstSize)
        {
            Ticks = 0;
            state = "Blocked";
            printf("%d: %s\n", processId, state.c_str());
            return true;
        }
        else
        {
            return false;
        }
    }

    bool Process::HasFinishedIO()
    {
        if (Ticks >= IODuration)
        {
            IOBurstCount++;
            Ticks = 0;
            return true;
        }
        else
        {
            return false;
        }
    }

    void Process::SetToReady()
    {
        state = "Ready";
        printf("%d: %s\n", processId, state.c_str());
    }

    void Process::Run()
    {
        state = "Running";
        printf("%d: %s\n", processId, state.c_str());
        Ticks++;
        CPUConsumption++;
    }

    void Process::PerformIO()
    {
        Ticks++;
    }

    int Process::GetQueueNum()
    {
        return queueNumber;
    }

    void Process::SetQueueNum(int i)
    {
        queueNumber = i;
    }

    int Process::GetID()
    {
        return processId;
    }

I suspect I have somehow created this as a static class without meaning too...

Was it helpful?

Solution

It seems you have placed all your member variables outside the class!

int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;

int Ticks;
int CPUConsumption;
int CPUBurstSize;
int queueNumber;
int IOBurstCount;
string state;

Should be here:

class Process
{
public:
    Process(int _processId, int _arrivalTime, int _CPUTime,
        int _IOFrequency, int _IODuration);
    ~Process();

    bool HasArrived(int time);
    bool HasCompleted();
    bool HasFinishedBurst();
    bool HasFinishedIO();
    int GetQueueNum();
    int GetID();
    void SetQueueNum(int i);
    void SetToReady();
    void Run();
    void PerformIO();

private:
   int processId;
   int arrivalTime;
   int CPUTime;
   int IOFrequency;
   int IODuration;

   int Ticks;
   int CPUConsumption;
   int CPUBurstSize;
   int queueNumber;
   int IOBurstCount;
   string state;
};

OTHER TIPS

You defined global variables

int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;

and the constructor of class Process each time when it is called overrides their values.

Process::Process(int _processId, int _arrivalTime, int _CPUTime,
    int _IOFrequency, int _IODuration)
{
    processId = _processId;
    arrivalTime = _arrivalTime;
    CPUTime = _CPUTime;
    IOFrequency = _IOFrequency;
    IODuration = _IODuration;
    IOBurstCount = 0;
    CPUConsumption = 0;
    Ticks = 0;
    queueNumber = 0;
    //...

While the class itself has no data members.

So these global variables keep that values that were assigned to them in the last call of the constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top