ok, i need help with changing the state of a process. So far my struct Process has an integer which contains the priority and a pointer next which points to another process, and i need to add a state so that every time i call the function insert() it would change its state to READY and display it, and also every time i call the function remove() it should change its state to RUNNING. any help on doing this? is there an easy way?

this is some part of my header and class file

struct Process
{
  int id;
  Process *next;
};
void LL::insert(el_t NewNum)
{

  if(rear == NULL)//CASE:1(if rear is null)                                        
    {
      front = rear = new Process;
      rear->id = NewNum;
    }
  else//CASE:2(if the list is not empty)                                           
    {
      rear->next = new Process;
      rear = rear->next;
      rear->id = NewNum;
      rear->next = NULL;
    }


  count++;            
  sort();                                 
}
void LL::displayQueue()
{
  if(isEmpty())
    {
      cout <<"queue is empty";
    }
  else
    {
  PCB *x = front;
  while(x!= NULL)
    {
      cout << x->id << "\t";
      x = x->next;
    }
  cout << endl;
    }
}

void LL::remove(el_t& OldNum)
{

  if(isEmpty())//CASE:1(if the list is empty)                                      
    {
      cout <<"Cant remove from an empty list";
    }

  else//CASE:2(if the list is not empty)                                           
    {
      PCB *second;
      second = front->next;
      delete front;
      front = second;
    }
  count--;

}

Recommended Answers

All 3 Replies

Just convert

struct Process
{
  int id;
  Process *next;
};

to

struct Process
{
  int id;
  state_t state;
  Process *next;
};

Where state_t is defined as

enum state_t { READY, RUNNING , ... };

Of course, you will need to fill that enumeration with as many states as you would like to support.

Just convert

struct Process
{
  int id;
  Process *next;
};

to

struct Process
{
  int id;
  state_t state;
  Process *next;
};

Where state_t is defined as

enum state_t { READY, RUNNING , ... };

Of course, you will need to fill that enumeration with as many states as you would like to support.

ok thanks, but what would i have to do so it can actually display the state for example READY instead of the index?

When I need to output the name of an enum member, I usually write something like the following:

const char * state2str (state_t state) {
   switch state {
      case READY: return "ready";
      case RUNNING: return "running";
      ...
      default: return "undefined state";
   }
   return "switch failed";
}

An alternative is to have a global array of strings representing the enum. However, maintenance on a global array is more delicate as order must be preserved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.