c++实现操作系统进程调度(四种方式)操作系统实习项目

包含先来先服务、抢占式最短作业优先、时间片轮转、优先数调度算法。

下图为时间片轮转效果的示例

#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
double Sum_T_time = 0.0;
double Sum_WT_time = 0.0;
int current_time = 0; // 当前时间
class Process
{
public:
    string Name;
    char state;                 // 运行状态,W就绪队列中,R运行,F结束,N尚未进入就绪队列
    int Enter_time;             // 到达时间
    int Running_time;           // 运行时间
    int Priorty;                // 优先级
    int Start_time;             // 开始时间
    int Finish_time;            // 结束时间
    int T_time;                 // 周转时间
    double WT_time;             // 带权周转时间
    int Remaining_Running_Time; // 剩余运行时间
    Process *next;
    Process()
    {
        this->state = 'N'; // 运行状态
        this->Name = '0';
        this->Enter_time = 0;
        this->Running_time = 0;
        this->Priorty = 0;
        this->next = NULL;
        this->Start_time = 0;
        this->Finish_time = 0;
        this->T_time = 0;
        this->WT_time = 0.0;
        this->Remaining_Running_Time = 0;
    }
    Process(string name, int Enter_time, int Running_time)
    {
        this->state = 'N';
        this->Name = name;
        this->Enter_time = Enter_time;
        this->Running_time = Running_time;
        this->Priorty = 0;
        this->next = NULL;
        this->Start_time = 0;
        this->Finish_time = 0;
        this->T_time = 0;
        this->WT_time = 0.0;
        this->Remaining_Running_Time = this->Running_time;
    }
    void init_RRT()
    {
        this->Remaining_Running_Time = this->Running_time;
    }
};
class Process_Queue
{
public:
    Process *Head;
    Process *Last;
    int size;
    Process_Queue()
    {
        this->size = 0;
        Last = new Process();
        Head = Last;
    }
};
Process_Queue *Ready_queue = new Process_Queue();  // 就绪队列
Process_Queue *Run_queue = new Process_Queue();    // 运行队列
Process_Queue *Finish_queue = new Process_Queue(); // 结束队列
void Print_Queue(Process_Queue *queue)
{
    Process *current_pro = queue->Head->next;
    cout << "队列长度:" << queue->size << endl;
    int count = 0;
    for (int i = 0; i < queue->size; i++)
    {
        cout << "进程名字:" << current_pro->Name << "\t";
        cout << "进程到达时间:" << current_pro->Enter_time << "\t";
        cout << "进程运行时间:" << current_pro->Running_time << "\t";
        cout << "进程剩余运行时间:" << current_pro->Remaining_Running_Time << "\t";
        cout << "序号:" << count++;
        cout << endl;
        current_pro = current_pro->next;
    }
}
void Process_Show(Process *current_pro) // 输出
{
    cout << setiosflags(ios::fixed) << setprecision(2);
    cout << current_pro->Name << "\t" << current_pro->Enter_time << "\t" << current_pro->Running_time << "\t"
         << "  " << current_pro->Priorty << "\t"
         << "  " << current_pro->Start_time << "\t"
         << "   " << current_pro->Finish_time << "\t"
         << "   " << current_pro->T_time << "\t"
         << "  " << current_pro->WT_time << endl;
}
void Finish_queue_Show(Process_Queue *queue)
{
    Process *current_pro = queue->Head->next;
    cout << "进程 到达时间 运行时间 优先级 开始时间 结束时间 周转时间 带权周转时间" << endl;
    for (int i = 0; i < queue->size; i++)
    {
        Process_Show(current_pro);
        current_pro = current_pro->next;
    }
}
int Max(int a, int b)
{
    return a > b ? a : b;
}
void init_Remaining_Running_Time(Process *process, int Num)
{
    for (int i = 0; i < Num; i++)
    {
        process[i].init_RRT();
    }
}
void SortbyEnter_time(Process *process, int First, int Last)
{
    for (int i = First; i < Last - 1; i++)
    {
        for (int j = First; j < Last - i - 1; j++)
        {
            if (process[j].Enter_time > process[j + 1].Enter_time)
            {
                Process pro;
                pro = process[j];
                process[j] = process[j + 1];
                process[j + 1] = pro;
            }
        }
    }
}
void SortbyPriorty(Process *process, int First, int 
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值