qofcourse
March 8th, 2006, 12:55 AM
I've been trying for the last few hours to get my homework assignment to compile with no luck. There were a lot of errors I've successfully cleaned out, but there are also a lot that don't make any sense to me - g++ is telling me that variables passed into a function aren't declared in that scope, that "cout" is undeclared when I've already included iostream, that functions I just wrote can't be used as functions, etc etc. I'll admit it: I'm about as confused as I've ever been. I need help.
The objective of the assignment is to mimic a CPU scheduling system. As yet I have no idea if the program does what it's supposed to, but I can figure that out later. For now, I'd love some help getting this to run.
This program is the one you actually execute, which forks off the second program then reads commands from a file and pipes them to the second program. It compiles fine.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
using namespace std;
int main(){
int filedes[2];
pid_t pid;
char* cmd;
char arg[8];
if (pipe(filedes) != 0){
perror("Failed to create pipe.");
exit(1);
}
if ((pid = fork()) == 0){
// this is the child process in here
snprintf(arg, 8, "%d", filedes[0]);
execl("manager", "manager", arg, NULL);
}
while(1){
cin >> cmd;
write(filedes[1], cmd, strlen(cmd));
sleep(1);
}
}
This is the meat of the program, which interprets and acts on the commands, and where I'm getting most of the compilation issues:
#include <iostream>
#include <queue>
#include <fstream>
#include <sys/types.h>
#include "queue_array.h"
using namespace std;
class ProgramArray{
public:
ProgramArray::ProgramArray (char readFrom, int pid, int ppid, int priority, int time);
ProgramArray::ProgramArray (char[] readFrom, int pid, int ppid, int priority, int time);
void setPid(int npid) { this->pid = npid; }
int getPid() { return this->pid; }
void setPPID(int nppid) { this->ppid = nppid; }
int getPpid() { return this->ppid; }
// progcounter is the number of instructions that have been done yet
void setProgCounter(int npcv) { *this->pcv = npcv; }
int* getProgCounter() { return this->pcv; }
void setData(int ndata) { this->data = ndata; }
int getData() { return this->data; }
void setPriority(int npriority) { this->priority = npriority; }
int getPriority() { return this->priority; }
void setState(char nstate) { this->state = nstate; }
char getState() { return this->state; }
void setStartTime(int nstart) { this->startTime = nstart; }
int getStartTime() { return this->startTime; }
void setTimeUsed(int ntime) { this->timeUsed = ntime; }
int getTimeUsed() { return this->timeUsed; }
void setCommands(char[] cmds) { this->commands = cmds; }
int getCommands() { return this->commands; }
private:
char[50] commands;
int pid;
int ppid;
int* pcv;
int data;
int priority;
char state;
int startTime;
int timeUsed;
}
class CPU{
public:
void incrementProgCounter() { progCounter++; }
ProgramArray* getCurrentlyExecuting() { return currentlyExecuting; }
void setCurrentlyExecuting(ProgramArray* novwi) { currentlyExecuting = novwi; }
int getProgCounter() { return progCounter; }
void setProgCounter(int novwi) { progCounter = novwi; }
int getData() { return data; }
void setData(int novwi) { data = novwi; }
int getTimeSlice() { return timeSlice; }
void setTimeSlice(int novwi) { timeSlice = novwi; }
private:
ProgramArray* currentlyExecuting;
int progCounter;
int data;
int timeSlice = 0;
}
void report(){
cout << "***************************************************" << endl;
cout << "Current system state is as follows:" << endl;
cout << "***************************************************" << endl;
cout << endl;
cout << "CURRENT TIME: " << endl;
cout << endl;
cout << "Running Process:" << endl;
cout << endl;
cout << endl;
cout << "BLOCKED PROCESSES:" << endl;
cout << "Queue of blocked processes:" << endl;
cout << endl;
cout << endl;
cout << "PROCESSES READY TO EXECUTE:" << endl;
cout << "Queue of processes with priority 0:" << endl;
cout << endl;
cout << "Queue of processes with priority 1:" << endl;
cout << endl;
cout << "Queue of processes with priority 2:" << endl;
cout << endl;
cout << "Queue of processes with priority 3:" << endl;
cout << endl;
}
ProgramArray* duplicateProcess(CPU* cpu, int pid, int time){
ProgramArray* dupedProc = new ProgramArray(cpu->getCurrentlyExecuting()->getCommands(), pid, cpu->getCurrentlyExecuting()->getPid(), cpu->getCurrentlyExecuting()->getPriority(), time);
dupedProc->setData(cpu->getCurrentlyExecuting()->getData());
dupedProc->setProgCounter(cpu->getProgCounter()+1);
return dupedProc;
}
ProgramArray::ProgramArray (char readFrom, int pid, int ppid, int priority, int time){
this->pid = pid;
char next;
this->ppid = ppid;
this->priority = priority;
this->startTime = time;
this->timeUsed = 0;
ifstream fin(readFrom);
while(fin >> next){
commands[commandCounter] = next;
commandCounter++;
}
fin.close();
}
ProgramArray::ProgramArray (char[] readFrom, int pid, int ppid, int priority, int time){
this->pid* = pid;
this->ppid* = ppid;
this->priority = priority;
this->commands* = readFrom*;
this->startTime = time;
this->timeUsed = 0;
}
void block(queue* blockedState, CPU* cpu){
cpu->getCurrentlyExecuting()->setData(cpu->getData());
cpu->getCurrentlyExecuting()->setProgCounter(cpu->getProgCounter());
cpu->getCurrentlyExecuting()->addTime(cpu->getTime());
blockedState->enque(cpu->getCurrentlyExecuting());
return;
}
void executeProc(queue_array* readyState, CPU* cpux){
ProgramArray* execMe;
readyState->Dequeue(execMe);
cpux->setData(execMe->getData());
cpux->setTimeSlice(0);
cpux->setProgCounter(execMe->getProgCounter());
cpux->setCurrentlyExecuting(execMe);
return;
}
int getQuanta(int priority){
switch (priority){
case 0: return 1;
case 1: return 2;
case 2: return 4;
case 3: return 8;
break;
}
return -1;
}
int main(int argc, char **argv){
int read_pipe = atoi(argv[1]);
char cmd;
pid_t pid;
int prog_counter = 0;
// Prescribed data structures begin here
int time = 0;
CPU* cpu;
ProgramArray pcbTable[50];
queue_array* readyState(4);
queue* blockedState;
int runningState;
// make the first sim process, then increment the # of counters
pcbTable[prog_counter] = newProcess('B', prog_counter++);
while(1){
read(read_fd, cmd, 1);
switch(cmd){
case 'Q': {
string int_interpreter = "";
string currentCommand = currentlyExecuting.getCommands[cpu->getProgCounter()];
cpu->incrementProgCounter();
int arg;
switch(currentCommand.c_str[0]){
case 'S':
for(i = 2; i < currentCommand.c_str.size(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(std::atoi(int_interpreter.c_str()));
break;
case 'A':
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(cpu->getData()+std::atoi(int_interpreter.c_str()));
break;
case 'D':
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(cpu->getData()-std::atoi(int_interpreter.c_str()));
break;
case 'B':
if (cpu->getCurrentlyExecuting()->getPriority() > 0){
cpu->getCurrentlyExecuting()->setPriority(cpu->getCurrentlyExecuting()->getPriority()-1);
}
block(blockedState, cpu);
executeProc(readyState, cpu);
break;
case 'E':
pcbTable[cpu->getCurrentlyExecuting()->getPid()] = null;
~(cpu->getCurrentlyExecuting());
executeProc(readyState, cpu);
break;
case 'F':{
ProgramArray* forked = duplicateProcess(cpu, prog_counter);
pcbTable[prog_counter] = forked&;
readyQueue.Enqueue(forked);
prog_counter++;
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
arg = std::atoi(int_interpreter);
cpu->setProgCounter(cpu->getProgCounter() + arg);
break;
}
case 'R':
char newProg = currentCommand.c_str[2], next;
char[50] newCmds;
int commandCounter;
cpu->setProgCounter(0);
cpu->setData(null);
ifstream fin(newProg);
while(fin >> next){
newCmds[commandCounter] = next;
commandCounter++;
}
fin.close();
cpu->getCurrentlyExecuting()->setCommands(newCmds);
break;
}
time++;
cpu->setTimeSlice(cpu->getTimeSlice()+1);
int currentPriority = cpu->getCurrentlyExecuting()->getPriority();
if (cpu->getTimeSlice() == getQuanta(currentPriority){
if (currentPriority < 3) { cpu->getCurrentlyExecuting->setPriority(currentPriority+1); }
block(blockedQueue, cpu);
executeProc(readyQueue, cpu);
}
break;
}
case 'U':
{
if (!blockedState->empty()){
ProgramArray ready = blockedState->deque();
readyState.Enqueue(ready, ready.getPriority());
}
}
break;
case 'P': if ((pid = fork()) == 0){ report(); exit(1); }
break;
case 'T': if ((pid = fork()) == 0){ report(); exit(1); }
exit(1);
break; // not exactly necessary, is it?
}
}
}
And finally, the queue_array.h data structure which gives the first handful of errors. This was written by the class TA, and several other students have said it works fine.
#ifndef _QUEUE_ARRAY_H_
#define _QUEUE_ARRAY_H_
#include <vector>
template <class type>
class QueueArray
{
private:
vector< vector< type > > _qarray;
public:
QueueArray(int size)
{
this->_qarray = vector< vector< type > >(size);
}
int Enqueue(const type &item, int index)
{
if( (size_t)index >= this->_qarray.size() ) {
return -1;
}
int qsize = this->Qsize(index);
this->_qarray[index].push_back(item);
if( qsize + 1 == this->Qsize(index) ) {
return 1;
}
return 0;
}
int Dequeue(type &item)
{
for( size_t i = 0; i < this->_qarray.size(); i++ ) {
if( this->_qarray[i].size() != 0 ) {
item = this->_qarray[i].front();
this->_qarray[i].erase(this->_qarray[i].begin());
return 1;
}
}
return 0;
}
int Qsize(int index) const
{
if( (size_t)index < this->_qarray.size() ) {
return this->_qarray[index].size();
}
return -1;
}
int Asize() const
{
return this->_qarray.size();
}
int QAsize() const
{
int size = 0;
for( size_t i = 0; i < this->_qarray.size(); i++ ) {
size += _qarray[i].size();
}
return size;
}
type *Qstate(int index, int &numItem) const
{
if( (size_t)index >= this->_qarray.size() ) {
return NULL;
}
type *buf = new type[this->_qarray[index].size()];
if( buf == NULL ) {
cerr << "Out of memory." << endl;
return NULL;
}
for( int i = 0; (size_t)i < this->_qarray[index].size(); i++ ) {
buf[i] = this->_qarray[index][i];
}
numItem = this->_qarray[index].size();
return buf;
}
int PrintQueue() const
{
typename vector< vector< type > >::const_iterator it1;
typename vector< type >::const_iterator it2;
for( it1 = _qarray.begin(); it1 != _qarray.end(); it1++ ) {
for( it2 = it1->begin(); it2 != it1->end(); it2++ ) {
cout << *it2 << endl;
}
cout << "--------------------" << endl;
}
return 0;
}
};
#endif
Thanks a lot, I just can't seem to make any progress on this assignment.
The objective of the assignment is to mimic a CPU scheduling system. As yet I have no idea if the program does what it's supposed to, but I can figure that out later. For now, I'd love some help getting this to run.
This program is the one you actually execute, which forks off the second program then reads commands from a file and pipes them to the second program. It compiles fine.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
using namespace std;
int main(){
int filedes[2];
pid_t pid;
char* cmd;
char arg[8];
if (pipe(filedes) != 0){
perror("Failed to create pipe.");
exit(1);
}
if ((pid = fork()) == 0){
// this is the child process in here
snprintf(arg, 8, "%d", filedes[0]);
execl("manager", "manager", arg, NULL);
}
while(1){
cin >> cmd;
write(filedes[1], cmd, strlen(cmd));
sleep(1);
}
}
This is the meat of the program, which interprets and acts on the commands, and where I'm getting most of the compilation issues:
#include <iostream>
#include <queue>
#include <fstream>
#include <sys/types.h>
#include "queue_array.h"
using namespace std;
class ProgramArray{
public:
ProgramArray::ProgramArray (char readFrom, int pid, int ppid, int priority, int time);
ProgramArray::ProgramArray (char[] readFrom, int pid, int ppid, int priority, int time);
void setPid(int npid) { this->pid = npid; }
int getPid() { return this->pid; }
void setPPID(int nppid) { this->ppid = nppid; }
int getPpid() { return this->ppid; }
// progcounter is the number of instructions that have been done yet
void setProgCounter(int npcv) { *this->pcv = npcv; }
int* getProgCounter() { return this->pcv; }
void setData(int ndata) { this->data = ndata; }
int getData() { return this->data; }
void setPriority(int npriority) { this->priority = npriority; }
int getPriority() { return this->priority; }
void setState(char nstate) { this->state = nstate; }
char getState() { return this->state; }
void setStartTime(int nstart) { this->startTime = nstart; }
int getStartTime() { return this->startTime; }
void setTimeUsed(int ntime) { this->timeUsed = ntime; }
int getTimeUsed() { return this->timeUsed; }
void setCommands(char[] cmds) { this->commands = cmds; }
int getCommands() { return this->commands; }
private:
char[50] commands;
int pid;
int ppid;
int* pcv;
int data;
int priority;
char state;
int startTime;
int timeUsed;
}
class CPU{
public:
void incrementProgCounter() { progCounter++; }
ProgramArray* getCurrentlyExecuting() { return currentlyExecuting; }
void setCurrentlyExecuting(ProgramArray* novwi) { currentlyExecuting = novwi; }
int getProgCounter() { return progCounter; }
void setProgCounter(int novwi) { progCounter = novwi; }
int getData() { return data; }
void setData(int novwi) { data = novwi; }
int getTimeSlice() { return timeSlice; }
void setTimeSlice(int novwi) { timeSlice = novwi; }
private:
ProgramArray* currentlyExecuting;
int progCounter;
int data;
int timeSlice = 0;
}
void report(){
cout << "***************************************************" << endl;
cout << "Current system state is as follows:" << endl;
cout << "***************************************************" << endl;
cout << endl;
cout << "CURRENT TIME: " << endl;
cout << endl;
cout << "Running Process:" << endl;
cout << endl;
cout << endl;
cout << "BLOCKED PROCESSES:" << endl;
cout << "Queue of blocked processes:" << endl;
cout << endl;
cout << endl;
cout << "PROCESSES READY TO EXECUTE:" << endl;
cout << "Queue of processes with priority 0:" << endl;
cout << endl;
cout << "Queue of processes with priority 1:" << endl;
cout << endl;
cout << "Queue of processes with priority 2:" << endl;
cout << endl;
cout << "Queue of processes with priority 3:" << endl;
cout << endl;
}
ProgramArray* duplicateProcess(CPU* cpu, int pid, int time){
ProgramArray* dupedProc = new ProgramArray(cpu->getCurrentlyExecuting()->getCommands(), pid, cpu->getCurrentlyExecuting()->getPid(), cpu->getCurrentlyExecuting()->getPriority(), time);
dupedProc->setData(cpu->getCurrentlyExecuting()->getData());
dupedProc->setProgCounter(cpu->getProgCounter()+1);
return dupedProc;
}
ProgramArray::ProgramArray (char readFrom, int pid, int ppid, int priority, int time){
this->pid = pid;
char next;
this->ppid = ppid;
this->priority = priority;
this->startTime = time;
this->timeUsed = 0;
ifstream fin(readFrom);
while(fin >> next){
commands[commandCounter] = next;
commandCounter++;
}
fin.close();
}
ProgramArray::ProgramArray (char[] readFrom, int pid, int ppid, int priority, int time){
this->pid* = pid;
this->ppid* = ppid;
this->priority = priority;
this->commands* = readFrom*;
this->startTime = time;
this->timeUsed = 0;
}
void block(queue* blockedState, CPU* cpu){
cpu->getCurrentlyExecuting()->setData(cpu->getData());
cpu->getCurrentlyExecuting()->setProgCounter(cpu->getProgCounter());
cpu->getCurrentlyExecuting()->addTime(cpu->getTime());
blockedState->enque(cpu->getCurrentlyExecuting());
return;
}
void executeProc(queue_array* readyState, CPU* cpux){
ProgramArray* execMe;
readyState->Dequeue(execMe);
cpux->setData(execMe->getData());
cpux->setTimeSlice(0);
cpux->setProgCounter(execMe->getProgCounter());
cpux->setCurrentlyExecuting(execMe);
return;
}
int getQuanta(int priority){
switch (priority){
case 0: return 1;
case 1: return 2;
case 2: return 4;
case 3: return 8;
break;
}
return -1;
}
int main(int argc, char **argv){
int read_pipe = atoi(argv[1]);
char cmd;
pid_t pid;
int prog_counter = 0;
// Prescribed data structures begin here
int time = 0;
CPU* cpu;
ProgramArray pcbTable[50];
queue_array* readyState(4);
queue* blockedState;
int runningState;
// make the first sim process, then increment the # of counters
pcbTable[prog_counter] = newProcess('B', prog_counter++);
while(1){
read(read_fd, cmd, 1);
switch(cmd){
case 'Q': {
string int_interpreter = "";
string currentCommand = currentlyExecuting.getCommands[cpu->getProgCounter()];
cpu->incrementProgCounter();
int arg;
switch(currentCommand.c_str[0]){
case 'S':
for(i = 2; i < currentCommand.c_str.size(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(std::atoi(int_interpreter.c_str()));
break;
case 'A':
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(cpu->getData()+std::atoi(int_interpreter.c_str()));
break;
case 'D':
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
cpu->setData(cpu->getData()-std::atoi(int_interpreter.c_str()));
break;
case 'B':
if (cpu->getCurrentlyExecuting()->getPriority() > 0){
cpu->getCurrentlyExecuting()->setPriority(cpu->getCurrentlyExecuting()->getPriority()-1);
}
block(blockedState, cpu);
executeProc(readyState, cpu);
break;
case 'E':
pcbTable[cpu->getCurrentlyExecuting()->getPid()] = null;
~(cpu->getCurrentlyExecuting());
executeProc(readyState, cpu);
break;
case 'F':{
ProgramArray* forked = duplicateProcess(cpu, prog_counter);
pcbTable[prog_counter] = forked&;
readyQueue.Enqueue(forked);
prog_counter++;
for(i = 2; i < currentCommand.c_str.length(); i++){
int_interpreter += currentCommand.c_str[i];
}
arg = std::atoi(int_interpreter);
cpu->setProgCounter(cpu->getProgCounter() + arg);
break;
}
case 'R':
char newProg = currentCommand.c_str[2], next;
char[50] newCmds;
int commandCounter;
cpu->setProgCounter(0);
cpu->setData(null);
ifstream fin(newProg);
while(fin >> next){
newCmds[commandCounter] = next;
commandCounter++;
}
fin.close();
cpu->getCurrentlyExecuting()->setCommands(newCmds);
break;
}
time++;
cpu->setTimeSlice(cpu->getTimeSlice()+1);
int currentPriority = cpu->getCurrentlyExecuting()->getPriority();
if (cpu->getTimeSlice() == getQuanta(currentPriority){
if (currentPriority < 3) { cpu->getCurrentlyExecuting->setPriority(currentPriority+1); }
block(blockedQueue, cpu);
executeProc(readyQueue, cpu);
}
break;
}
case 'U':
{
if (!blockedState->empty()){
ProgramArray ready = blockedState->deque();
readyState.Enqueue(ready, ready.getPriority());
}
}
break;
case 'P': if ((pid = fork()) == 0){ report(); exit(1); }
break;
case 'T': if ((pid = fork()) == 0){ report(); exit(1); }
exit(1);
break; // not exactly necessary, is it?
}
}
}
And finally, the queue_array.h data structure which gives the first handful of errors. This was written by the class TA, and several other students have said it works fine.
#ifndef _QUEUE_ARRAY_H_
#define _QUEUE_ARRAY_H_
#include <vector>
template <class type>
class QueueArray
{
private:
vector< vector< type > > _qarray;
public:
QueueArray(int size)
{
this->_qarray = vector< vector< type > >(size);
}
int Enqueue(const type &item, int index)
{
if( (size_t)index >= this->_qarray.size() ) {
return -1;
}
int qsize = this->Qsize(index);
this->_qarray[index].push_back(item);
if( qsize + 1 == this->Qsize(index) ) {
return 1;
}
return 0;
}
int Dequeue(type &item)
{
for( size_t i = 0; i < this->_qarray.size(); i++ ) {
if( this->_qarray[i].size() != 0 ) {
item = this->_qarray[i].front();
this->_qarray[i].erase(this->_qarray[i].begin());
return 1;
}
}
return 0;
}
int Qsize(int index) const
{
if( (size_t)index < this->_qarray.size() ) {
return this->_qarray[index].size();
}
return -1;
}
int Asize() const
{
return this->_qarray.size();
}
int QAsize() const
{
int size = 0;
for( size_t i = 0; i < this->_qarray.size(); i++ ) {
size += _qarray[i].size();
}
return size;
}
type *Qstate(int index, int &numItem) const
{
if( (size_t)index >= this->_qarray.size() ) {
return NULL;
}
type *buf = new type[this->_qarray[index].size()];
if( buf == NULL ) {
cerr << "Out of memory." << endl;
return NULL;
}
for( int i = 0; (size_t)i < this->_qarray[index].size(); i++ ) {
buf[i] = this->_qarray[index][i];
}
numItem = this->_qarray[index].size();
return buf;
}
int PrintQueue() const
{
typename vector< vector< type > >::const_iterator it1;
typename vector< type >::const_iterator it2;
for( it1 = _qarray.begin(); it1 != _qarray.end(); it1++ ) {
for( it2 = it1->begin(); it2 != it1->end(); it2++ ) {
cout << *it2 << endl;
}
cout << "--------------------" << endl;
}
return 0;
}
};
#endif
Thanks a lot, I just can't seem to make any progress on this assignment.