SJF算法和RR算法的适用场景
时间: 2023-11-18 18:56:49 浏览: 51
SJF算法适用于作业的执行时间已知的情况下,可以根据作业的执行时间来进行调度,以达到最短的平均等待时间和平均周转时间。但是,如果作业的执行时间未知,则无法使用SJF算法。
RR算法适用于多个进程共享CPU的情况下,可以平衡各个进程的响应时间和吞吐量。RR算法将时间片分配给每个进程,每个进程在时间片内运行,如果时间片用完了,进程就被放回就绪队列,等待下一次调度。但是,如果时间片太短,会导致进程频繁地切换,增加系统开销;如果时间片太长,会导致响应时间变长。
相关问题
linux实现SJF算法和优先级算法
Linux实现了SJF(短作业优先)算法和优先级调度算法。在Linux中,SJF算法通过将进程的运行时间作为优先级来进行调度,运行时间越短的进程优先级越高。而优先级调度算法则根据进程的优先级来进行调度,优先级高的进程先执行。
要在Linux中实现SJF算法和优先级算法,可以使用调度策略SCHED_SHORT、SCHED_FIFO和SCHED_RR。SCHED_SHORT是SJF算法的实现方式,它使用进程的运行时间作为优先级进行调度。而SCHED_FIFO和SCHED_RR则是优先级调度算法的实现方式,它们根据进程的优先级来进行调度。
c++模拟实现RR算法和SJF算法代码实验
以下是C++语言实现RR算法和SJF算法的代码:
RR算法实现:
```c++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int burst_time; // 执行时间
int remain_time; // 剩余时间
int arrival_time; // 到达时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void RR(vector<Process> processes, int quantum) {
queue<Process> ready_queue;
int total_time = 0;
int n = processes.size();
int completed = 0;
int current_time = 0;
for (int i = 0; i < n; i++) {
total_time += processes[i].burst_time;
processes[i].remain_time = processes[i].burst_time;
}
while (completed < n) {
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remain_time > 0) {
ready_queue.push(processes[i]);
}
}
if (ready_queue.empty()) {
current_time++;
continue;
}
Process current_process = ready_queue.front();
ready_queue.pop();
int execution_time = min(quantum, current_process.remain_time);
current_time += execution_time;
current_process.remain_time -= execution_time;
if (current_process.remain_time <= 0) {
completed++;
current_process.turnaround_time = current_time - current_process.arrival_time;
current_process.waiting_time = current_process.turnaround_time - current_process.burst_time;
cout << "Process " << current_process.pid << " completed." << endl;
cout << "Turnaround time: " << current_process.turnaround_time << endl;
cout << "Waiting time: " << current_process.waiting_time << endl;
} else {
ready_queue.push(current_process);
}
}
}
int main() {
vector<Process> processes = {
{1, 10, 0, 0, 0},
{2, 5, 0, 1, 0},
{3, 8, 0, 2, 0},
{4, 3, 0, 3, 0},
{5, 7, 0, 4, 0}
};
int quantum = 2;
RR(processes, quantum);
return 0;
}
```
SJF算法实现:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int pid; // 进程ID
int burst_time; // 执行时间
int arrival_time; // 到达时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
bool compare(const Process& a, const Process& b) {
return a.burst_time < b.burst_time;
}
void SJF(vector<Process> processes) {
sort(processes.begin(), processes.end(), compare);
int n = processes.size();
int current_time = 0;
for (int i = 0; i < n; i++) {
current_time += processes[i].burst_time;
processes[i].turnaround_time = current_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
cout << "Process " << processes[i].pid << " completed." << endl;
cout << "Turnaround time: " << processes[i].turnaround_time << endl;
cout << "Waiting time: " << processes[i].waiting_time << endl;
}
}
int main() {
vector<Process> processes = {
{1, 10, 0, 0, 0},
{2, 5, 1, 0, 0},
{3, 8, 2, 0, 0},
{4, 3, 3, 0, 0},
{5, 7, 4, 0, 0}
};
SJF(processes);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)