smitsky 0 Light Poster

Hi. I want to output all of the processes in the printer queue here, and I'm not able to figure out quite how to do that. The only process that shows up is the last one I entered. For instance, if I put a process into printer queue 1 and then one into printer queue 2 and then take a screen shot, printer 2's information (the last entered) is the only one to show up.

I'll mark the lines of code where the input is and hopefully that will be enough, but if it isn't and more information is needed, please ask. Thanks.

I enter the intance of the PCB object in line 359.

At line 516 I output in the screenLoop() function.

Lines 711-723 show my screenLoop() function definition.

#include <iostream>
#include <string>
#include <limits>
#include <vector>
#include <iomanip>
#include <cstdlib>

using namespace std;

/**
 * Global Variables
 */
string k;
int pmem, plen;
int dmem, dlen;
int cmem, clen;
string dname, pname, cname, drw, prw, crw;
int g_pid = 0;
float tempTau;

/**
 * Process Control Block Class
 */
class PCB
{
public:
    PCB() : pid (0), filename ( "InitialString" ), memoryLocation ( 0 ), rw ( "InitialString" ), fileLen ( 0 ), procTime( 0 ), tau( 0 ) { }
    void setPid( int p );
    int getPid() const;
    void setFilename( string f );
    string getFilename() const;
    void setMemoryStart( int m );
    int getMemoryStart() const;
    void setFileLength( int l );
    int getFileLength() const;
    void setRW( string r );
    string getRW() const;
    void setTime( float t );
    float getTime() const;
    void setTau( float t );
    float getTau() const;
    void setAlpha( float a );
    float getAlpha() const;
    void setCylin( int cc );
    int getCylin() const;
    float calculateTau();
    void setCpuTime( float c );
    float getCpuTime() const;
private:
    int pid;
    string filename;
    int memoryLocation;
    string rw;
    int fileLen;
    int procTime;
    int cylinder;
    float tau;
    static float alpha;
    float cpuTime;
    static float time;
};

/*
 * Computer structure
 */
struct computer
{
        int printer;
        int disk;
        int cd;
        int num, num2;
        vector<PCB> readyQueue;
        vector<PCB> printerQueue;
        vector<PCB> printerQueues[9];
        vector<PCB> diskQueue;
        vector<PCB> cdQueue;
        vector<int> time;
        vector<PCB> vCylinder;
        void screen();
        void prompt();
        void setNum2( int j );
        void runningPrompt();
        void calculate();
        int & findMin( vector<int> & v );
        int findPos( vector<int> & vv );
        void removeElem();
        void screenLoop();
};

/**
 * Sys Gen Process begins here. Prompt the user for input.
 */
void computer::prompt()
{
    cout << "Welcome to the Sys Gen Process"<<endl;
    cout<<"Please enter the number of printers in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> printer ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(printer<1 || printer>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of printers:"<<endl;
        while( ! ( cin >> printer ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }

    }
    printerQueues[printer];
    cout<<"Thank you. Please enter the number of disks in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> disk ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(disk<1 || disk>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of disks:"<<endl;
        while( ! ( cin >> disk ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }

    }
    cout<<"Please enter the number of CD/RW's in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> cd ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(cd<1 || cd>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of CD/RW's:"<<endl;
        while( ! ( cin >> cd ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }
    }
    cout << "Please enter an initial burst tau: " << endl;
    PCB t;
    string inputTau;
    cin >> inputTau;
    tempTau = atof(inputTau.c_str());
    while( tempTau <= 0 )
    {
        cout << "Please enter a value such as 0.9" << endl;
        cin >> inputTau;
        tempTau = atof(inputTau.c_str());
    }
    t.setTau( tempTau );
    cout << "Please enter a value for alpha: " << endl;
    string inputAlpha;
    float tempAlpha;
    cin >> inputAlpha;
    tempAlpha = atof(inputAlpha.c_str());
    while( tempAlpha <= 0 )
    {
        cout << "Please enter a value such as 0.9" << endl;
        cin >> inputAlpha;
        tempAlpha = atof(inputAlpha.c_str());
    }
    t.setAlpha( tempAlpha );
    cout << "Please enter how many cylinders for each disk: " << endl;
    string inputCylinder;
    int tempCylinder;
    for( int i = 1; i < disk + 1; i++ )
    {
        cout << "Disk[" << i << "]:" << endl;
        cin >> inputCylinder;
        tempCylinder = atof(inputCylinder.c_str());
        while( tempCylinder <= 0 )
        {
            cout << "Please enter an integer value: " << endl;
            cin >> inputCylinder;
            tempCylinder = atof(inputCylinder.c_str());
        }
    t.setCylin( tempCylinder );
    vCylinder.push_back( t );
    }
}

/**
* This runningPrompt Function receives input from the keyboard regarding processes
*/
void computer::runningPrompt()
{
    cout << "You may enter a command or type 'exit' to exit the system." << endl;
    PCB process;
    while (k != "exit")
    {
        cout << "Please enter command: ";
        cin >> k;
        if( k[0] == 'A' )
        {
            if( !readyQueue.empty() )
            {
                cout << "Interrupt detected" << endl;
                cout << "How long? " << endl;
                string inputTime;
                cin >> inputTime;
                int timeTemp;
                timeTemp = atof( inputTime.c_str());
                while( timeTemp <= 0 )
                {
                    cout << "Please enter an integer value: " << endl;
                    cin >> inputTime;
                    timeTemp = atof(inputTime.c_str());
                }
                g_pid++;
                process.setPid( g_pid );
                process.setTau( timeTemp );
                readyQueue.push_back( process );
            }
            else
            {
                cout << "New Process created." << endl;
                g_pid++;
                process.setPid( g_pid );
                process.setTau( tempTau );
                readyQueue.push_back( process );
            }
            calculate();
        }
        if( k[0] == 'L' ) // this is a test construct
        {
            cout << printerQueues[0][0].getFilename() << endl;
            cout << printerQueues[0][1].getFilename() << endl;
            //cout << printerQueues[2][0].getFilename() << endl;
            //PCB j;
            //cout << j.getTau() << endl;
            //cout << j.getAlpha() << endl;
            //j.calculate();
            //cout << j.getTau() << endl;
            //cout << readyQueue[0].calculate() << endl;
            //cout << readyQueue[1].calculate() << endl;
        }
        if( k[0] == 't' )
        {
            if( !readyQueue.empty() )
            {
                cout << "Process terminating." << endl;
                readyQueue.erase( readyQueue.begin() );
            }
            else
            {
                cout << "Ready queue is empty." << endl;
            }
        }
        if( k[0] == 'S' )
        {
            screen();
        }
        if( k[1] == '1' )
            num = 0;
        if( k[1] == '2' )
            num = 1;
        if( k[1] == '3' )
            num = 2;
        if( k[1] == '4' )
            num = 3;
        if( k[1] == '5' )
            num = 4;
        if( k[1] == '6' )
            num = 5;
        if( k[1] == '7' )
            num = 6;
        if( k[1] == '8' )
            num = 7;
        if( k[1] == '9' )
            num = 8;
        if ( k[0] == 'd' )
        {
            if( disk >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting Disk " << num << endl;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> dname;
                    process.setFilename( dname );
                    cout << "What is the starting location in memory? Please enter it:" << endl;
                    while( ( ! ( cin >> dmem ) ) || dmem < 0  )
                    {
                        cout << "That was not a positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setMemoryStart( dmem );
                    cout << "Please enter the length of your file in an integer value:" << endl;
                    while( ( ! ( cin >> dlen ) ) || dlen < 0 )
                    {
                        cout << "That was not positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setFileLength( dlen );
                    cout << "Please enter r for read or w for write" << endl;
                    cin >> drw;
                    while( ! ( drw == "r" || drw == "w" ) )
                    {
                        cout << "Please enter a r or a w" << endl;
                        cin >> drw;
                    }
                    process.setRW( drw );
                    process.setPid( readyQueue.front().getPid() );
                    diskQueue.push_back( process );
                    readyQueue.erase( readyQueue.begin());
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }
            }
            else
            {
                cout << "Please enter a valid disk number" << endl;
            }
        }
        if ( k[0] == 'p' )
        {
            if( printer >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting printer: " << num << endl;
                    num2++;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> pname;
                    process.setFilename( pname );
                    cout << "What is the starting location in memory? Please enter it:" << endl;
                    while( ( ! ( cin >> pmem ) ) || pmem < 0  )
                    {
                        cout << "That was not a positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setMemoryStart( pmem );
                    cout << "Please enter the length of your file in an integer value:" << endl;
                    while( ( ! ( cin >> plen ) ) || plen < 0 )
                    {
                        cout << "That was not positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setFileLength( plen );
                    process.setPid( readyQueue.front().getPid() );
                    readyQueue.erase( readyQueue.begin());
                    printerQueues[num].push_back( process );
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }

            }
            else
            {
                cout << "Please enter a valid printer number" << endl;
            }
        }
        if ( k[0] == 'c' )
        {
            if( cd >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting CD/RW: " << num << endl;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> cname;
                    process.setFilename( cname );
                    cout << "What is the starting location in memory? Please enter it:" << endl;
                    while( ( ! ( cin >> cmem ) ) || cmem < 0  )
                    {
                        cout << "That was not a positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setMemoryStart( cmem );
                    cout << "Please enter the length of your file in an integer value:" << endl;
                    while( ( ! ( cin >> clen ) ) || clen < 0 )
                    {
                        cout << "That was not positive integer...\nTry again: ";
                        cin.clear();
                        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                    }
                    process.setFileLength( clen );
                    cout << "Please enter r for read or w for write" << endl;
                    cin >> crw;
                    while( ! ( crw == "r" || crw == "w" ) )
                    {
                        cout << "Please enter a r or a w" << endl;
                        cin >> crw;
                    }
                    process.setRW( crw );
                    process.setPid( readyQueue.front().getPid() );
                    cdQueue.push_back( process );
                    readyQueue.erase( readyQueue.begin() );
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }
            }
            else
            {
                cout << "Please enter a valid CD/RW number. " << endl;
            }
        }
        if( k[0] == 'D' )
        {
            if( !diskQueue.empty() )
            {
                     readyQueue.push_back( process );
                      diskQueue.erase( diskQueue.begin() );

            }
            else
            {
               cout << "The disk queue is empty. " << endl;
            }
        }
        if( k[0] == 'P' )
        {
            if( !printerQueues[num].empty() )
            {
                     readyQueue.push_back( process );
                      printerQueues[num].erase( printerQueues[num].begin() );
            }
            else
            {
               cout << "The printer queue is empty. " << endl;
            }
        }
        if( k[0] == 'C' )
        {
            if( !cdQueue.empty() )
            {
                     readyQueue.push_back( process );
                      cdQueue.erase( cdQueue.begin() );
            }
            else
            {
               cout << "The CD/RW queue is empty. " << endl;
            }
        }
    }
}

/**
 * User can get a "Screen Shot" but typing "S" at command line.
 */
void computer::screen()
{
    PCB p1;
    cout << "Taking Snapshot." << endl;
    cout << "Please select r, d, p, or c." << endl;
    string rpdc;
    cin >> rpdc;
    while ( ! ( rpdc[0] == 'r' || rpdc[0] == 'p' || rpdc[0] == 'd' || rpdc[0] == 'c' ) )
    {
        cout << "Please enter r, p, d, or c. Thank you." << endl;
        cout << "Enter selection" << endl;
        cin >> rpdc;
    }
    if( rpdc[0] == 'r' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << " R/W" << endl;
        if( !readyQueue.empty() )
        {
            for( int i = 0; i < readyQueue.size(); i++ )
            {
                cout << left << setw(0) << "" << readyQueue[i].getPid() << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << " " << endl;
            }

        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'p' )
    {
        //PCB l;
        //cout << l.calculate() << endl;
        //cout << l.calculateTau() << endl;
        /*for( int i = 0; i < vCylinder.size(); i++ )
        {
            cout << vCylinder[i].getCylin() << endl;
        } // This works <--------------------------------------------------*/
        /*for( int i = 0; i < readyQueue.size(); i++ )
        {
            cout << readyQueue[i].getTau() << endl;
        }
        calculate();
        cout << readyQueue[0].getTau() << endl; // This works <------------------------------------------ */
        cout << left << setw(8) << "PID" << setw(14) << "Filename"
        << setw(14) << "Memstart" << setw(14) << "FileLen" << " R/W" << endl;
        if( !printerQueues[num].empty() )
        {
            screenLoop();
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'd' )
    {
        cout << left << setw(8) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !diskQueue.empty() )
        {
            for( int i = 0; i < diskQueue.size(); i++ )
            {
                cout << setw(0) << "d" << diskQueue[i].getPid() << setw(6) <<  " "
                << setw(14) << diskQueue[i].getFilename() << setw(14) <<  diskQueue[i].getMemoryStart()
                << setw(14) << diskQueue[i].getFileLength() << diskQueue[i].getRW() << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'c' )
    {
        cout << left << setw(8) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !cdQueue.empty() )
        {
            for( int i = 0; i < cdQueue.size(); i++ )
            {
                cout << left << setw(0) << "c" << cdQueue[i].getPid() << left << setw(5) <<  " "
                << left << setw(14) << cdQueue[i].getFilename() << left << setw(14) <<  cdQueue[i].getMemoryStart()
                << left << setw(14) << cdQueue[i].getFileLength() << cdQueue[i].getRW() << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
}

/**
 * Member function definititions follows:
 */
void PCB::setPid(int p)
{
    pid = p;
}

int PCB::getPid() const
{
    return pid;
}

void PCB::setFilename( string f )
{
    filename = f;
}

string PCB::getFilename() const
{
    return filename;
}

void PCB::setMemoryStart( int m )
{
    memoryLocation = m;
}

int PCB::getMemoryStart() const
{
    return memoryLocation;
}

void PCB::setFileLength( int l )
{
    fileLen = l;
}

int PCB::getFileLength() const
{
    return fileLen;
}

void PCB::setRW( string r )
{
    rw = r;
}

string PCB::getRW() const
{
    return rw;
}

void PCB::setTime( float t )
{
     time = t;
}


float PCB::getTime() const
{
    return time;
}

int & computer::findMin( vector<int> & v )
{
    int minIndex = 0;

    for( int i = 1; i < v.size( ); i++ )
        if( v[ minIndex ] > v[ i ] )
            minIndex = i;
    return v[ minIndex ];
}

int computer::findPos( vector<int> & v )
{
    int minIndex = 0;

    for( int i = 1; i < v.size( ); i++ )
        if( v[ minIndex ] > v[ i ] )
            minIndex = i;
    return minIndex;
}

void computer::removeElem()
{
    time.erase( time.begin() + ( findPos( time ) ) );
}

void PCB::setTau( float t )
{
     tau = t;
}

float PCB::getTau() const
{
    return tau;
}


void PCB::setAlpha( float a )
{
     alpha = a;
}

float PCB::getAlpha() const
{
       return alpha;
}

void PCB::setCylin( int cc )
{
     cylinder = cc;
}

int PCB::getCylin() const
{
    return cylinder;
}

void computer::calculate()
{
      float t;
      t = ( readyQueue[0].getTau() - readyQueue[1].getTau());
      readyQueue[0].setTau( t );
}

float PCB::calculateTau()
{
    float f;
    f = ( (getAlpha() * getTau()) + (getAlpha() * getTime()) );
    return f;
}

void PCB::setCpuTime( float c )
{
     cpuTime = c;
}

float PCB::getCpuTime() const
{
      return cpuTime;
}

void computer::screenLoop()
{
        cout << "Printer Queue " << num+1 << ":" << endl;
        setNum2( 0 );
        for( int i = 0; i < printerQueues[num].size(); i++ )
        {
            cout << setw(0) << printerQueues[num][num2].getPid() << setw(6) <<  " "
            << setw(14) << printerQueues[num][num2].getFilename() << setw(14) << printerQueues[num][num2].getMemoryStart()
            << setw(14) << printerQueues[num][num2].getFileLength() << " - " << endl;
            num2++;
            cout << endl;
        }
}

void computer::setNum2( int j )
{
    num2 = j;
}

float PCB::alpha = 0;
float PCB::time = 0;

/**
* main() begins here--
*/
int main()
{
    computer c;
    c.prompt();
    c.runningPrompt();

    return 0;
}
Be a part of the DaniWeb community

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