Question: class Job { public: std::string jobID; char jobClass; int priority; double runtime; long long startTime; long long endTime; double elapsedTime; Job ( ) : jobID

class Job {
public:
std::string jobID;
char jobClass;
int priority;
double runtime;
long long startTime;
long long endTime;
double elapsedTime;
Job() : jobID(""), jobClass(''), priority(0), runtime(0), startTime(-1), endTime(-1), elapsedTime(-1){}
Job(const std::string& id, char cls, int prio)
: jobID(id), jobClass(cls), priority(prio), runtime(30+ std::rand()%61),
startTime(-1), endTime(-1), elapsedTime(-1){}
bool operator<(const Job& other) const {
return priority < other.priority;
}
};
std::queue populateInputQueue(const std::string& filename){
std::queue inputQueue;
std::ifstream file(filename);
std::string line;
std::getline(file, line);
while (std::getline(file, line)){
std::istringstream iss(line);
std::string jobID;
char jobClass;
int priority;
iss >> jobID >> jobClass >> priority;
if (!iss.fail()){
Job job(jobID, jobClass, priority);
std::cout << "Loaded job: "<< job.jobID <<"| Class: "<< job.jobClass
<<"| Priority: "<< job.priority
<<"| Runtime: "<< std::fixed << std::setprecision(1)<< job.runtime <<"s
";
inputQueue.push(job);
}
}
return inputQueue;
}
std::unordered_map> moveJobsToClassQueues(std::queue& inputQueue){
std::unordered_map> jobClassQueues;
for (char c ='A'; c <='Z'; ++c){
jobClassQueues[c]= std::priority_queue();
}
while (!inputQueue.empty()){
Job job = inputQueue.front();
inputQueue.pop();
jobClassQueues[job.jobClass].push(job);
std::cout << "Moving job "<< job.jobID <<" to class '"<< job.jobClass <<"' queue.
";
}
return jobClassQueues;
}
std::vector dispatchJobs(std::unordered_map>& jobClassQueues,
const std::unordered_map& processors){
std::vector outputQueue;
std::unordered_map> processorStatus;
for (const auto& p : processors){
processorStatus[p.first]= std::vector(p.second);
}
int currentTime =100000000;
std::srand(static_cast(std::time(0)));
while (true){
bool allQueuesEmpty = true;
for (const auto& entry : jobClassQueues){
if (!entry.second.empty()||
std::any_of(processorStatus[entry.first].begin(), processorStatus[entry.first].end(),
[](const Job& job){ return !job.jobID.empty(); })){
allQueuesEmpty = false;
break;
}
}
if (allQueuesEmpty) break;
for (const auto& entry : processorStatus){
char classType = entry.first;
for (size_t i =0; i < entry.second.size(); ++i){
if (entry.second[i].jobID.empty() && !jobClassQueues[classType].empty()){
Job job = jobClassQueues[classType].top();
job.startTime = currentTime;
job.endTime = currentTime + static_cast(job.runtime);
processorStatus[classType][i]= job;
std::cout << "Dispatching job: "<< job.jobID <<" from Class: "<< job.jobClass
<<" to Processor "<< i <<"
";
jobClassQueues[classType].pop();
}
}
}
for (auto& entry : processorStatus){
char classType = entry.first;
for (size_t i =0; i < entry.second.size(); ++i){
Job& job = entry.second[i];
if (!job.jobID.empty() && job.endTime <= currentTime){
job.elapsedTime = job.endTime - job.startTime;
outputQueue.push_back(job);
std::cout << "Completed job: "<< job.jobID
<<"| Start Time: "<< job.startTime
<<"| End Time: "<< job.endTime
<<"| Runtime: "<< std::fixed << std::setprecision(1)<< job.runtime <<"s
";
entry.second[i]= Job("",'',0);
}
}
}
currentTime++;
}
return outputQueue;
}
This phase will implementconcurrency of the three different processes.
Chnages to existing functions:
After reading in the initial "jobs file" to populate the input queue, the 'read job' process will allow a user at the console to enter new jobs. Each new job will be entered at the console. The user will enter the job class, and the job priority (a higher value has a higher priority). The process will generate a 'runtime' between 30 and 90 seconds, and the jobname will be "J" with the next sequential job number (3 digits).
The 'system' will shut down once there are no jobs to be processed. This occurs when there are no available jobs for the processors to handle. C++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!