我被困在目前的任务上,主要是因为我在不使用很长一段时间后对C语言并不十分熟悉。
我们需要制作一个FCFS(First Come First Serve)调度算法模拟器,它简单地完成并完成每个过程发生的所有时间事件,并在完成过程和周转时间时打印出来。
我了解这个过程是如何工作的,但是将它实现到C或C ++程序中让我感到困惑,我甚至不知道从哪里开始。
我们将从stdin获取输入,它将采用以下格式:
第一行:( cpus#)(进程数)(量子大小)
生产线:(ID)(优先级)(提交时间)(需要的CPU时间)(需要I / O之前的计算时间)(每个计算的I / O时间)
根据从第一行定义的进程数量,需要更多的生产线。
示例输入可能是:
1 1 10
1 1 0 20 5 50
长话短说,任何人都可以向我指出一些资源,这些资源可以帮助或提供包含多个CPU可能性的源代码。或者,如果你们甚至可以帮助我开始,我真的很感激它。我不是要求你完成整个事情,只是帮助我开始,所以我会知道从哪里去。
谢谢!
编辑:
这是我到目前为止所得到的。这是非常简陋的,但到目前为止,我只是想完成这件事,就像我说的那样,我对C非常生疏(不管我是不是很精通它):
int main()
{
//process * proc = (process *)malloc(100*sizeof(process));
process proc[25];
CPU cp[4];
int count = 0;
//int * input = malloc(sizeof(int) * 100);
int input = 0;
int cpus = 0;
int processes = 0;
int quantum = 0;
int processLoop = 0;
int x = 0;
int id = 0;
int pri = 0;
int sub = 0;
int cptime = 0;
int compute = 0;
int itime = 0;
int complete = 0;
while(1 == scanf("%d", &input))
{
if(count < 0)
break;
if(count == 0)
{
cpus = input;
}
else if(count == 1)
{
processes = input;
}
else if(count == 2)
{
quantum = input;
}
else
{
if(count == 3)
{
proc[processLoop].ID = input;
}
else if(count == 4)
{
proc[processLoop].Priority = input;
}
else if(count == 5)
{
proc[processLoop].subTime = input;
}
else if(count == 6)
{
proc[processLoop].cpuTime = input;
}
else if(count == 7)
{
proc[processLoop].computeTime = input;
}
else if(count == 8)
{
proc[processLoop].ioTime = input;
proc[processLoop].isCompleted = 0;
processLoop++;
if(processLoop == processes)
{
count = -1; //Leaves possibility for multiple simulations in one run
}
else
{
count = 2; //Repeats if multiple processes are detected
}
}
}
count++;
}
for(x = 0; x < cpus; x++)
{
cpu[x].idle = 0;
}
return 0;
}
是的,超级原始而且效率不高,但它会读取所有数字并以EOF结束,或者当任何不是数字的时候都会出现。也是一个负数。我使proc为25的数组的原因是因为这是我们的教练说数字也会增加的限制。有了CPU,他说最多会使用4个,所以我只是制作了一个数组,主要是因为我用指针很糟糕。
现在我将数据存入我的数组,我需要通过subTime对proc数组进行排序,并开始实际的计算。这个部分有多糟糕,尤其是我糟糕的设置?
答案 0 :(得分:3)
实现目标的行动计划:
第1步:
为Process定义数据结构。对于C,您可以使用如下结构:(对于C ++使用类)
typedef struct process
{
int ID;
int Priority;
int time_of_submission;
int cpu_time_required;
int compute_time_before_IO;
int IO_time_per_compute;
int isCompleted; // if 1 means its complete. at start, its value is 0
}process;
对于CPU,维护一个数组,该数组可以告诉您id空闲或分配了一些进程。
typedef struct CPU
{
int idle; // if set to 1, then its idle.
// If set to 0, then its working on a process
process *next; // points to the process that CPU is executing
// points to null if CPU is idle.
}CPU;
第2步:
使用scanf扫描所有输入,为所有输入过程填充“process
”数组。为了简化您的工作,请根据根据FCFS决定调度的字段对“process
”数组进行排序。 (例如,time_of_submission,所需的cpu时间,优先级..我将把它留给你用于其他领域)
初始化'CPU
'数组。
第3步: 开发单CPU代码。验证它是否正常工作。
我认为你遇到了多CPU场景的问题。因此暂时不考虑多CPU概念。记下纸上单CPU的逻辑。创建伪代码。然后从中创建一个C(或C ++)代码。每当坚持语法时,谷歌就如何做到这一点并继续前进。
第4步: 修改多个CPU的代码。验证它是否有效
考虑多CPU会发生什么。
Loop over all CPUs.
For CPU[i], check its status
if its idle
assign it a process..similar logic as for single CPU case (step 3)
if its not idle
if its corresponding process is finished, set status of CPU to idle.
完成此操作后,您可以对其进行修改以优先考虑流程。
答案 1 :(得分:0)
#include <iostream>
using namespace std;
//fcfs algorithm in c++
//Here is the simple example involving 3 process;
//All process are assumed to be arrived at same time=0 seconds
int main()
{
//arrival time of each proces=0;
//all arive at same time
//arrival time is assumed to be same
int process[10]={1,2,3};
int bt[10]={10,5,8};
int wt[10];
int tt[10];
wt[0]=0;
//waiting time of each process;
for(int i=1;i<3;i++){
wt[i]=bt[i-1]+wt[i-1];
}
//turnaround time calculation for each processs
for(int i=0;i<3;i++){
tt[i]=wt[i]+bt[i];
}
float awt=0;//average waiting time
float att=0;//average turnaround time;
for(int i=0;i<3;i++){
awt=awt+wt[i];
att=att+tt[i];
}
for(int i=0;i<3;i++){
cout<<"process"<<i+1<<"--->"<<" "<<bt[i]<<" "<<wt[i]<<" "<<tt[i]<<endl;
}
cout<<"Awerage waiting time"<<awt/3<<endl;
cout<<"average turnaround time"<<att/3<<endl;
}