操作系统进程调度之分时,优先,分时优先

process.h

//
// Created by youlingdada on 2021/5/9.
//

/**
 * 模拟进程调度
 */
#include <iostream>
#include <queue>
#include <cstring>
#include <fstream>

#ifndef DEMO_PROCESS_H
#define DEMO_PROCESS_H
#endif //DEMO_PROCESS_H

#define PRO_NUMBER 8   // 进程数量
#define TIME_PAGE 2  // 时间片大小

using namespace std;

// 进程的状态
enum STATUS {
    PRO_WAIT,   // 等待
    PRO_READY,   // 准备
    PRO_RUN,    // 运行
    PRO_FINISH  // 完成
};


// 进程
typedef struct {
    int id;
    int priority;  // 优先级
    int cup_time;      // 占用cpu的时间
    int all_time;      // 整个进程所需要的时间
    int arrived_time;  // 进程到达时间
    int remain_time;   // 进程剩余时间
    int finish_time;   // 进程完成时间
    STATUS status;     // 进程状态
} Process;


// 仿函数 设定优先等待队列的比较规则 按时间先后排序
typedef struct {
    bool operator()(Process process1, Process process2) {
        return process1.arrived_time > process2.arrived_time;
    }
} functional_wait_queue;

//仿函数 设定优先就绪队列的比较规则 按优先级排序
typedef struct {
    bool operator()(Process process1, Process process2) {
        return process1.priority > process2.priority;
    }
} functional_priority_queue;

// 普通就绪队列
queue<Process> pro_ready_queue;  // 就绪队列
priority_queue<Process, vector<Process>, functional_priority_queue> pro_ready_priority_queue; // 优先队列

int os_time;  // 系统时间

priority_queue<Process, vector<Process>, functional_wait_queue> pro_wait_queue;


// 初始化函数 给等待队列赋予初值
void init(const string &init_data_path) {
    os_time = 0;
    Process pro;
    ifstream in;
    in.open(init_data_path);
    while (pro_wait_queue.size() < PRO_NUMBER) {
        in >> pro.id;
        in >> pro.priority;
        in >> pro.all_time;
        in >> pro.arrived_time;
        pro.cup_time = 0;
        pro.remain_time = pro.all_time;
        pro.finish_time = 0;
        pro.status = PRO_WAIT;
        pro_wait_queue.push(pro);
    }
}

// 进程进入就绪队列
void ready(Process &pro);

// 进程运行
void run(Process &pro);

// 进程结束
void finish(Process &pro) {
    if (pro.remain_time == 0) {
        pro.status = PRO_FINISH;
        pro.finish_time = os_time;
        cout << pro.id << " complete and finish time is " << pro.finish_time << endl;
    }
}

// 进程生命周期函数
void pro_run();

// 如果进程运行完了,后面的进程还没有到,直接将os_time调到下一个进程到达时间
void first() {
    if (!pro_wait_queue.empty()) os_time = pro_wait_queue.top().arrived_time;
}

// 结果输出到文件
void output_to_file(const char *output_data_path);

分时调度

//
// Created by youlingdada on 2021/5/9.
//

#include "process.h"

using namespace std;

void ready(Process &pro) {
    pro.status = PRO_READY;
    pro_ready_queue.push(pro);
}

void run(Process &pro) {
    pro.status = PRO_RUN;

    if (pro.remain_time < TIME_PAGE) {
        pro.cup_time += pro.remain_time;
        os_time += pro.remain_time;
        pro.remain_time = 0;
    } else {
        os_time += TIME_PAGE;
        pro.cup_time += TIME_PAGE;
        pro.remain_time -= TIME_PAGE;
    }

    cout << pro.id << " run and remain time is " << pro.remain_time << "----- os time is : " << os_time << endl;
    pro.status = PRO_WAIT;
}


void pro_run() {
    Process temp, pro;
    temp.status = PRO_FINISH;
    while (true) {
        if (pro_wait_queue.empty() && pro_ready_queue.empty() && temp.status == PRO_FINISH) break;

        while (!pro_wait_queue.empty() && os_time >= pro_wait_queue.top().arrived_time) {
            pro = pro_wait_queue.top();
            ready(pro);
            pro_wait_queue.pop();
        }

        if (temp.remain_time > 0 && temp.status == PRO_WAIT) ready(temp);
        if (pro_ready_queue.empty()) first();

        if (!pro_ready_queue.empty())temp = pro_ready_queue.front();
        else exit(0);

        run(temp);
        finish(temp);

        pro_ready_queue.pop();
    }
}

int main() {
    init("init_data.txt");
    pro_run();
    cout << "over" << endl;
}

非抢占式的优先级调度

//
// Created by youlingdada on 2021/5/9.
//

/**
 * 非抢占式的优先
 */

#include "process.h"

void ready(Process &pro) {
    pro.status = PRO_READY;
    pro_ready_priority_queue.push(pro);
}

void run(Process &pro) {
    pro.status = PRO_RUN;
    pro.remain_time = 0;
    pro.cup_time = pro.all_time;
    os_time += pro.all_time;
}


void pro_run() {
//    定义两个进程变量
    Process temp, pro;

    while (true) {
//        如果进程全部完成的情况,就跳出循环
        if (pro_wait_queue.empty() && pro_ready_priority_queue.empty() && temp.status == PRO_FINISH) break;

        while (!pro_wait_queue.empty() && os_time >= pro_wait_queue.top().arrived_time) {
            pro = pro_wait_queue.top();
            ready(pro);
            pro_wait_queue.pop();
        }

        if (pro_ready_priority_queue.empty()) first();

        temp = pro_ready_priority_queue.top();
        run(temp);

        finish(temp);
        pro_ready_priority_queue.pop();

    }

}

int main() {
    init("init_data.txt");
    pro_run();
    cout << "over" << endl;
}

分时优先调度

//
// Created by youlingdada on 2021/5/10.
//

#include "process.h"

void ready(Process &pro) {
    pro.status = PRO_READY;
    pro_ready_priority_queue.push(pro);
}

void run(Process &pro) {
    pro.status = PRO_RUN;

    if (pro.remain_time < TIME_PAGE) {
        pro.cup_time += pro.remain_time;
        os_time += pro.remain_time;
        pro.remain_time = 0;
    } else {
        os_time += TIME_PAGE;
        pro.cup_time += TIME_PAGE;
        pro.remain_time -= TIME_PAGE;
    }

    cout << pro.id << " run and remain time is " << pro.remain_time << "----- os time is : " << os_time << endl;
    pro.status = PRO_WAIT;
}


void pro_run() {
    Process temp, pro;
    temp.status = PRO_FINISH;
    while (true) {
        if (pro_wait_queue.empty() && pro_ready_priority_queue.empty() && temp.status == PRO_FINISH) break;

        while (!pro_wait_queue.empty() && os_time >= pro_wait_queue.top().arrived_time) {
            pro = pro_wait_queue.top();
            ready(pro);
            pro_wait_queue.pop();
        }

        if (os_time >= TIME_PAGE && temp.remain_time > 0 && temp.status == PRO_WAIT) ready(temp);
        if (pro_ready_priority_queue.empty()) first();

        if (!pro_ready_priority_queue.empty()) temp = pro_ready_priority_queue.top();
        else exit(0);


        run(temp);
        finish(temp);

        pro_ready_priority_queue.pop();
    }
}

int main() {
    init("init_data.txt");
    pro_run();
    cout << "over" << endl;
}

输入数据init_data.txt 文件

1 1 5 0
2 1 7 4
3 2 5 2
4 2 7 1
5 2 5 0
6 4 7 1
7 3 5 0
8 2 7 1

操作系统进程调度之分时,优先,分时优先

上一篇:匿名函数 | Python从入门到精通:高阶篇之十四


下一篇:UVa 1502 GRE Words (DP+AC自动机+jry线段树)