用c++写最短进程优先的进程调度,输入:进程名 到达时间 服务时间 优先级 输出:进程名 开始时间 完成时间 周转时间 等待时间(不抢占)
时间: 2024-04-30 16:19:40 浏览: 76
以下是用C++编写的最短进程优先(Non-preemptive Shortest Job First,NSJF)的进程调度算法:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
string name;
int arrival_time;
int service_time;
int priority;
int start_time;
int finish_time;
int turnaround_time;
int waiting_time;
};
bool comp_arrival_time(Process a, Process b) {
return a.arrival_time < b.arrival_time;
}
bool comp_service_time(Process a, Process b) {
return a.service_time < b.service_time;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
for (int i = 0; i < n; i++) {
cout << "Enter the name, arrival time, service time, and priority of process " << i+1 << ": ";
cin >> processes[i].name >> processes[i].arrival_time >> processes[i].service_time >> processes[i].priority;
processes[i].start_time = -1;
processes[i].finish_time = -1;
processes[i].turnaround_time = -1;
processes[i].waiting_time = -1;
}
sort(processes.begin(), processes.end(), comp_arrival_time);
vector<Process> ready_queue;
int current_time = 0;
int total_turnaround_time = 0;
int total_waiting_time = 0;
while (!processes.empty() || !ready_queue.empty()) {
// Move arriving processes to the ready queue
while (!processes.empty() && processes[0].arrival_time <= current_time) {
ready_queue.push_back(processes[0]);
processes.erase(processes.begin());
}
// Sort the ready queue by service time
sort(ready_queue.begin(), ready_queue.end(), comp_service_time);
// Select the process with the shortest service time
Process selected_process = ready_queue[0];
ready_queue.erase(ready_queue.begin());
// Update start time, finish time, turnaround time, and waiting time
selected_process.start_time = current_time;
selected_process.finish_time = current_time + selected_process.service_time;
selected_process.turnaround_time = selected_process.finish_time - selected_process.arrival_time;
selected_process.waiting_time = selected_process.turnaround_time - selected_process.service_time;
total_turnaround_time += selected_process.turnaround_time;
total_waiting_time += selected_process.waiting_time;
// Update current time
current_time = selected_process.finish_time;
// Print the selected process
cout << selected_process.name << "\t" << selected_process.start_time << "\t" << selected_process.finish_time << "\t" << selected_process.turnaround_time << "\t" << selected_process.waiting_time << endl;
}
// Print the average turnaround time and waiting time
cout << "Average turnaround time: " << (double)total_turnaround_time / n << endl;
cout << "Average waiting time: " << (double)total_waiting_time / n << endl;
return 0;
}
```
该程序首先要求用户输入进程的数量和每个进程的名称、到达时间、服务时间和优先级。然后,它将进程按到达时间排序,并将它们移动到就绪队列中。在每个时间步骤中,程序选择服务时间最短的进程,并将其从就绪队列中移除。然后,程序更新所选进程的开始时间、完成时间、周转时间和等待时间,并将它们累加到总周转时间和等待时间中。最后,程序输出所选进程的详细信息以及平均周转时间和等待时间。
请注意,该程序实现了非抢占式最短进程优先调度算法,即一旦进程开始执行,它将运行到完成,而不会被其他进程抢占。如果需要实现抢占式最短进程优先调度算法,请修改程序以确保在就绪队列中始终选择服务时间最短的进程。
阅读全文