3.24进程调度模拟算法

一、实验目的

通过编写模拟进程调度程序,了解进程控制块、进程队列等基本概念,体会优先数算法和时间片轮转算法的具体实施过程,掌握进程调度的核心机制,提高对处理机管理的理解和应用能力。

二、实验要求

    1.设计进程控制块 PCB 的结构,通常应包括如下信息:

进程名、进程优先数 (或轮转时间片数)、  进程已占用的 CPU 时间、进程到完成还需要 的时间、 进程的状态、  当前队列指针等。

2.

优先数调度算法程序

循环轮转调度算法程序

 

三、实验过程

1.准备

A. 查阅相关资料:

B. 初步编写程序:

C. 准备测试数据:

2.上机调试

 

3. 主要流程和源代码

       #include <iostream>

#include <string>

#include <queue>

#include <vector>

using namespace std;

 

// 定义进程控制块PCB结构

struct PCB {

    string name;          // 进程名

    int prio;             // 优先数

    int cputime;          // 已占用的CPU时间

    int needtime;         // 还需要的时间

    char state;           // 进程状态: R(运行), W(就绪), F(完成)

    PCB* next;            // 指向下一个PCB的指针

 

    PCB(string n, int p, int c, int ne, char s) : name(n), prio(p), cputime(c), needtime(ne), state(s), next(nullptr) {}

};

 

// 全局指针

PCB* run = nullptr;       // 当前运行进程指针

PCB* readyHead = nullptr; // 就绪队列头指针

PCB* readyTail = nullptr; // 就绪队列尾指针

PCB* finishHead = nullptr;// 完成队列头指针

 

// 函数声明

void insert1(PCB* pcb);   // 优先数算法中插入就绪队列

void insert2(PCB* pcb);   // 时间片轮转法中插入就绪队列尾部

void firstIn();           // 调度就绪队列的第一个进程投入运行

void printStatus();       // 显示所有进程的状态

PCB* createProcess(string name, int needtime); // 创建新进程

void priSched();          // 优先数调度算法

void roundSched();        // 时间片轮转调度算法

 

int main() {

    int choice;

    cout << "请选择调度算法: 1. 优先数调度算法  2. 时间片轮转调度算法" << endl;

    cin >> choice;

 

    int numProcesses;

    cout << "请输入进程数量: ";

    cin >> numProcesses;

 

    for (int i = 0; i < numProcesses; ++i) {

        string name;

        int needtime;

        cout << "请输入进程名和所需时间: ";

        cin >> name >> needtime;

        PCB* newProcess = createProcess(name, needtime);

        if (choice == 1) {

            insert1(newProcess);

        } else if (choice == 2) {

            insert2(newProcess);

        }

    }

 

    while (run || readyHead) {

        if (choice == 1) {

            priSched();

        } else if (choice == 2) {

            roundSched();

        }

        printStatus();

    }

 

    return 0;

}

 

// 插入优先数算法的就绪队列

void insert1(PCB* pcb) {

    PCB* temp = readyHead;

    if (!temp || pcb->prio > temp->prio) {

        pcb->next = readyHead;

        readyHead = pcb;

        if (!readyTail) readyTail = pcb;

    } else {

        while (temp->next && temp->next->prio >= pcb->prio) {

            temp = temp->next;

        }

        pcb->next = temp->next;

        temp->next = pcb;

        if (!temp->next) readyTail = pcb;

    }

}

 

// 插入时间片轮转法的就绪队列尾部

void insert2(PCB* pcb) {

    if (!readyTail) {

        readyHead = readyTail = pcb;

    } else {

        readyTail->next = pcb;

        readyTail = pcb;

    }

}

 

// 调度就绪队列的第一个进程投入运行

void firstIn() {

    if (readyHead) {

        run = readyHead;

        readyHead = readyHead->next;

        if (!readyHead) readyTail = nullptr;

        run->state = 'R';

    }

}

 

// 显示所有进程的状态

void printStatus() {

    cout << "name\tcputime\tneedtime\tpriority\tstate" << endl;

    // 显示运行态

    if (run) {

        cout << run->name << "\t" << run->cputime << "\t" << run->needtime << "\t" << run->prio << "\tR" << endl;

    }

    // 显示就绪态

    PCB* temp = readyHead;

    while (temp) {

        cout << temp->name << "\t" << temp->cputime << "\t" << temp->needtime << "\t" << temp->prio << "\tW" << endl;

        temp = temp->next;

    }

    // 显示完成态

    temp = finishHead;

    while (temp) {

        cout << temp->name << "\t" << temp->cputime << "\t" << temp->needtime << "\t" << temp->prio << "\tF" << endl;

        temp = temp->next;

    }

}

 

// 创建新进程

PCB* createProcess(string name, int needtime) {

    int prio = 50 - needtime; // 优先数计算

    return new PCB(name, prio, 0, needtime, 'W');

}

 

// 优先数调度算法

void priSched() {

    if (!run) firstIn(); // 如果没有运行的进程,调度就绪队列的第一个进程

    if (run) {

        run->cputime++;

        run->prio--;

        run->needtime--;

        if (run->needtime == 0) { // 如果进程完成

            run->state = 'F';

            PCB* temp = run;

            run = nullptr;

            temp->next = finishHead;

            finishHead = temp;

        }

    }

}

 

// 时间片轮转调度算法

void roundSched() {

    if (!run) firstIn(); // 如果没有运行的进程,调度就绪队列的第一个进程

    if (run) {

        run->cputime += 2;

        run->needtime -= 2;

        if (run->needtime <= 0) { // 如果进程完成

            run->state = 'F';

            PCB* temp = run;

            run = nullptr;

            temp->next = finishHead;

            finishHead = temp;

        } else { // 否则将进程放回就绪队列尾部

            run->state = 'W';

            insert2(run);

            run = nullptr;

        }

    }

}

       

4.遇到的主要问题和解决方法

**问题**:优先数调度和时间片轮转调度逻辑混淆,状态更新和链表操作易出错。

 

**解决方法**:明确算法逻辑,独立封装调度函数;仔细检查链表指针操作,调试输出验证;严格定义状态转换条件,确保状态更新正确。

四、实验结果

    

 

posted @ 2025-06-20 00:47  jais  阅读(14)  评论(0)    收藏  举报