操作系统-处理机调度之进程调度

三种算法的模拟:

1、先来先服务算法

2、循环轮转算法

3、多级队列反馈调度算法

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

struct PCB {
    int id;
    int arrive_time;
    int run_time;
    int need_time;
    int finish_time;
    PCB* next;
};

queue<PCB> coming_queue;
queue<PCB> wait_queue;
queue<PCB> wait_queue1;
queue<PCB> wait_queue2;

void init_processes() {
    int n;
    cout << "输入进程数: ";
    cin >> n;

    vector<PCB> temp_queue; 

    for (int i = 0; i < n; ++i) {
        PCB pcb;
        pcb.id = i + 1;
        cout << "输入进程 " << pcb.id << " 的到达时间和运行时间: ";
        cin >> pcb.arrive_time >> pcb.run_time;
        pcb.finish_time = 0;
        pcb.need_time = pcb.run_time;
        pcb.next = nullptr;

        auto it = temp_queue.begin();
        while (it != temp_queue.end() && it->arrive_time <= pcb.arrive_time) {
            ++it;
        }
        temp_queue.insert(it, pcb);
    }
    for (const auto& pcb : temp_queue) {
        coming_queue.push(pcb);
    }
}

void print_process_stats(const vector<PCB>& finished_processes) {
    int total_turnaround_time = 0;
    cout << "ID\t到达时间\t执行时间\t结束时间\t周转时间\n";
    for (const auto& pcb : finished_processes) {
        int turnaround_time = pcb.finish_time - pcb.arrive_time;
        total_turnaround_time += turnaround_time;
        cout << pcb.id << "\t" << pcb.arrive_time << "\t\t" << pcb.run_time << "\t\t"
            << pcb.finish_time << "\t\t" << turnaround_time << "\n";
    }
    cout << "平均周转时间: " << (total_turnaround_time / finished_processes.size()) << "\n";
}

void fcfs() {
    vector<PCB> finished_processes;
    int current_time = 0;

    while (!coming_queue.empty()) {
        PCB pcb = coming_queue.front();
        coming_queue.pop();

        if (current_time < pcb.arrive_time) {
            current_time = pcb.arrive_time;
        }
        current_time += pcb.run_time;
        pcb.finish_time = current_time;
        finished_processes.push_back(pcb);
    }

    print_process_stats(finished_processes);
}

void rr(int time_slice) {
    vector<PCB> finished_processes;
    int current_time = coming_queue.front().arrive_time;

    // 模拟进程的到达,将到达时间在当前时间之前的进程放入等待队列
    while (!coming_queue.empty() && coming_queue.front().arrive_time <= current_time) {
        wait_queue.push(coming_queue.front());
        coming_queue.pop();
    }

    // 循环执行直到等待队列为空
    while (!wait_queue.empty()) {
        PCB pcb = wait_queue.front(); // 取出等待队列中的第一个进程
        wait_queue.pop();

        if (pcb.need_time <= time_slice) {
            current_time += pcb.need_time; // 运行完当前时间片
            pcb.finish_time = current_time; // 记录完成时间
            finished_processes.push_back(pcb); // 将进程加入已完成进程列表
        }
        else {
            current_time += time_slice; // 未运行完一个时间片,更新当前时间
            pcb.need_time -= time_slice; // 减去已运行的时间
            // 将未完成的进程重新放回等待队列
            while (!coming_queue.empty() && coming_queue.front().arrive_time <= current_time) {
                wait_queue.push(coming_queue.front());
                coming_queue.pop();
            }
            wait_queue.push(pcb); // 将当前进程放回等待队列
        }
    }
    print_process_stats(finished_processes); // 打印进程统计信息
}

void mlfq(int time_slice) {
    const int num_queues = 2; // 定义队列的数量
    queue<PCB> wait_queues[num_queues]; // 创建多个等待队列
    vector<PCB> finished_processes;
    int current_time = coming_queue.front().arrive_time;
    while (true) {
        // 将到达的进程放入第一个等待队列中
        while (!coming_queue.empty() && coming_queue.front().arrive_time <= current_time) {
            wait_queues[0].push(coming_queue.front());
            coming_queue.pop();
        }

        bool over = true;    //true表示进程都执行完

        // 从最高优先级的队列开始处理
        for (int i = 0; i < num_queues; ++i) {
            if (!wait_queues[i].empty()) {
                over = false;
                PCB pcb = wait_queues[i].front(); // 取出队首进程
                wait_queues[i].pop();

                if (current_time < pcb.arrive_time) {
                    current_time = pcb.arrive_time;
                }
                //时间更新
                int exec_time = min(time_slice, pcb.need_time);
                current_time += exec_time;
                pcb.need_time -= exec_time;

                // 将到达的进程放入第一个等待队列中
                while (!coming_queue.empty() && coming_queue.front().arrive_time <= current_time) {
                    wait_queues[0].push(coming_queue.front());
                    coming_queue.pop();
                }

                if (pcb.need_time > 0) {
                    // 如果进程没有完成,降级到下一个优先级的队列
                    if (i < num_queues - 1) {
                        wait_queues[i + 1].push(pcb);
                    }
                    else {
                        wait_queues[i].push(pcb); // 保持在当前队列
                    }
                }
                else {
                    // 进程完成
                    pcb.finish_time = current_time;
                    finished_processes.push_back(pcb);
                }

                break; // 处理一个进程后,跳出内循环重新检查所有队列
            }
        }

        // 如果所有队列都为空并且没有新进程到达,跳出循环
        if (over && coming_queue.empty()) {
            break;
        }

        // 如果所有队列都为空但还有新进程未到达,推进时间到下一个进程到达的时间
        if (over && !coming_queue.empty()) {
            current_time = coming_queue.front().arrive_time;
        }
    }

    print_process_stats(finished_processes); // 打印进程统计信息
}

void menu() {
    cout << "选择调度算法:\n";
    cout << "1. 先来先服务 (FCFS)\n";
    cout << "2. 轮转法 (时间片 = 2)\n";
    cout << "3. 多级反馈队列 (时间片 = 2)\n";
    cout << "4. 退出\n";
    cout << "输入你的选择: ";
}

int main() {
    int choice;
    while (true) {
        init_processes();
        menu();
        cin >> choice;

        switch (choice) {
        case 1:
            fcfs();
            system("pause");
            system("cls");
            break;
        case 2:
            rr(2);
            system("pause");
            system("cls");
            break;
        case 3:
            mlfq(2);
            system("pause");
            system("cls");
            break;
        case 4:
            return 0;
        default:
            cout << "无效选择。请重试。\n";
        }
    }
    return 0;
}

运行截图:

先来先服务算法:

轮转法:

多级队列反馈队列:

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值