#include "pch.h"
#include <iostream>
#include "stdio.h"
#include "string.h"
#define num 3
using namespace std;
struct PCB
{
char ID;
int runtime;
int pri;
char state;
};
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()
{
int i;
for (i = 0; i < num; i++)
{
printf("PCB[%d]:进程名 优先数 要求运行时间 \n", i + 1);
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();
}