时间片轮转调度算法实现

代码

/*
process:时间片轮转
author:lisuhang
time:2022-4-11
*/
#include<iostream>
#include <fstream>
using namespace std;

int const N = 100;
int i = 0;


//-------------进程--------------------//
//进程
typedef struct
{
    //进程名称
    string name;
    //到达时间
    int arrive_time;
    //完成需要时间
    int time;
}process;


//更新进程时间
void update_process(process& p)
{
    p.time--;
}


//进程是否完成
bool is_finish(process p)
{
    if(p.time)
    {
        return false;
    }
    return true;
}


void print(process p)
{
    cout << "进程" << p.name << "已经完成" <<"    ";
}
//-------------进程--------------------//





//--------------等待队列-----------------//
//等待队列
typedef struct
{
    process p[N];
    int first = 0;
    int last = 0;
}queue;


//插入等待队列
void insert(queue& q, process p)
{
    q.p[q.last++] = p;
    q.last %= N;
}


//离开等待队列
process pop(queue& q)
{
    process p = q.p[q.first++];
    q.first %= N;
    return p;
}


//判断队列是否为空
bool empty(queue q)
{
    return q.first == q.last;
}
//--------------等待队列-----------------//





//--------------pcb---------------------//
//pcb
typedef struct
{
    //正在执行的进程
    process p;
    //该进程已经占用CPU的时间
    int time = 0;
    //等待队列
    queue q;
    //当前进程状态
    int state = 0;
    // 时间片轮转时间
    int const m = 2;
}pcb;


//输出当前pcb信息
void print_pcb(pcb b)
{
    cout << "当前正在执行:" <<b.p.name << "    ";
}


//时间片更新
void update_pcb(pcb& b)
{
    //如果当前正在运行进程
    if(b.state)
    {
        //时间片时间
        b.time++;
        //进程剩余时间
        update_process(b.p);
        //如果进程完成,主动放弃cpu
        if(is_finish(b.p))
        {
            b.state = 0;
            //打印完成信息
            print(b.p);
            b.time = 0;
        }
        //否则检查时间是否到达使用时间
        else if(b.time == b.m)
        {
            //打印进程到期信息
            cout << b.p.name << "限制时间已到" << "    ";
            //进程状态置0
            b.state = 0;
            //未完成的进程继续插入到等待队列中
            insert(b.q, b.p);
            b.time = 0;
        }
        else
        {
            print_pcb(b);
        }
    }
    //如果当前没有运行进程
    if(!b.state)
    {
        //如果等待队列不为空
        if(!empty(b.q))
        {
            b.p = pop(b.q);
            cout << b.p.name << "开始运行" << "    ";
            b.state = 1;
        }
        //如果等待队列为空
        else
        {
            cout << "当前无可执行进程" << "    ";
        }
    }
}
//--------------pcb---------------------//

//待执行进程总数
int n = 0;
//最多运行的时间
int all = 0;
//进程
process p[N];
//读入进程数据
void data()
{
    ifstream readFile("data.txt");
    if(!readFile.is_open())
    {
        cout << "打开数据文件失败" << endl;
        exit(0);
    }
    while(!readFile.eof())
    {
        readFile >> p[n].name >> p[n].arrive_time >> p[n].time;
        if(all > p[n].arrive_time) all += p[n].time;
        else all = p[n].arrive_time + p[n].time;
        n++;
    }
    readFile.close();
}


int main()
{
    int j = 0;
    pcb b;

    data();
    for(; i <= all; i++)
    {
        cout << "第" << i << "时刻" << "    ";
        //进程到达,则进入等待队列
        while(j < n && p[j].arrive_time == i)
        {
            insert(b.q, p[j]);
            cout << p[j].name << "进入等待队列" << "    ";
            j++;
        }
        update_pcb(b);
        cout << endl;
    }
    return 0;
}

数据

p1 0 5
p2 2 4
p3 4 1
p4 5 6
posted @ 2022-04-11 16:54  爱敲代码的自闭杭  阅读(241)  评论(0)    收藏  举报