#multiprocessing #round-robin
Вопрос:
Я пытаюсь изменить свой код в соответствии с требованиями моего класса, но не знаю, как это сделать. Проект заключается в следующем: После того, как вы смоделировали планирование заданий для 1 процессора, выполните ту же симуляцию для 4 процессоров, предполагая, что у вас есть 4 отдельные очереди готовности и 1 общая очередь ожидания. Это означает, что все ожидающие задания будут помещены в одну общую очередь, а готовые к выполнению задания будут иметь 4 разные очереди для размещения. мой код таков:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
//process used to update the queue for the processes to be ran from the file.
void readyQUEUE(int queue[],int timer,int arrival[],int n, int maxProccessIndex)
{
int zeroIndex;
for(int i = 0; i < n; i )
{
if(queue[i] == 0)
{
zeroIndex = i;
break;
}
}
queue[zeroIndex] = maxProccessIndex 1;
}
void waitingQUEUE(int queue[], int n)
{
for(int i = 0; (i < n-1) amp;amp; (queue[i 1] != 0) ; i )
{
int temp = queue[i];
queue[i] = queue[i 1];
queue[i 1] = temp;
}
}
//function to check the new process as it arrives
void checkNewArrival(int timer, int arrival[], int n, int maxProccessIndex,int queue[])
{
if(timer <= arrival[n-1])
{
bool newArrival = false;
for(int j = (maxProccessIndex 1); j < n; j )
{
if(arrival[j] <= timer)
{
if(maxProccessIndex < j)
{
maxProccessIndex = j;
newArrival = true;
}
}
}
//adds the incoming process to the ready queue
//(if any arrives)
if(newArrival)
readyQUEUE(queue,timer,arrival,n, maxProccessIndex);
}
}
//Driver Code
int main()
{
//////////////////////////////////
struct job //struct created to have the data from the file put into
{
int id, arrTime, cpuTime;
};
struct cpu_struct
{
int runQuantumTime;
cpu_struct()
{
runQuantumTime=0;
}
};
vector<job> jobs; //vector for the jobs
job temp;
string data, number1, number2, number3; //strings for the data to be placed in
int i;
bool flag=false;
ifstream inputFile;
inputFile.open("job.txt"); //reading from the input file
while (!inputFile.eof())
{
getline(inputFile, data);
int pos1,pos2;
pos1 = data.find(",");
number1 = data.substr(0,pos1); //getting the first number from the line which is the process number
data = data.substr(pos1 1, data.size()-1);
pos2 = data.find(",");
number2 = data.substr(0,pos2); //second number in the line which is the time the process is to arrive at the cpu
number3 = data.substr(pos2 1, data.size()-1-pos2); //third number in the line which is the time needed for the process to run
//temp to store the data read from the job.txt file
temp.id = stoi(number1);
temp.arrTime = stoi(number2);
temp.cpuTime = stoi(number3);
jobs.push_back(temp);
}
/////////////////////////
int n,tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
tq= 5; //quantum value
n = 1001; //number of processes in the file
int arrival[n], burst[n], wait[n], turn[n], queue[n], temp_burst[n];
bool complete[n];
for(int i = 0; i < n; i )
arrival[i] = jobs[i].arrTime;
burst[i] = jobs[i].cpuTime;
temp_burst[i] = burst[i];
for(int i = 0; i < n; i )
{ //Initializing the queue and complete array
complete[i] = false;
queue[i] = 0;
}
while(timer < arrival[0]) //Incrementing Timer until the first process arrives
timer ;
queue[0] = 1;
while(true)
{
bool flag = true;
for(int i = 0; i < n; i )
{
if(temp_burst[i] != 0)
{
flag = false;
break;
}
}
if(flag)
break;
for(int i = 0; (i < n) amp;amp; (queue[i] != 0); i )
{
int ctr = 0;
while((ctr < tq) amp;amp; (temp_burst[queue[0]-1] > 0))
{
temp_burst[queue[0]-1] -= 1;
timer = 1;
ctr ;
//Checking and Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//If a process is completed then store its exit time
//and mark it as completed
if((temp_burst[queue[0]-1] == 0) amp;amp; (complete[queue[0]-1] == false))
{
//turn array currently stores the completion time
turn[queue[0]-1] = timer;
complete[queue[0]-1] = true;
cout<<" Job "<<jobs[i].id<< "scheduled for 5ms , completed "<< endl;
}
//checks whether or not CPU is idle
bool idle = true;
if(queue[n-1] == 0)
{
for(int i = 0; i < n amp;amp; queue[i] != 0; i )
{
if(complete[queue[i]-1] == false)
{
idle = false;
}
}
}
else
idle = false;
if(idle)
{
timer ;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//Maintaining the entires of processes
//after each premption in the ready Queue
waitingQUEUE(queue,n);
cout<<" Job "<< jobs[i].id << " scheduled for 5ms"<< endl;
}
}
for(int i = 0; i < n; i )
{
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}
for(int i =0; i< n; i )
{
avgWait = wait[i];
avgTT = turn[i];
}
return 0;
}