21xrx.com
2025-07-09 11:35:27 Wednesday
文章检索 我的文章 写文章
C++代码实现进程调度算法
2023-07-02 06:17:32 深夜i     24     0
C++ 进程调度 算法

进程调度算法是操作系统中的一个重要概念,它决定了操作系统如何决定在众多进程中选择哪个进程运行。在操作系统中,一般有先来先服务(FCFS)、最短作业优先(SJF)、高响应比优先(HRRN)等多种进程调度算法。本文将着重介绍如何使用C++代码实现这些进程调度算法。

首先,我们来看先来先服务(FCFS)算法的代码实现。FCFS算法的特点是按照进程的到来时间先后,以队列的形式对进程进行调度。本算法的C++代码实现如下:

#include<iostream>
using namespace std;
int main()
{
  int n;
  cout<<"请输入进程数:"<<endl;
  cin>>n;
  int tmp, t;
  for(int i=1; i<=n; i++){
    cout<<"请输入第"<<i<<"个进程的运行时间:"<<endl;
    cin>>t;
    tmp += t;
    cout<<"第"<<i<<"个进程的完成时间:"<<tmp<<endl;
  }
  cout<<"平均周转时间:"<<(double)tmp/n<<endl;
  return 0;
}

接下来,我们来看最短作业优先(SJF)算法的C++代码实现。SJF算法的特点是按照进程运行的时间长短进行调度,即运行时间短的进程先运行。本算法的C++代码实现如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
struct Process bts[100];
bool cmp(Process a,Process b)
   return a.bt<b.bt;
void findWaitingTime(Process ps[], int n, int wt[])
{
  wt[0] = 0;
  for (int i = 1; i < n ; i++ ){
    wt[i] = ps[i-1].bt + wt[i-1];
  }
}
void findTurnAroundTime( Process ps[], int n,int wt[], int tat[])
{
  for (int i = 0; i < n ; i++){
    tat[i] = ps[i].bt + wt[i];
  }
}
void findavgTime(Process ps[], int n)
{
  int wt[n], tat[n], total_wt = 0, total_tat = 0;
  findWaitingTime(ps, n, wt);
  findTurnAroundTime(ps, n, wt, tat);
  cout << "Process  " << " Burst time  "<< " Waiting Time " << " Turn around time\n";
  for (int i=0; i<n; i++){
    total_wt = total_wt + wt[i];
    total_tat = total_tat + tat[i];
    cout << " " << ps[i].pid << "\t\t"<< ps[i].bt <<"\t   "<< wt[i]<<"\t\t " << tat[i] <<endl;
  }
  cout << "\nAverage waiting time = " << (double)total_wt/n;
  cout << "\nAverage turn around time = " << (double)total_tat/n;
}
int main()
{
  cout<<"请输入进程数:"<<endl;
  cin>>n;
  for(int i=0;i<n;i++){
    cout<<"请输入第"<<i+1<<"个进程的运行时间:";
    cin>>s[i].bt;
    s[i].pid = i+1;
  }
  sort(s,s+n,cmp);
  findavgTime(s,n);
  return 0;
}

最后,我们来看高响应比优先(HRRN)算法的C++代码实现。HRRN算法的特点是按照进程的响应比调度进程,响应比越高的进程优先运行。本算法的C++代码实现如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n;
struct Process{
  int pid, bt, wt, rt;
}s[100];
bool cmp(Process a,Process b)
{
   return a.bt<b.bt;
}
void findWaitingTime(Process ps[], int n, int wt[])
{
  int rt[n];
  for (int i = 0; i < n ; i++){
    rt[i] = ps[i].bt;
  }
  int complete = 0, t = 0, minM;
  bool check = false;
  while (complete != n){
    for (int j = 0; j < n; j++){
      if ((ps[j].rt <= t) && (rt[j] > 0)){
        minM = j;
        check = true;
        break;
      }
    }
    if (check == false){
      t++;
      continue;
    }
    for (int j = 0; j < n; j++){
      if ((ps[j].rt <= t) && (rt[j] > 0)){
        if ((rt[j] / ps[j].bt) > (rt[minM] / ps[minM].bt)){
          minM = j;
        }
      }
    }
    rt[minM]--;
    if (rt[minM] == 0){
      complete++;
      check = false;
      int finish_time = t + 1;
      ps[minM].wt = finish_time - ps[minM].bt;
      if (ps[minM].wt < 0)
        ps[minM].wt = 0;
    }
    t++;
  }
}
void findTurnAroundTime( Process ps[], int n,int wt[], int tat[])
{
  for (int i = 0; i < n ; i++){
    tat[i] = ps[i].bt + wt[i];
  }
}
void findavgTime(Process ps[], int n)
{
  int wt[n], tat[n], total_wt = 0, total_tat = 0;
  findWaitingTime(ps, n, wt);
  findTurnAroundTime(ps, n, wt, tat);
  cout << "Process  "<< " Burst time  "<< " Waiting Time " << " Turn around time\n";
  for (int i=0; i<n; i++){
    total_wt = total_wt + wt[i];
    total_tat = total_tat + tat[i];
    cout << " " << ps[i].pid << "\t\t" << ps[i].bt <<"\t   " << wt[i]<<"\t\t " << tat[i] <<endl;
  }
  cout << "\nAverage waiting time = " << (double)total_wt/n;
  cout << "\nAverage turn around time = " << (double)total_tat/n;
}
int main()
{
  cout<<"请输入进程数:"<<endl;
  cin>>n;
  for(int i=0;i<n;i++){
    cout<<"请输入第"<<i+1<<"个进程的运行时间:";
    cin>>s[i].bt;
    s[i].pid = i+1;
  }
  sort(s,s+n,cmp);
  findavgTime(s,n);
  return 0;
}

以上是三个进程调度算法的C++代码实现。大家可以根据自己的需要来选择相应的算法进行应用。

  
  

评论区