四种进程调度算法的时间计算
四种进程调度算法的时间计算
进程调度算法是操作系统中的核心概念之一,用于决定CPU如何在多个等待执行的进程中进行分配。本文将通过一个具体的例题,详细讲解四种常见的进程调度算法:先来先服务(FCFS)、最短作业优先(SJF)、优先级调度和轮转法(RR)。
一、基本概念
1. CPU执行时间:
- CPU执行时间指的是一个进程在CPU上执行指令的时间长度。它是进程在CPU上运行的时间段。
2. 周转时间:
- 周转时间是指一个进程从进入系统到执行完成并退出系统的总时间。
3. 等待时间:
- 等待时间是指一个进程在就绪队列中等待执行的时间总和。
4. 平均等待时间:
- 等待时间是指一个进程在就绪队列中等待执行的时间总和除以进程总数。
5. 时间片(Time Quantum):
- 时间片是指在使用轮转调度算法时,每个进程被允许运行的时间长度。当一个进程运行的时间达到了时间片的大小后,操作系统会切换到下一个就绪队列中的进程来执行。
6. 先来先服务 (First-Come, First-Served, FCFS):
- 进程按照它们抵达CPU的顺序进行调度,先到达的进程先被执行。
- 简单且易于实现,但可能导致长作业等待时间,特别是当一个长时间运行的进程排在一组短作业之前时。
7. 最短作业优先 (Shortest Job First, SJF):
- 调度时选择下一个CPU周期最短的进程执行。
- 可以最大程度地减少平均等待时间,但需要提前知道每个进程的CPU-burst time,这对实际系统来说可能不太现实。
8. 优先级调度 (Priority Scheduling):
- 每个进程被分配一个优先级,调度器会选择具有最高优先级的进程来执行。
- 可以根据进程的重要性或其他因素来设置优先级,但可能出现饥饿(低优先级进程无法获得CPU时间)和优先级反转等问题。
9. 轮转法 (Round-Robin Scheduling, RR):
- 每个进程被分配一个小的时间片(quantum),执行完时间片后,调度器把进程移到就绪队列的末尾,然后选择下一个进程执行。
- 适合多用户系统,能够确保所有进程都能及时得到执行,但可能导致上下文切换开销增加。
二、例题
1. 英文版:
• Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:
process burst time priority
P1 22 2
P2 17 2
P3 3 2
P4 6 1
The processes are assumed to have arrived in the order P1, P2, P3, P4, all at time 0.
** a. Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a nonpreemptive priority (smaller priority implies higher priority), and RR (quantum=6) scheduling**
b. What is the turnaround time of each process for each of the scheduling algorithms in part a
c. What is the waiting time of each process for each of the scheduling algorithms in part a
d. Which of the schedules in part a results in the minimal average waiting time (over all processes)
2. 中文版:
• 请考虑以下一组进程,其CPU执行时间的长度,以毫秒为单位:
进程 执行时间 优先级
P1 22 2
P2 17 2
P3 3 2
P4 6 1
假定进程已按顺序P1、P2、P3、P4到达,均在时间0到达。
** a. 绘制四个甘特图,来说明使用FCFS、SJF、非抢占优先级(优先级越小意味着优先级越高)和RR(时间片=6)调度执行的过程.**
b. a部分中每个调度算法的每个进程的周转时间是多少?
c. a部分中每个调度算法的每个进程的等待时间是多少?
d. a部分中的哪个计划导致最短的平均等待时间?(在所有进程中)
3. 解答:
a. 绘制甘特图
- FCFS (先来先服务):
- P1: 0-22ms
- P2: 22-39ms
- P3: 39-42ms
- P4: 42-48ms
- SJF (最短作业优先):
- P3: 0-3ms
- P4: 3-9ms
- P2: 9-26ms
- P1: 26-48ms
- 非抢占式优先级调度:
- P4: 0-6ms (优先级1)
- P1: 6-28ms (优先级2)
- P2: 28-45ms (优先级2)
- P3: 45-48ms (优先级2)
- RR (轮转调度, 时间片为6ms):
- P1: 0-6ms, 18-24ms, 30-36ms, 42-48ms
- P2: 6-12ms, 24-30ms
- P3: 12-15ms
- P4: 15-21ms
b. 计算周转时间(执行时间+等待时间or结束时间-开始时间)
FCFS:
P1: 22 ms
P2: 22 + 17 = 39 ms
P3: 22 + 17 + 3 = 42 ms
P4: 22 + 17 + 3 + 6 = 48 ms
SJF:
P3: 3 ms
P4: 3 + 6 = 9 ms
P2: 3 + 6 + 17 = 26 ms
P1: 3 + 6 + 17 + 22 = 48 ms
Nonpreemptive Priority:
P4: 6 ms
P3: 48 ms
P2: 45 ms
P1: 28 ms
RR (quantum=6):
P1: 48 ms
P2: 44 ms
P3: 15 ms
P4: 21 ms
c. 计算等待时间
FCFS:
P1: 0 ms (it starts immediately)
P2: 22 ms
P3: 22 + 17 = 39 ms
P4: 22 + 17 + 3 = 42 ms
SJF:
P3: 0 ms (it starts immediately)
P4: 3 ms
P2: 3 + 6 = 9 ms
P1: 26 ms
Nonpreemptive Priority:
P4: 0 ms (it starts immediately)
P3: 45 ms
P2: 28 ms
P1: 6 ms
RR (quantum=6):
P1: 21-6+33-27+44-39=26 ms
P2: 6+27-12+39-33 = 27 ms
P3: 12 ms
P4: 15 ms
d. 计算平均等待时间
FCFS:
(0 ms+22 ms+39 ms+42 ms)/4= 25.75 ms
SJF:
(0 ms +3 ms +9 ms + 26 ms)/4= 9.5 ms
Nonpreemptive Priority:
( 0 ms+45 ms +28 ms +6 ms)/4= 19.75 ms
RR (quantum=6):
(26 ms+27 ms+12 ms+15 ms)/4= 22.25 ms
所以最短平均等待时间的方案是SJF。