实现多CPU FCFS算法

时间:2015-10-18 03:47:21

标签: c++ algorithm

我正在尝试实现多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 Time\n";
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 << "\nProcess\t\tBurst Time\tWaiting Time\tTurnaround 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 << "]" << "\t\t" << burstTime[i] << "\t\t" << waitingTime[i] << "\t\t" << totalAT[i];
}

aveWT /= i;
aveTAT /= i;
cout << "\n\nAverage 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

1 个答案:

答案 0 :(得分:0)

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

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

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的多个线程。使用不同的流程并不能给你带来任何好处,而是增加了开销。