我被困在目前的任务中,主要是因为我在很长一段时间没有使用过C语言后,并不是很精通。
我们需要做一个FCFS (先来先服务)调度算法模拟器,它简单地通过并经历每个过程将发生的所有时间事件,并在它们完成其过程和周转时间时打印出来。
我知道这个过程是如何工作的,但是在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,他说最多会使用四个,所以我做了一个数组,主要是因为我不擅长指针。
现在我已经将数据放入了我的数组中,我需要按subTime对proc数组进行排序,并开始实际的计算。这部分会有多糟糕,尤其是我糟糕的设置?
发布于 2012-04-12 13:06:09
帮助您实现目标的行动计划:
是否正常工作
第1步:
定义流程的数据结构。对于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,维护一个数组,它可以告诉你它是空闲的,还是分配了一些进程。
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
‘数组。为了使您的工作更容易,根据字段对'process
‘数组进行排序,这些字段决定了如何根据FCFS进行调度。(例如,time_of_submission、所需cpu时间、优先级..我将把它留给您来处理其他字段)
初始化“CPU
”的数组。
第3步:为单CPU开发代码。验证它是否工作正常。
我认为您在多CPU方案中遇到了问题。因此,暂时不考虑多CPU概念。在纸上写下单CPU的逻辑。创建伪代码。然后用它创建一个C(或C++)代码。无论何时陷入语法困境,谷歌都会告诉你如何做到这一点并继续前进。
第四步:修改多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.
在这里完成后,您可以修改它,使其具有要考虑的进程的优先级。
发布于 2018-03-12 23:42:35
#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;
}
https://stackoverflow.com/questions/10113028
复制相似问题