进程到达时间相同时的轮转调度算法实现

轮转调度 是一种CPU调度算法,其中每个进程都被循环分配一个固定的时间片。它是“先来先服务”CPU调度算法的抢占式版本。

本文将专注于实现所有进程到达时间相同的轮转调度程序。在这种场景下,所有进程同时到达,这使得调度变得更容易。我们可以专注于平均分配CPU时间和管理进程队列的核心逻辑。

到达时间相同的轮转调度算法特点

当所有进程共享相同的到达时间时,轮转调度算法具有以下关键特征:

  • 平均分配时间: 每个进程获得一个相等且固定的时间片(时间量子)来执行,确保了公平性。
  • 循环执行: 进程按循环顺序调度,CPU在完成当前时间片后移动到队列中的下一个进程。
  • 无进程饥饿: 所有进程都保证定期获得CPU时间,防止任何进程被忽视。
  • 相同启动时间: 由于所有进程同时到达,调度时无需考虑到达时间,从而简化了流程。
  • 上下文切换: 随着CPU在每个时间片后在进程之间移动,会发生频繁的上下文切换,这可能会稍微影响性能。

到达时间相同的轮转调度算法示例

如何通过程序计算轮转调度中的各项时间指标?

  • 完成时间: 进程完成执行的时间。
  • 周转时间: 完成时间与到达时间之差。周转时间 = 完成时间 – 到达时间
  • 等待时间 (W.T): 周转时间与突发时间之差。

等待时间 = 周转时间 – 突发时间

进程

完成时间

周转时间 (CT – AT)

等待时间 (TAT – BT)

P1

17

17

10

P2

13

13

9

P3

20

20

11## 所有进程到达时间均为0的轮转调度程序

计算所有进程等待时间的步骤

> – 创建一个数组 rem_bt[] 来跟踪进程的剩余突发时间。该数组最初是 bt[](突发时间数组)的副本。

> – 创建另一个数组 wt[] 来存储进程的等待时间。将此数组初始化为 0。

> – 初始化时间:t = 0

> – 在所有进程未完成之前持续遍历它们。对于第i个进程,如果尚未完成,则执行以下操作。

> – 如果 rem_bt[i] > quantum(大于时间量子)

> – t = t + quantum

> – rem_bt[i] -= quantum;

> – 否则 // 该进程的最后一个周期

> – t = t + rem_bt[i];

> – wt[i] = t – bt[i]

> – rem_bt[i] = 0; // 该进程结束

一旦我们有了等待时间,我们就可以计算进程的周转时间 tat[i],即等待时间和突发时间之和,即 wt[i] + bt[i]。

下面是上述步骤的实现。

C++


// C++ program for implementation of RR scheduling

#include

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)

{

// Make a copy of burst times bt[] to store remaining

// burst times.

int rem_bt[n];

for (int i = 0; i < n; i++)

rem_bt[i] = bt[i];

int t = 0; // Current time

// Keep traversing processes in round robin manner

// until all of them are not done.

while (1)

{

bool done = true;

// Traverse all processes one by one repeatedly

for (int i = 0; i < n; i++)

{

// If burst time of a process is greater than 0

// then only need to process further

if (rem_bt[i] > 0)

{

done = false; // There is a pending process

if (rem_bt[i] > quantum)

{

// Increase the value of t i.e. shows

// how much time a process has been processed

t += quantum;

// Decrease the burst_time of current process

// by quantum

rem_bt[i] -= quantum;

}

// If burst time is smaller than or equal to

// quantum. Last cycle for this process

else

{

// Increase the value of t i.e. shows

// how much time a process has been processed

t = t + rem_bt[i];

// Waiting time is current time minus time

// used by this process

wt[i] = t – bt[i];

// As the process gets fully executed

/

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/23596.html
点赞
0.00 平均评分 (0% 分数) - 0