如何用c++写一个优先级服务调度算法和短作业调度算法与高响应算法程序
时间: 2024-10-16 10:14:49 浏览: 56
在 C++ 中编写优先级调度算法、短作业调度算法(也称为 FCFS - First Come, First Served 算法)以及高响应比调度算法(如 HRRN - High Response Ratio Next),通常涉及到进程管理的概念。以下是简化的步骤和示例代码:
### 1. 优先级调度算法 (Priority Scheduling)
```cpp
struct Process {
int pid;
int priority;
// 其他属性...
};
bool compareByPriority(const Process& a, const Process& b) {
return a.priority > b.priority;
}
// 调度函数
void priorityScheduling(vector<Process>& processes) {
sort(processes.begin(), processes.end(), compareByPriority);
for (auto& process : processes) {
// 执行调度操作
// ...
}
}
```
相关问题
先来先服务短作业优先优先级调度调度算法代码C++
以下是一个简单的短作业优先优先级调度算法的 C++ 代码示例:
```
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Process {
int pid; // 进程 ID
int arrivalTime; // 到达时间
int burstTime; // 执行时间
int priority; // 优先级
int waitingTime; // 等待时间
int turnaroundTime; // 周转时间
};
bool compareArrival(Process a, Process b) { // 按到达时间排序的比较函数
return a.arrivalTime < b.arrivalTime;
}
bool comparePriority(Process a, Process b) { // 按优先级排序的比较函数
return a.priority < b.priority;
}
void sjf(vector<Process>& processes) {
sort(processes.begin(), processes.end(), compareArrival); // 按到达时间排序
int time = 0;
for (int i = 0; i < processes.size(); i++) {
time = max(time, processes[i].arrivalTime); // 更新当前时间为到达时间或者上一个进程结束的时间
int minIndex = i;
for (int j = i; j < processes.size() && processes[j].arrivalTime <= time; j++) { // 在当前时间之前到达的进程中找到最短作业
if (processes[j].burstTime < processes[minIndex].burstTime) {
minIndex = j;
}
}
swap(processes[i], processes[minIndex]); // 把最短作业交换到当前位置
processes[i].waitingTime = time - processes[i].arrivalTime; // 计算等待时间
time += processes[i].burstTime; // 更新时间
processes[i].turnaroundTime = time - processes[i].arrivalTime; // 计算周转时间
}
}
void priority(vector<Process>& processes) {
sort(processes.begin(), processes.end(), compareArrival); // 按到达时间排序
int time = 0;
for (int i = 0; i < processes.size(); i++) {
time = max(time, processes[i].arrivalTime); // 更新当前时间为到达时间或者上一个进程结束的时间
int maxIndex = i;
for (int j = i; j < processes.size() && processes[j].arrivalTime <= time; j++) { // 在当前时间之前到达的进程中找到最高优先级的进程
if (processes[j].priority < processes[maxIndex].priority) {
maxIndex = j;
}
}
swap(processes[i], processes[maxIndex]); // 把最高优先级的进程交换到当前位置
processes[i].waitingTime = time - processes[i].arrivalTime; // 计算等待时间
time += processes[i].burstTime; // 更新时间
processes[i].turnaroundTime = time - processes[i].arrivalTime; // 计算周转时间
}
}
int main() {
vector<Process> processes = {
{1, 0, 6, 2},
{2, 1, 3, 1},
{3, 2, 8, 3},
{4, 3, 4, 4},
{5, 4, 5, 5}
};
sjf(processes); // 使用短作业优先算法调度进程
// priority(processes); // 使用优先级调度算法调度进程
// 输出每个进程的等待时间和周转时间
cout << "Process\tWaiting Time\tTurnaround Time" << endl;
for (int i = 0; i < processes.size(); i++) {
cout << processes[i].pid << "\t" << processes[i].waitingTime << "\t\t" << processes[i].turnaroundTime << endl;
}
return 0;
}
```
其中,`Process` 结构体代表一个进程,包含进程 ID、到达时间、执行时间、优先级、等待时间和周转时间等信息。`compareArrival` 和 `comparePriority` 函数分别用于按到达时间和优先级排序。`sjf` 函数实现了短作业优先算法,而 `priority` 函数实现了优先级调度算法。最后在 `main` 函数中调用 `sjf` 或 `priority` 函数来进行进程调度,并输出每个进程的等待时间和周转时间。
用c++综合实现:先来先服务调度算法、短 进程优先调度算法、时间片轮转调度算法和优先级调度算法(抢占式)、高响应 比优先调度算法。
以下是用 C++ 综合实现先来先服务调度算法、短进程优先调度算法、时间片轮转调度算法和优先级调度算法(抢占式)、高响应比优先调度算法的示例代码。
先来先服务调度算法:
```cpp
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int id;
int arriveTime;
int serveTime;
int startTime;
int waitTime;
int finishTime;
bool operator < (const node &b) const{
return arriveTime<b.arriveTime;
}
}a[1005];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].arriveTime>>a[i].serveTime;
a[i].id=i;
}
sort(a+1,a+n+1);
a[1].startTime=a[1].arriveTime;
a[1].finishTime=a[1].startTime+a[1].serveTime;
a[1].waitTime=0;
for(int i=2;i<=n;i++){
if(a[i].arriveTime<=a[i-1].finishTime){
a[i].startTime=a[i-1].finishTime;
}
else{
a[i].startTime=a[i].arriveTime;
}
a[i].finishTime=a[i].startTime+a[i].serveTime;
a[i].waitTime=a[i].startTime-a[i].arriveTime;
}
for(int i=1;i<=n;i++){
cout<<"Process "<<a[i].id<<":\n";
cout<<"Start time: "<<a[i].startTime<<endl;
cout<<"Finish time: "<<a[i].finishTime<<endl;
cout<<"Wait time: "<<a[i].waitTime<<endl;
cout<<endl;
}
return 0;
}
```
短进程优先调度算法:
```cpp
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int id;
int arriveTime;
int serveTime;
int startTime;
int waitTime;
int finishTime;
bool operator < (const node &b) const{
if(arriveTime!=b.arriveTime){
return arriveTime<b.arriveTime;
}
else{
return serveTime<b.serveTime;
}
}
}a[1005];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].arriveTime>>a[i].serveTime;
a[i].id=i;
}
sort(a+1,a+n+1);
a[1].startTime=a[1].arriveTime;
a[1].finishTime=a[1].startTime+a[1].serveTime;
a[1].waitTime=0;
for(int i=2;i<=n;i++){
int pos=i;
for(int j=i-1;j>=1;j--){
if(a[j].finishTime<=a[i].arriveTime){
break;
}
if(a[j].serveTime<a[pos].serveTime){
pos=j;
}
}
swap(a[i],a[pos]);
if(a[i].arriveTime<=a[i-1].finishTime){
a[i].startTime=a[i-1].finishTime;
}
else{
a[i].startTime=a[i].arriveTime;
}
a[i].finishTime=a[i].startTime+a[i].serveTime;
a[i].waitTime=a[i].startTime-a[i].arriveTime;
}
for(int i=1;i<=n;i++){
cout<<"Process "<<a[i].id<<":\n";
cout<<"Start time: "<<a[i].startTime<<endl;
cout<<"Finish time: "<<a[i].finishTime<<endl;
cout<<"Wait time: "<<a[i].waitTime<<endl;
cout<<endl;
}
return 0;
}
```
时间片轮转调度算法:
```cpp
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int id;
int arriveTime;
int serveTime;
int startTime;
int waitTime;
int finishTime;
int leftTime;
bool operator < (const node &b) const{
if(arriveTime!=b.arriveTime){
return arriveTime<b.arriveTime;
}
else{
return serveTime<b.serveTime;
}
}
}a[1005];
queue<node> q;
int main(){
int n,timeSlice;
cin>>n>>timeSlice;
for(int i=1;i<=n;i++){
cin>>a[i].arriveTime>>a[i].serveTime;
a[i].id=i;
a[i].leftTime=a[i].serveTime;
}
sort(a+1,a+n+1);
a[1].startTime=a[1].arriveTime;
a[1].finishTime=a[1].startTime+min(a[1].leftTime,timeSlice);
a[1].waitTime=a[1].startTime-a[1].arriveTime;
if(a[1].leftTime<=timeSlice){
a[1].leftTime=0;
}
else{
a[1].leftTime-=timeSlice;
q.push(a[1]);
}
int currentTime=a[1].finishTime;
int cnt=1;
while(!q.empty()||cnt<n){
while(cnt<n&&a[cnt+1].arriveTime<=currentTime){
cnt++;
q.push(a[cnt]);
}
if(q.empty()){
cnt++;
q.push(a[cnt]);
currentTime=a[cnt].arriveTime;
}
node tmp=q.front();
q.pop();
tmp.startTime=currentTime;
tmp.finishTime=currentTime+min(tmp.leftTime,timeSlice);
tmp.waitTime=tmp.startTime-tmp.arriveTime;
currentTime=tmp.finishTime;
if(tmp.leftTime<=timeSlice){
tmp.leftTime=0;
}
else{
tmp.leftTime-=timeSlice;
q.push(tmp);
}
a[tmp.id]=tmp;
}
for(int i=1;i<=n;i++){
cout<<"Process "<<a[i].id<<":\n";
cout<<"Start time: "<<a[i].startTime<<endl;
cout<<"Finish time: "<<a[i].finishTime<<endl;
cout<<"Wait time: "<<a[i].waitTime<<endl;
cout<<endl;
}
return 0;
}
```
优先级调度算法(抢占式):
```cpp
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int id;
int arriveTime;
int serveTime;
int startTime;
int waitTime;
int finishTime;
int priority;
bool operator < (const node &b) const{
if(arriveTime!=b.arriveTime){
return arriveTime<b.arriveTime;
}
else{
return priority<b.priority;
}
}
}a[1005];
struct cmp{
bool operator () (node x,node y) const{
return x.priority<y.priority;
}
};
priority_queue<node,vector<node>,cmp> q;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].arriveTime>>a[i].serveTime>>a[i].priority;
a[i].id=i;
}
sort(a+1,a+n+1);
a[1].startTime=a[1].arriveTime;
a[1].finishTime=a[1].startTime+a[1].serveTime;
a[1].waitTime=a[1].startTime-a[1].arriveTime;
for(int i=2;i<=n;i++){
if(a[i].arriveTime<=a[i-1].finishTime){
a[i].startTime=a[i-1].finishTime;
}
else{
a[i].startTime=a[i].arriveTime;
}
a[i].finishTime=a[i].startTime+a[i].serveTime;
a[i].waitTime=a[i].startTime-a[i].arriveTime;
}
int currentTime=a[1].finishTime;
int cnt=1;
while(!q.empty()||cnt<n){
while(cnt<n&&a[cnt+1].arriveTime<=currentTime){
cnt++;
q.push(a[cnt]);
}
if(q.empty()){
cnt++;
q.push(a[cnt]);
currentTime=a[cnt].arriveTime;
}
node tmp=q.top();
q.pop();
tmp.startTime=currentTime;
tmp.finishTime=currentTime+tmp.serveTime;
tmp.waitTime=tmp.startTime-tmp.arriveTime;
currentTime=tmp.finishTime;
a[tmp.id]=tmp;
}
for(int i=1;i<=n;i++){
cout<<"Process "<<a[i].id<<":\n";
cout<<"Start time: "<<a[i].startTime<<endl;
cout<<"Finish time: "<<a[i].finishTime<<endl;
cout<<"Wait time: "<<a[i].waitTime<<endl;
cout<<endl;
}
return 0;
}
```
高响应比优先调度算法:
```cpp
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int id;
int arriveTime;
int serveTime;
int startTime;
int waitTime;
int finishTime;
int priority;
double ratio;
bool operator < (const node &b) const{
if(arriveTime!=b.arriveTime){
return arriveTime<b.arriveTime;
}
else{
return ratio<b.ratio;
}
}
}a[1005];
struct cmp{
bool operator () (node x,node y) const{
return x.priority<y.priority;
}
};
priority_queue<node,vector<node>,cmp> q;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].arriveTime>>a[i].serveTime>>a[i].priority;
a[i].id=i;
a[i].ratio=1.0*a[i].serveTime/a[i].waitTime;
}
sort(a+1,a+n+1);
a[1].startTime=a[1].arriveTime;
a[1].finishTime=a[1].startTime+a[1].serveTime;
a[1].waitTime=a[1].startTime-a[1].arriveTime;
for(int i=2;i<=n;i++){
if(a[i].arriveTime<=a[i-1].finishTime){
a[i].startTime=a[i-1].finishTime;
}
else{
a[i].startTime=a[i].arriveTime;
}
a[i].finishTime=a[i].startTime+a[i].serveTime;
a[i].waitTime=a[i].startTime-a[i].arriveTime;
}
int currentTime=a[1].finishTime;
int cnt=1;
while(!q.empty()||cnt<n){
while(cnt<n&&a[cnt+1].arriveTime<=currentTime){
cnt++;
a[cnt].waitTime=currentTime-a[cnt].arriveTime;
a[cnt].ratio=1.0*a[cnt].serveTime/a[cnt].waitTime;
q.push(a[cnt]);
}
if(q.empty()){
cnt++;
a[cnt].waitTime=0;
a[cnt].ratio=1.0*a[cnt].serveTime;
q.push(a[cnt]);
currentTime=a[cnt].arriveTime;
}
node tmp=q.top();
q.pop();
tmp.startTime=currentTime;
tmp.finishTime=currentTime+tmp.serveTime;
tmp.waitTime=tmp.startTime-tmp.arriveTime;
currentTime=tmp.finishTime;
a[tmp.id]=tmp;
}
for(int i=1;i<=n;i++){
cout<<"Process "<<a[i].id<<":\n";
cout<<"Start time: "<<a[i].startTime<<endl;
cout<<"Finish time: "<<a[i].finishTime<<endl;
cout<<"Wait time: "<<a[i].waitTime<<endl;
cout<<endl;
}
return 0;
}
```
阅读全文