操作系统实验-基于优先数的时间片轮转调度算法调度处理器

#include "pch.h"
#include <iostream>
#include "stdio.h"
#include "string.h" 
#define num 3//假定系统中进程个数为5
using namespace std;
struct PCB
{
	char ID;//进程名
	int runtime;//要求运行时间
	int pri;//优先数
	char state; //状态,R-就绪,F-结束
};
struct PCB  pcblist[num];//定义进程控制块数组
int max_pri_process(int key)//确定最大优先级进程子程序
{
	int min = pcblist[key].pri;
	int i = 0, j = 0;
	int key1 = 0;
	if (pcblist[key].runtime<=0)
	{
		min = 999999;
		for (int i = 0; i < num; i++)
		{
			if (pcblist[i].pri <= min && pcblist[i].state == 'R') {
				key1 = i;
				min = pcblist[i].pri;
			}
			if (pcblist[i].state == 'F') {
				j = j + 1;
			}
		}
	}
	else
	{
		for (i = 0; i < num; i++) {
			if (pcblist[i].pri <= min && pcblist[i].state == 'R') {
				key1 = i;
				min = pcblist[i].pri;
			}
			if (pcblist[i].state == 'F') {
				j = j + 1;
			}
		}
	}
	if (j >= num) {
		return -1;
	}
	else {
		pcblist[key1].pri++;
		pcblist[key1].runtime--;
		return key1;
	}
}
void show()//显示子程序

{
	int i;
	printf("\n 进程名 优先数 要求运行时间 状态 \n");
	printf("-------------------------------------------------\n");
	for (i = 0; i < num; i++)//依次显示每个进程的名、优先数、要求运行时间和状态
	{
		printf("%6s%8d%8d%8s\n", &pcblist[i].ID, pcblist[i].pri, pcblist[i].runtime, &pcblist[i].state);
	}
	printf(" pressany key to continue...\n");

}
void update(int key) {
	for (int i = 0; i < num; i++)
	{
		if (pcblist[i].runtime <= 0)
		{
			pcblist[i].state = 'F';
		}
		else
		{
			pcblist[i].state = 'R';
		}
	}
	show();
	int addr = max_pri_process(key);
	if (addr == -1)
	{
		return;
	}
	else
	{
		update(addr);
	}
}
void  init()//PCB初始化子程序
{
	int i;
	for (i = 0; i < num; i++)

	{
		printf("PCB[%d]:进程名 优先数 要求运行时间 \n", i + 1);//为每个进程任意指定pri和runtime
		cin >> pcblist[i].ID;
		cin >> pcblist[i].pri;
		cin >> pcblist[i].runtime;
		pcblist[i].state = 'R';//进程初始状态均为就绪
		getchar();//接收回车符
	}

	int key = max_pri_process(0);
	update(key);
	getchar();
}
int main()//按动态优先数调度主程序
{
	init();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值