SRTF:最短剩余时间优先调度算法

SRTF, Which Stands for Shortest Remaining Time First is a scheduling algorithm used in Operating Systems, which can also be called as the preemptive version of the SJF scheduling algorithm. The process which has the least processing time remaining is executed first. As it is a preemptive type of schedule, it is claimed to be better than SJF scheduling Algorithm.

SRTF ,它是最短的剩余时间优先 ,它是操作系统中使用的调度算法,也可以称为SJF调度算法的抢先版本。 首先执行剩余处理时间最少的过程。 由于它是调度的优先类型,因此它比SJF调度算法要好。

Let's understand this with the help of an example. Suppose we have the following 3 processes with process ID's P1, P2, and P3 and they arrive into the CPU in the following manner:

让我们借助示例来理解这一点。 假设我们有以下三个进程,进程ID为P1P2P3 ,它们以以下方式到达CPU:

SRJF algorithm

Gant Chart:

甘特图:

SRJF algorithm

Explanation:

说明:

  • At the 0th unit of the CPU, we have only process P1, so it gets executed for the 1-time unit.

    在CPU的 0 单元中,我们只有进程P1 ,因此它以1次单元执行。

  • At the 1st unit of the CPU, the Process P2 also arrives. Now, the P1 needs 7 more units more to be executed, and P2 needs only 2 units. So, P2 is executed by preempting P1.

    在CPU的第一个单元上,过程P2也到达。 现在, P1需要多执行7个单元,而P2仅需要2个单元。 因此,P2被抢占P1执行。

  • P2 gets completed at time unit 3, and unit now no new process has arrived. So, after the completion of P2, again P1 is sent for execution.

    P2在第3单元的时间完成,并且现在没有新的进程到达。 因此,在P2完成之后,再次发送P1以便执行。

  • Now, P1 has been executed for one unit only, and we have an arrival of new process P3 at time unit 4. Now, the P1 needs 6-time units more and P3 needs only 3-time units. So, P3 is executed by preempting P1.

    现在,仅以一个单位执行P1 ,并且新的过程P3以时间单位4到达。现在, P1需要更多的6倍时间单位,而P3仅需要3倍时间单位。 因此,通过抢占P1来执行P3

  • P1 gets completed at time unit 7, and after that, we have the arrival of no other process. So again, P1 is sent for execution, and it gets completed at 13th unit.

    P1在时间单位7处完成,此后,我们没有其他进程到达。 因此,再次发送P1来执行,它以 13 单位完成。

SRJF algorithm
    Total Turn Around Time = 13 + 2 + 3
                = 18 milliseconds
    Average Turn Around Time= Total Turn Around Time / Total No. of Processes
                = 18 / 3
                = 6 milliseconds

    Total Waiting Time = 5 + 0 + 0
                = 5 milliseconds
    Average Waiting Time = Total Waiting Time / Total No. of Processes
                = 5 / 3
                = 1.67 milliseconds


翻译自: https://www.includehelp.com/operating-systems/srtf-shortest-remaining-time-first-scheduling-algorithm.aspx

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 实验目的 调度的实质是操作系统按照某种预定的策略来分配资源。进程调度的目的是分配CPU资源。由于进程调度程序执行的频率很高,因此调度算法的好坏直接影响到操作系统的性能。本实验的目的是编程模拟实现几种常用的进程调度算法,通过对几组进程分别使用不同的调度算法,计算进程的平均周转时间和平均带权周转时间,比较各种算法的性能优劣。 2. 实验原理 [1]. 进程调度算法描述 进程调度算法包括先来先服务调度算法最短作业时间优先(抢占式和非抢占式)、最高响应比调度算法4种。(每个人必须做FCFS,然后在后面的三种中任选一种,即每个人必须做2种调度算法的模拟。) [2]. 衡量算法性能的参数 计算进程的平均周转时间和平均带权周转时间。 3. 实验内容 (1)编程实现本实验的程序,要求: [1]. 建立进程的进程控制块,进程控制块至少包括: a) 进程名称; b) 进程需要执行时间; c) 进入就绪队列时间; d) 进程执行开始时间 e) 进程执行结束时间 [2]. 编程实现调度算法。 [3]. 进程及相关信息的输入。这些信息可以直接从键盘上输入,也可以从文件读取。 [4]. 时间片与时间流逝的模拟。本实验需要对算法的执行计时,程序应该提供计算时间的方法。一种最简单的方法是使用键盘,比如每敲一次空格代表一个时间片的流逝。另一种方法是使用系统时钟。 [5]. 一组进程序列执行完毕,打印出结果信息。程序需要计算出每个进程的开始执行时间、结束时间、周转时间和带权周转时间,并为整个进程序列计算平均周转时间和平均带权周转时间。程序将计算结果按一定的格式显示在计算机屏幕上或输出到文件中。打印出进程调度顺序图。 [6]. 实现数据在磁盘文件上的存取功能。 (2)对下列就绪进程序列分别使用上面的几种算法进行调度,计算每种算法下的平均周转时间和平均带权周转时间。 进程号 到达时间 要求执行时间 0 0 1 1 1 35 2 2 10 3 3 5 4 6 9 5 7 21 6 9 35 7 11 23 8 12 42 9 13 1 10 14 7 11 20 5 12 23 3 13 24 22 14 25 31
SRTF(Shortest Remaining Time First)是一种用于调度任务的算法,它根据任务的剩余执行时间来选择下一个要执行的任务。以下是用C语言实现SRTF调度算法的示例代码: ```c #include <stdio.h> #include <stdbool.h> typedef struct { int pid; // 进程ID int burst; // 执行时间 int remaining; // 剩余执行时间 } Process; void SRTF(Process processes[], int n) { int currentTime = 0; int totalBurstTime = 0; bool isCompleted[n]; // 初始化剩余执行时间和完成状态 for (int i = 0; i < n; i++) { processes[i].remaining = processes[i].burst; isCompleted[i] = false; totalBurstTime += processes[i].burst; } while (currentTime < totalBurstTime) { int shortestJobIndex = -1; int shortestRemainingTime = INT_MAX; // 寻找剩余时间最短的任务 for (int i = 0; i < n; i++) { if (!isCompleted[i] && processes[i].remaining < shortestRemainingTime) { shortestRemainingTime = processes[i].remaining; shortestJobIndex = i; } } // 更新当前时间和任务的剩余执行时间 currentTime++; processes[shortestJobIndex].remaining--; // 如果任务执行完毕,将其标记为已完成 if (processes[shortestJobIndex].remaining == 0) { isCompleted[shortestJobIndex] = true; } } // 打印结果 printf("进程ID\t执行时间\n"); for (int i = 0; i < n; i++) { printf("%d\t%d\n", processes[i].pid, processes[i].burst); } } int main() { Process processes[] = { {1, 6}, {2, 8}, {3, 3}, {4, 4} }; int n = sizeof(processes) / sizeof(Process); SRTF(processes, n); return 0; } ``` 这段代码中,定义了一个`Process`结构体,其中包含进程的ID、执行时间和剩余执行时间。`SRTF`函数用于实现SRTF调度算法,使用了一个循环来寻找剩余时间最短的任务,并依次减少任务的剩余执行时间。程序会不断更新当前时间和任务的剩余执行时间,直到所有任务都执行完毕。最后,会打印出每个进程的ID和执行时间。在`main`函数中,定义了一个示例任务队列并调用了`SRTF`函数进行调度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值