实现了一个多cpu的FCFS算法

Implementing a multi-CPU FCFS algorithm

本文关键字:cpu FCFS 算法 一个 实现      更新时间:2023-10-16

我正在尝试实现一个多CPU FCFS算法,但我想不出一种方法来实现如何作业/进程跳转到另一个CPU。

谁能给我解释一下或给我一些提示,从哪里开始?

这是我到目前为止所做的,我首先尝试为单个CPU实现FCFS算法:

int n, burstTime[99], waitingTime[99], totalAT[99], aveWT = 0, aveTAT = 0, i, j;
cout << "Enter total number of processes: ";
cin >> n;
cout << "nEnter Process Burst Timen";
for (i = 0; i<n; i++)
{
    cout << "P[" << i + 1 << "]: ";
    cin >> burstTime[i];
}
waitingTime[0] = 0;    //waiting time for first process is 0
              //calculating waiting time
for (i = 1; i<n; i++)
{
    waitingTime[i] = 0;
    for (j = 0; j<i; j++)
        waitingTime[i] += burstTime[j];
}
cout << "nProcessttBurst TimetWaiting TimetTurnaround Time";
//calculating turnaround time
for (i = 0; i<n; i++)
{
    totalAT[i] = burstTime[i] + waitingTime[i];
    aveWT += waitingTime[i];
    aveTAT += totalAT[i];
    cout << "nP[" << i + 1 << "]" << "tt" << burstTime[i] << "tt" << waitingTime[i] << "tt" << totalAT[i];
}
aveWT /= i;
aveTAT /= i;
cout << "nnAverage Waiting Time: " << aveWT;
cout << "nAverage Turnaround Time: " << aveTAT <<endl;

编辑:例如,下面是我想用程序执行和实现的示例输出:

    Enter number of CPUs: 2
Enter total number of processes: 6
Enter Process Burst Time
P1: [input here]
P2: [input here]
P3: [input here]
p4: [input here]
p5: [input here]
p6: [input here]
Process     Burst Time         Waiting Time                 Turn Around Time
P1          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P2          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P3          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P4          [burst time here]  [calculated waiting time here]      [calculated turn around time]
 P5          [burst time here]  [calculated waiting time here]      [calculated turn around time]
 P6          [burst time here]  [calculated waiting time here]      [calculated turn around time]

CPUs handling the processes: 
CPU 1: P1, P3, P4
CPU 2: P2, P5, P6

有一种简单的并行处理方法。基本思想是将任务分成独立的块,每个块由单独的线程并行处理。这并不总是最适合的方法,因为在某些情况下,根本不可能将数据分割成同样独立的块。

无论如何,让我们假设任务实际上可以并行化。例如,让我们考虑一组被监视的数据:

data = input("some large file")
output = []
for i in length(data):
    output[i] = frobnosticate(data[i])

第一步是将任务分成几个块:

chunks = 42
data = input("some large file")
chunksize = length(data) / chunks
output = []
for c in chunks:
    # split off one chunk and frobnosticate it
    chunk = data[c * chunksize ... (c + 1) * chunksize]
    tmp = []
    for i in chunk:
        tmp[i] = frobnosticate(chunk[i])
    # store results in the output container
    for i in length(tmp):
        output[c * chunksize + i] = tmp[i]

这段代码应该将数据分割成大小相等的块,并分别处理这些块。这里的棘手之处在于,创建大小相等的块可能是不可能的。此外,您应该确保不要不必要地复制输入数据,特别是当输入数据很大时。这意味着chunktmp都应该是代理而不是容器,它们只访问dataoutput中正确位置的数据。第二个内循环基本上不应该存在!

作为最后一步,将内部循环的执行移到一个单独的线程中。首先,为每个CPU启动一个线程,然后等待这些线程完成并检索结果:

chunks = 42
data = input("some large file")
chunksize = length(data) / chunks
output = []
threads = []
for c in chunks:
    # split off one chunk and frobnosticate it in a separate thread
    chunk = data[c * chunksize ... (c + 1) * chunksize]
    threads[c] = create_thread(frobnosticate, chunk)
for c in chunks:
    # wait for one thread to finish and store its results in the output container
    threads[c].join()
    tmp = threads[c].get_result() 
    for i in length(tmp):
        output[c * chunksize + i] = tmp[i]

在c++中实现这个应该不是问题。您可以使用std::thread来运行多个线程,操作系统会自动将这些线程分配给不同的cpu。在这里,使用不同的进程不会给您带来任何好处,反而会增加开销。