系统相关
首页 > 系统相关> > 进程调度模拟程序

进程调度模拟程序

作者:互联网

#include <windows.h>
#include <iostream>

using namespace std;

const int PROCESS_NUMBER = 5;//进程数量
const int TIMESLICE = 100;//时间片长度
const int MIN = -999;//最小优先级

//进程状态
enum process_state
{
ready,//就绪
execute,//执行
block,//阻塞
finish//完成
};

//进程控制块
struct pcb
{
char Name[10];//进程名
int Priority;//优先级
int Total_Time;//运行总时间
int CPU_Time;//CPU运行时间
process_state State;//进程状态
int count;//计数器
int round;//时间片
pcb* next;//PCB链表指针
};

//输入PCB的信息
pcb* get_process()
{
//初始化指针
pcb* head, * p, * q;
head = q = nullptr;
int i = 0;
cout << "Please input " << PROCESS_NUMBER << " process names and times: " << endl;
while (i < PROCESS_NUMBER)
{
//动态内存分配指针的
p = new pcb;
cin >> p->Name;
cin >> p->Total_Time;
p->CPU_Time = 0;
p->Priority = TIMESLICE - (p->Total_Time - p->CPU_Time);//计算优先级
p->count = 0;
p->round = 0;
p->State = ready;//提前设置就绪状态
p->next = nullptr;
if (i == 0)
{
//设置头指针
head = p;
q = p;
}
else
{
q->next = p;
q = p;
}
i++;
}
return head;
}

//判断进程是否已执行完毕
int is_process_finish(pcb* p)
{
int i = 1;
while (p && i)
{
i = i & ((p->Total_Time - p->CPU_Time) == 0);
p = p->next;
return i;
}
}

//输出优先级调度时进程的状态
void output_priority(pcb* p)
{
cout << "Name\t\tPriority\tTotal Time\tCPU Time\tState" << endl;
while (p)
{
cout << p->Name << "\t\t"
<< p->Priority << "\t\t"
<< p->Total_Time << "\t\t"
<< p->CPU_Time << "\t\t";
switch (p->State)
{
case ready:
cout << "ready";
break;
case execute:
cout << "execute";
break;
case block:
cout << "block";
break;
case finish:
cout << "finish";
break;
}
cout << endl;
p = p->next;
}
}

//执行进程
void cpu_execute(pcb* p)
{
pcb* q = p;
int selected = MIN;
//寻找优先级最大的进程
while (p)
{
//判断是否结束
if (p->State != finish)
{
p->State = ready;
if ((p->Total_Time - p->CPU_Time) == 0)
{
p->State = finish;
}
}
//判断优先级及状态
if (selected < p->Priority && p->State != finish)
{
selected = p->Priority;
q = p;
}
p = p->next;
}
//计算剩余时间
if ((q->Total_Time - q->CPU_Time) != 0)
{
q->Priority -= 3;//优先级减3
q->CPU_Time++;
q->State = execute;
}
}

//优先级调度
void priority_scheduling()
{
pcb* p;
p = get_process();//获取PCB
int time = 0;
//输出PCB以及循环
while (!is_process_finish(p))
{
time++;
cout << "CPU Time: " << time << endl;
cpu_execute(p);
output_priority(p);
Sleep(1000);
cout << endl;
}
cout << "Process finished, program exit." << endl;
system("pause");
exit(0);
}

//设置CPU轮转
void cpu_round(pcb* p)
{
p->CPU_Time += 2;//时间片加2
//判断剩余时间
if ((p->Total_Time - p->CPU_Time) < 0)
{
p->CPU_Time = p->Total_Time;
}
p->count++;
p->round++;
p->State = execute;
}

//获取轮转时下一个PCB
pcb* get_next_process(pcb* p, pcb* head)
{
pcb* q = p;
do
{
q = q->next;
} while (q && q->State == finish);
//若传入的为空指针则取头指针
if (q == NULL)
{
q = head;
while (q->next != p && q->State == finish)
{
q = q->next;
}
}
return q;
}

//更改PCB状态
void change_state(pcb* p)
{
while (p)
{
//判断是否结束
if ((p->Total_Time - p->CPU_Time) == 0)
{
p->State = finish;
}
if (p->State == execute)
{
p->State = ready;
}
p = p->next;
}
}

//显示时间片轮转时的PCB
void output_round(pcb* p)
{
cout << "Name\t\tTotal Time\tCPU Time\tCount\tRound\tState" << endl;
while (p)
{
cout << p->Name << "\t\t"
<< p->Total_Time << "\t\t"
<< p->CPU_Time << "\t\t"
<< p->count << "\t"
<< p->round << "\t";
switch (p->State)
{
case ready:
cout << "ready";
break;
case execute:
cout << "execute";
break;
case block:
cout << "block";
break;
case finish:
cout << "finish";
break;
}
cout << endl;
p = p->next;
}
}

//时间片轮转调度
void round_robin()
{
pcb* p, * q;
p = get_process();
int time = 0;
q = p;
while (!is_process_finish(p))
{
time += 2;
cpu_round(q);
q = get_next_process(q, p);
cout << "CPU Time: " << time << endl;
output_round(p);
change_state(p);
Sleep(1000);
cout << endl;
}
cout << "Process finished, program exit." << endl;
system("pause");
exit(0);
}

//菜单
void menu()
{
cout << "Process scheduling" << endl
<< "1.Priority Scheduling" << endl//优先级调度
<< "2.Round Robin" << endl;//时间片轮转调度
cout << "Please enter the choice: ";
}

int main()
{
menu();
int choice;
cin >> choice;
switch (choice)
{
case 1:
priority_scheduling();
break;
case 2:
round_robin();
break;
default:
cout << "Error, program exit." << endl;
exit(0);
}
return 0;
}

标签:cout,int,调度,pcb,State,模拟程序,Time,进程,CPU
来源: https://www.cnblogs.com/lin19/p/16350197.html