- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我卡在了我目前的任务上,主要是因为我在很长一段时间没有使用 C 后不太精通。
我们需要制作一个 FCFS(先到先得)调度算法模拟器,它可以简单地遍历并遍历每个进程将发生的所有时间事件,并在它们完成进程和周转时间时打印出来。
我知道这个过程是如何工作的,但是将它实现到 C 或 C++ 程序中让我很困惑,我什至不知道从哪里开始。
我们将从 stdin 获取输入,格式如下:
第一行:(# of cpus)(# of processes)(量子大小)
进程线:(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;
}
是的, super 原始且效率不高,但它会读取所有数字并在 EOF 或任何非数字出现时结束。也是负数。我将 proc 设置为 25 的数组的原因是因为这是我们的讲师所说的数字也会增加的限制。对于 CPU,他说最多会使用四个,所以我只做了一个数组,主要是因为我对指针很糟糕。
现在我已将数据放入我的数组中,我需要按 subTime 对我的 proc 数组进行排序并开始进行实际计算。这部分会有多糟糕,尤其是我糟糕的设置?
最佳答案
实现目标的行动计划:
第 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,维护一个数组,它可以告诉您标识其空闲或分配了一些进程。
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.
完成此处后,您可以修改它以优先考虑进程。
关于c - 我应该如何开始用 C 或 C++ 编写 FCFS 调度程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113028/
我正在尝试在Elasticsearch中返回的值中考虑地理位置的接近性。我希望近距离比某些字段(例如legal_name)重要,但比其他字段重要。 从文档看来,当前的方法是使用distance_fea
我是Elasticsearch的初学者,今天在进行“多与或”查询时遇到问题。 我有一个SQL查询,需要在Elastic中进行转换: WHERE host_id = 999 AND psh_pid =
智能指针应该/可以在函数中通过引用传递吗? 即: void foo(const std::weak_ptr& x) 最佳答案 当然你可以通过const&传递一个智能指针。 这样做也是有原因的: 如果接
我想执行与以下MYSQL查询等效的查询 SELECT http_user, http_req_method, dst dst_port count(*) as total FROM my_table
我用这两个查询进行测试 用must查询 { "size": 200, "from": 0, "query": { "bool": { "must": [ { "mat
我仍在研究 Pro Android 2 的简短服务示例(第 304 页)同样,服务示例由两个类组成:如下所示的 BackgroundService.java 和如下所示的 MainActivity.j
给定标记 like this : header really_wide_table..........................................
根据 shouldJS 上的文档网站我应该能够做到这一点: ''.should.be.empty(); ChaiJS网站没有使用 should 语法的示例,但它列出了 expect 并且上面的示例似乎
我在 Stack Overflow 上读到一些 C 函数是“过时的”或“应该避免”。你能给我一些这种功能的例子以及原因吗? 这些功能有哪些替代方案? 我们可以安全地使用它们 - 有什么好的做法吗? 最
在 C++11 中,可变参数模板允许使用任意数量的参数和省略号运算符 ... 调用函数。允许该可变参数函数对每个参数做一些事情,即使每个参数的事情不是一样的: template void dummy(
我在我从事的项目之一上将Shoulda与Test::Unit结合使用。我遇到的问题是我最近更改了此设置: class MyModel :update end 以前,我的(通过)测试看起来像这样: c
我该如何做 or使用 chai.should 进行测试? 例如就像是 total.should.equal(4).or.equal(5) 或者 total.should.equal.any(4,5)
如果您要将存储库 B 中的更改 merge 到存储库 A 中,是否应该 merge .hgtags 中的更改? 存储库 B 可能具有 A 中没有的标签 1.01、1.02、1.03。为什么要将这些 m
我正在尝试执行X AND(y OR z)的查询 我需要获得该代理为上市代理或卖方的所有已售属性(property)。 我只用 bool(boolean) 值就可以得到9324个结果。当我添加 bool
我要离开 this教程,尝试使用 Mocha、Supertest 和 Should.js 进行测试。 我有以下基本测试来通过 PUT 创建用户接受 header 中数据的端点。 describe('U
我正在尝试为 Web 应用程序编写一些 UI 测试,但有一些复杂的问题希望您能帮助我解决。 首先,该应用程序有两种模式。其中一种模式是“训练”,另一种是“现场”。在实时模式下,数据直接从我们的数据库中
我有一个规范: require 'spec_helper' # hmm... I need to include it here because if I include it inside desc
我正在尝试用这个测试我在 Rails 中的更新操作: context "on PUT to :update" do setup do @countdown = Factory(:count
我还没有找到合适的答案: onclick="..." 中是否应该转义 &(& 符号)? (或者就此而言,在每个 HTML 属性中?) 我已经尝试在 jsFiddle 和 W3C 的验证器上运行转义和非
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Main extends Applet i
我是一名优秀的程序员,十分优秀!