multiprocessing - 多重处理
问题描述
我正在尝试更改我的代码以满足我班级的要求,但不知道该怎么做。该项目是:在模拟 1 个 CPU 的作业调度后,假设您有 4 个单独的就绪队列和 1 个公共等待队列,对 4 个 CPU 进行相同的模拟。这意味着所有等待的作业将被放入一个公共队列,准备运行的作业将有 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) && (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) && (queue[i] != 0); i++)
{
int ctr = 0;
while((ctr < tq) && (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) && (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 && 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;
}
解决方案
推荐阅读
- python - 将值添加到另一个列表python的基于列表的索引
- javascript - 子字符串索引问题
- sql-server - T-SQL: alter sequence number current_value without changing start_value
- html - Hamburger nav not clickable - already checked scripts and CDN
- c# - How to get self name
- javascript - 使用 getJSON 时如何使用变量作为 JSON 文件名
- r - 具有不同 x 刻度的双 Y 轴基础 R 图
- android - how to deal with 2 FragmentTransaction
- cordova - 使用带有--release标签的离子构建时任务':app:mergeReleaseResources'的执行失败
- flutter - 作为 api 请求的结果,如何将变量获取到文本字段中?