c - 我应该如何开始用 C 或 C++ 编写 FCFS 调度程序?

标签 c algorithm scheduling simulator

我卡在了我目前的任务上,主要是因为我在很长一段时间没有使用 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. 定义进程和 CPU 的数据结构
  2. 扫描所有输入。初始化所有数据结构。
  3. 为单 CPU 开发代码。验证它是否正常工作
  4. 修改多CPU代码。验证它是否有效

第 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/

相关文章:

java - Jboss 7 摧毁 Singleton EJB

c snprintf 用于连接字符串

c - strtok函数和多线程

python - 在这种情况下,python 比 C 慢得多的原因是什么?

c++ - 调度大量线程,因此只有 4 个线程并行执行

algorithm - 优化算法来安排具有依赖性的任务?

c - 自动分配给指针数组的值

performance - 我应该如何使用递归扫描算法实现 Cilk 并行?

algorithm - 从 5 个数字中选出 2 个数字的概率(来自编程珠玑,第 2 版)

algorithm - 图算法的运行时间