Wednesday, April 23, 2008

C language: Process Control Block Queue

This is a basic program that consists of a Process Control Block (PCB), and a Queue (which is a vector). I won't display the whole program, but I will show the structure. First, the PCB class definition. I only need a header file for the PCB class, because the class only needs variables, not methods:


// PCB.h

#ifndef PCB_H
#define PCB_H

Class *PCB {
int ID;
string STATE;
};

#endif


Now, here is the ReadyQ class, that uses a vector. The ReadyQ class's methods manipulate the PCB. I will only list the class source file, and not bother displaying the header:

//*************************************************************************************
// Name: Queue.C
// By: Phillip McCubbin
// Description:
// This file contains the implementation of Queue class which is
// used to store the
data in the order of first in first out (FIFO)
//*************************************************************************************

#include
#include"PCB.h"
#include "ReadyQ.h"

using namespace std;

//*************************************************************************************
// Method: Queue::Queue
// Description:
// Constructor. Initializes the data members to 0 and allocates
// memory to storage.

// No arguments, no returns
//*************************************************************************************
ReadyQ::ReadyQ() {
*PCB = new int
}

//*************************************************************************************
// Method: Queue::~Queue
// Destructor
//*************************************************************************************
ReadyQ::~ReadyQ(){
m_data.clear();
}

//*************************************************************************************
// Method: Queue::isEmpty
// Description:
// Check if the queue is empty
// Returns:
// true - if the queue is empty
// false - otherwise
//*************************************************************************************
bool ReadyQ::isEmpty(){
return m_data.empty();
}

//*************************************************************************************
// Method: Queue::size
// Description:
// Returns the size of the queue
// Returns:
// m_total - size of the queue
//*************************************************************************************
int ReadyQ::size() {
return m_data.size();
}

//*************************************************************************************
// Method: Queue::enqueue_proc
// Description:
// This pushes the data onto the queue
// Arguments:
// value - data to be inserted
//*************************************************************************************
void ReadyQ::enqueue_proc(int value){
m_data.push_back(value);
}

//*************************************************************************************
// Method: Queue::dequeue_proc
// Description:
// Pop an element from the queue
//*************************************************************************************
void ReadyQ::dequeue_proc() {
if ( isEmpty() ){
cout << "Fatal Error - Queue is empty!!" << endl;
return;
}
m_data.erase(m_data.begin());
}

//*************************************************************************************
// Method: Queue::front
// Returns the first/top element of the queue.
// Returns:
// int - top element
//*************************************************************************************
int ReadyQ::front(){
return m_data[0];
}

//*************************************************************************************
// Method: Queue::displayAll
// Description:
// Displays the entire queue to stdout.
//*************************************************************************************
void ReadyQ::displayAll(){
for ( int i = 0; i < m_data.size(); i++ ) {
cout << m_data[i] << " ";
}
cout << endl;
}



So, there it is, now you simply need a main function that calls the ReadyQ methods, and you are good to go!

No comments: