多级队列调度算法可视化界面_模拟多级反馈队列调度算法原理(源程序c++)

本文提供了一个C++程序,用于可视化演示多级队列调度算法的工作原理。程序接受用户输入的进程信息,包括名称、优先级、运行时间等,并根据优先级将进程分配到不同队列。通过模拟运行过程,展示每个时刻的进程状态,包括当前运行、就绪和等待的进程。
摘要由CSDN通过智能技术生成

#include "stdio.h"

#include

#include

#define getpch(type) (type*)malloc(sizeof(type))

#define NULL 0

int time[3];

struct program {

char name[10];

char state;

int queue;//进程队列

int

priority; // 数字越小优先级越高

int needtime;//需运行时间

int runtime; //已经运行时间

struct program *link;

}*ready=NULL;

typedef struct program PROGRAM;

PROGRAM

*run=NULL,*head1=NULL,*head2=NULL,*head3=NULL,*end1=NULL,*end2=NULL,*end3=NULL;

void sort(PROGRAM *p)

{switch(p->queue)

{case 1:

{if(head1==NULL) {head1=p;end1=p;}

else

{end1->link=p;end1=p;p->link=NULL;}

p->state='w';

break;

}

case 2:

{if(head2==NULL)

{head2=

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多级反馈队列调度算法是一种常见的进程调度算法,它将进程按照优先级划分为多个队列,并且每个队列都有一个时间片大小,优先级高的队列时间片小,优先级低的队列时间片大。当一个进程进入队列时,它被放置在最高优先级的队列中,如果它在该队列中运行完了时间片,但是还没有完成,则它会被移到下一个优先级的队列中,以此类推,直到完成或者到达最低优先级的队列。 下面是一个简单的多级反馈队列调度算法C++实现: ```c++ #include <iostream> #include <queue> using namespace std; struct Process { int pid; // 进程ID int priority; // 进程优先级 int burst_time; // 进程执行时间 }; int main() { // 创建5个队列 queue<Process> q0, q1, q2, q3, q4; // 初始化进程 Process p1 = {1, 0, 8}; Process p2 = {2, 1, 10}; Process p3 = {3, 2, 6}; Process p4 = {4, 3, 4}; Process p5 = {5, 4, 2}; // 将进程放入第0个队列 q0.push(p1); q0.push(p2); q0.push(p3); q0.push(p4); q0.push(p5); // 模拟调度过程 int time = 0; while (!q0.empty() || !q1.empty() || !q2.empty() || !q3.empty() || !q4.empty()) { if (!q0.empty()) { Process p = q0.front(); q0.pop(); cout << "Time " << time << ": Running process " << p.pid << " in queue 0" << endl; p.burst_time -= 1; time += 1; if (p.burst_time > 0) { q1.push(p); } } else if (!q1.empty()) { Process p = q1.front(); q1.pop(); cout << "Time " << time << ": Running process " << p.pid << " in queue 1" << endl; p.burst_time -= 1; time += 2; if (p.burst_time > 0) { q2.push(p); } } else if (!q2.empty()) { Process p = q2.front(); q2.pop(); cout << "Time " << time << ": Running process " << p.pid << " in queue 2" << endl; p.burst_time -= 1; time += 4; if (p.burst_time > 0) { q3.push(p); } } else if (!q3.empty()) { Process p = q3.front(); q3.pop(); cout << "Time " << time << ": Running process " << p.pid << " in queue 3" << endl; p.burst_time -= 1; time += 8; if (p.burst_time > 0) { q4.push(p); } } else if (!q4.empty()) { Process p = q4.front(); q4.pop(); cout << "Time " << time << ": Running process " << p.pid << " in queue 4" << endl; p.burst_time -= 1; time += 16; } } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值