操作系统——进程调度之时间片轮转

 实验源代码:

#include <iostream>
#include<ctime>
using namespace std;

//共有n个进程就绪,m个进程阻塞
//经过t个时间片释放系统资源,唤醒处于阻塞队列队首的进程
/*每个时间片大小为1,进程所需CPU时间为整数*/

struct PCB_type {
	char name;//进程名
	int statr;//进程状态  2执行,1就绪,0阻塞
	int cpu_time;//运行需要CPU时间
};
struct QueueNode {
	PCB_type PCB;
	QueueNode* next;
};
const int T = 5;//每过五个时间片,将阻塞队列中的进程放入就绪队列中
const int Timeslice = 2;//一个时间片大小
const int N = 5;//N为所有PCB进程的个数,0<N<26,对PCB进程命名为26个英文字母
double cpu_whole = 0;//总共用了多少个时间片
double cpu_free = 0;//空闲的时间片
QueueNode* ready_head = new QueueNode();//就绪队列头节点
QueueNode* blocked_head = new QueueNode();//阻塞队列头节点
QueueNode* node_tail = new QueueNode();//两个队列的尾部节点
//向队列尾部插入节点
void addNode(PCB_type pcb)
{
	QueueNode* Queue=node_tail;
	if (pcb.statr == 1)
	{
		Queue = ready_head;//就绪
	}
	else if (pcb.statr == 0)
	{
		Queue = blocked_head;//阻塞
	}
	//暂时不处理进程处于执行状态
	else
	{
		cout << "进程正在执行" << endl;
	}
	while (true)
	{
		if (Queue->next == node_tail)
		{
			break;
		}
		Queue = Queue->next;
	}
	QueueNode* Q = new QueueNode();
	Q->PCB = pcb;
	Q->next = Queue->next;
	Queue->next = Q;
}
//取出节点
//n=1取出就绪队列中节点执行后放入就绪队列末尾或者直接释放,
//n=2取出阻塞队列中节点放入就绪队列末尾
void delNode(int n)
{
	//PCB_type PCB;
	if (n == 1)
	{
		//取出就绪队列中节点,判断进程是否执行完,
		//未执行完则放入就绪队列末尾,否则删除
		//判断就绪队列是否为空,为空时不做操作
		if (ready_head->next != node_tail)
		{
			//减去一个时间片大小
			if ((ready_head->next->PCB.cpu_time-Timeslice) > 0)
			{
				//在时间片执行完后剩余运行所需CPU时间>0,将进程放入就绪队列末尾
				ready_head->next->PCB.cpu_time -= Timeslice;
				addNode(ready_head->next->PCB);
				ready_head->next = ready_head->next->next;
			}
			else
			{
				//在时间片执行完后进程结束,销毁进程
				ready_head->next = ready_head->next->next;
			}
		}
		else
		{
			cpu_free++;
		}
	}
	else if(n==2)
	{
		//将阻塞队列头部的进程放入就绪队列末尾
		//阻塞队列头部后移一个
		//判断阻塞队列是否为空,若为空则不操作
		if (blocked_head->next != node_tail)
		{
			blocked_head->next->PCB.statr = 1;
			addNode(blocked_head->next->PCB);
			blocked_head->next = blocked_head->next->next;
		}
	}
	else
	{
		cout << "参数错误" << endl;
	}
}
void out()//输出两个队列中所有的进程
{
	QueueNode* Queue = new QueueNode();
	cout << "就绪队列" << endl;
	Queue = ready_head;
	while (Queue->next!=node_tail)
	{
		cout << Queue->next->PCB.name <<"剩余长度"<<Queue->next->PCB.cpu_time<< endl;
		Queue = Queue->next;
	}
	cout << "阻塞队列" << endl;
	Queue = blocked_head;
	while (Queue->next != node_tail)
	{
		cout << Queue->next->PCB.name << endl;
		Queue = Queue->next;
	}
	cout << endl;
}
void start()
{
	ready_head->next = node_tail;
	blocked_head->next = node_tail;
	//对PCB进行初始化
	//使用PCB数组进行存储全部进程
	srand(time(0));//随机数种子
	PCB_type pcb[N];//定义N个PCB进程
	char Name = 'A';
	string temp = "阻塞";
	cout << "全部PCB进程如下" << endl;
	//创建PCB进程
	for (int i = 0;i < N;i++)
	{
		pcb[i].cpu_time = rand() % 10 + 1;
		pcb[i].statr = rand() % 2 + 0;//进程状态不考虑执行状态即状态码不为2
		pcb[i].name = Name;
		Name++;
		if (pcb[i].statr == 1)
		{
			temp = "就绪";
		}
		cout << "进程名称" << pcb[i].name << "进程所需CPU时间" << pcb[i].cpu_time << "进程状态" << temp << endl;
	}
	for (int i = 0;i < N;i++)
	{
		addNode(pcb[i]);
	}
	out();
}
int main()
{
	int a = 0;
	start();
	while (ready_head->next != node_tail && blocked_head != node_tail)
	{
		for (int i = 0;i < 5;i++)
		{
			delNode(1);
			if (ready_head->next == node_tail && blocked_head == node_tail)
			{
				//就绪队列和阻塞队列都空
				break;
			}
			cpu_whole++;
		}
		delNode(2);
		out();
	}
	cout << "CPU总占用时间" << cpu_whole << "CPU空闲时间" << cpu_free << endl;
	cout << "CPU利用率" << 1 - (cpu_free / cpu_whole) << endl;
}

 

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值