无法理解CPU调度概念

时间:2014-02-28 00:57:56

标签: c linux multithreading cpu scheduling

我必须用内核级线程编写CPU调度模拟。我必须能够使用先到先服务(FCFS)或循环(RR)算法。进程及其线程的数据以文本文件的形式给出。目前,我的程序将文本文件数据读入链接列表。我不确定如何开始模拟(我之前从未编程过模拟)。

这是我在FCFS的情况下如何进行的?当我到达第一个进程的第一个线程时,我将cpu时间添加到时钟时间。那么我只是简单地将io时间添加到时钟中,同时cpu处于空闲状态?或者我应该将它放回等待队列并允许下一个线程开始在cpu中运行?如果是这样,我如何跟踪每个线程已经被激活了多少?

这是一个示例测试文件:

2 4 6 // number_of_processes thread_switch process_switch
1 5 // process_number(1) number_of_threads(1)
1 0 4 // thread_number(1) arrival_time(1) number_of_CPU(1)
1 15 100 // 1 cpu_time io_time
2 18 120 // 2 cpu_time io_time
3 12 100 // 3 cpu_time io_time
4 16  // 4 cpu_time 
2 4 4 // thread_number(2) arrival_time(2) number_of_CPU(2)
1 18 110
2 15 80
3 20 75
4 15
3 6 5   //thread(3)
1 40 100
2 20 70
3 15 80
4 18 90
5 50
4 8 4   //thread(4) 
1 25 60
2 15 50
3 20 80
4 18  
5 18 4  //thread(5) 
1 8 60
2 15 120
3 12 80
4 10

1 个答案:

答案 0 :(得分:0)

根据提供的信息,I / O时间似乎不明确。你有规格/说明吗?

一般情况下,我认为I / O时间为您提供了一个窗口,在该窗口中,当等待I / O请求完成时,当前正在执行的线程可以被换出。虽然似乎没有任何迹象表明I / O时间是在CPU时间之前,之后还是混合时发生的。这可能是您在实施模拟器时期望做出的设计决定。

对于'number_of_CPU'选项的影响,也存在歧义。你在模拟多少个CPU核心?或者这仅仅是线程对CPU的请求数量的计数(它看起来像是这样,因为你不能同时在多个CPU上运行单个线程)?

在任何情况下,您对处理FCFS算法的一般方法都是正确的。基本上你会想要维护一个请求队列,每当CPU空闲时你只需将下一个东西拉出队列并执行它。假设单核CPU并忽略I / O时间,结果应如下所示:

time=0
    Thread 1 arrives, and wants to do 15 time-units of work
        Thread 1 starts executing 15 time-units of work

time=4
    Thread 2 arrives, and wants to do 18 time-units of work
        Thread 2 is blocked because Thread 1 is executing

time=6
    Thread 3 arrives, and wants to do 40 time-units of work
        Thread 3 is blocked because Thread 1 is Executing

time=8
    Thread 4 arrives and wants to do 25 time-units of work
        Thread 4 is blocked because Thread 1 is Executing

time=15
    Thread 1 completes its initial set of work
    Thread 2 is next in the queue, and begins executing
    Thread 1 wants to do 18 time-units of work
        Thread 1 is blocked because Thread 2 is executing

time=18
    Thread 5 arrives and wants to do 8 time-units of work
        Thread 5 is blocked because Thread 2 is executing

time=33
    Thread 2 completes its initial set of work
    Thread 3 is next in the queue, and begins executing
    Thread 2 wants to do 15 time-units of work
        Thread 2 is blocked because Thread 3 is executing

...