实现了一个多cpu的FCFS算法
Implementing a multi-CPU FCFS 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 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]
这段代码应该将数据分割成大小相等的块,并分别处理这些块。这里的棘手之处在于,创建大小相等的块可能是不可能的。此外,您应该确保不要不必要地复制输入数据,特别是当输入数据很大时。这意味着chunk
和tmp
都应该是代理而不是容器,它们只访问data
和output
中正确位置的数据。第二个内循环基本上不应该存在!
作为最后一步,将内部循环的执行移到一个单独的线程中。首先,为每个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。在这里,使用不同的进程不会给您带来任何好处,反而会增加开销。
相关文章:
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 编写一个函数以使用 n 百分比的 CPU 使用率
- 如何禁用 CPU 的无序执行
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 分别测量每个线程上花费的 CPU 时间(C++)
- 什么时候最好在子进程中使用 CPU 或 I/O 密集型代码 [ C++ ]
- 在程序运行时监视 VxWorks 中的任务 CPU 利用率
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- 您选择的 CPU 不支持 x86-64 指令集
- 如何降低应用程序的 CPU 使用率?
- 对于 CPU 无法原子操作的类型,std::atomic 有什么意义?
- 如何区分CPU和内存瓶颈?
- 如何以编程方式获取任务管理器进程CPU使用率(不是PerfMon API)
- CPU 如何提供memory_order_acquire保证?
- 一段时间后 CPU 使用率高
- C/C++memcpu基准测试:测量CPU和墙时间
- 高CPU使用率,在API桌面复制中获取帧之间具有不同的超时间隔
- FCFS CPU调度程序在C++
- 实现了一个多cpu的FCFS算法