|
【实验题目】 (1)用 C++ 语言来实现对 N 个进程采用动态优先权优先算法的进程调度。 (2)每个用来标识进程的进程控制块 PCB 用结构来描述,包括以下字段: • 进程标识数 ID。 • 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。 • 进程已占用的 CPU 时间 CPUTIME。 • 进程还需占用的 CPU 时间 ALLTIME。当进程运行完毕时,ALLTIME 变为 0。 • 进程的阻塞时间 STARTBLOCK,表示当进程再运行 STARTBLOCK 个时间片 后,将进入阻塞状态。 • 进程被阻塞的时间 BLOCKTIME,表示已阻塞的进程再等待 BLOCKTIME 个 时间片后,将转换成就绪状态。 • 进程状态 STATE。 • 队列指针 NEXT,用来将 PCB 排成队列。 (3)优先数改变的原则: • 进程在就绪队列中呆一个时间片,优先数加 1。 • 进程每运行一个时间片,优先数减 3。 【输入形式】 input.txt传入格式如下(属性之间以空格分隔): | ID | PRIORITY | CPUTIME | ALLTIME | STARTBLOCK | BLOCKTIME | 【输出形式】 先后调度进程的ID |
【实验核心代码】
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int TIME_SLICE = 1; // 时间片长度
enum State { READY, RUNNING, BLOCKED };
struct PCB {
int id;
int priority;
int cpuTime;
int allTime;
int startBlock;
int blockTime;
State state;
PCB* next;
PCB(int _id, int _priority, int _cputime, int _alltime, int _startblock, int _blocktime)
: id(_id), priority(_priority), cpuTime(_cputime), allTime(_alltime), startBlock(_startblock), blockTime(_blocktime), state(READY), next(nullptr) {}
};
class Scheduler {
public:
void addProcess(PCB* process) {
if (readyQueue.empty()) {
readyQueue.push_back(process);
} else {
auto it = find_if(readyQueue.begin(), readyQueue.end(),
[process](PCB* p) { return p->priority < process->priority; });
readyQueue.insert(it, process);
}
}
void readProcesses(const string& filename) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Failed to open file." << endl;
return;
}
int id, priority, cputime, alltime, startblock, blocktime;
while (file >> id >> priority >> cputime >> alltime >> startblock >> blocktime) {
addProcess(new PCB(id, priority, cputime, alltime, startblock, blocktime));
}
file.close();
}
void run() {
while (!readyQueue.empty() || !blockedQueue.empty()) {
if (!readyQueue.empty()) {
PCB* current = readyQueue.front();
readyQueue.erase(readyQueue.begin());
if (current->state == READY) {
current->state = RUNNING;
current->cpuTime += TIME_SLICE;
current->priority -= 3; // 每运行一个时间片,优先级减少3
if (current->allTime > 0) {
current->allTime -= TIME_SLICE;
}
if (current->startBlock > 0) {
current->startBlock -= TIME_SLICE;
}
if (current->allTime <= 0) {
cout << current->id << " ";
delete current;
continue;
}
if (current->startBlock == 0) {
current->state = BLOCKED;
current->blockTime = 3; // 假设阻塞时间为3个时间片
blockedQueue.push_back(current);
continue;
}
current->state = READY;
addProcess(current); // 重新加入就绪队列,可能因为优先级变化而位置不同
}
}
if (!blockedQueue.empty()) {
for (auto it = blockedQueue.begin(); it != blockedQueue.end(); ) {
PCB* process = *it;
if (process->state == BLOCKED) {
process->blockTime -= TIME_SLICE;
if (process->blockTime <= 0) {
process->state = READY;
process->priority += 1; // 每等待一个时间片,优先级增加1
addProcess(process);
it = blockedQueue.erase(it);
} else {
++it;
}
}
}
}
// 每个时间片结束时,更新就绪队列中的优先级
for (auto process : readyQueue) {
if (process->state == READY) {
process->priority += 1;
}
}
// 重新排序就绪队列
sort(readyQueue.begin(), readyQueue.end(), [](PCB* a, PCB* b) {
return a->priority > b->priority;
});
}
}
private:
vector<PCB*> readyQueue;
vector<PCB*> blockedQueue;
};
int main() {
Scheduler scheduler;
scheduler.readProcesses("input.txt");
// 开始调度
scheduler.run();
return 0;
}
231

被折叠的 条评论
为什么被折叠?



