您好,欢迎来到画鸵萌宠网。
搜索
您的当前位置:首页动态高优先权优先

动态高优先权优先

来源:画鸵萌宠网
实验一 动态高优先权优先

实验内容:

模拟实现动态高优先权优先(若数值越大优先权越高,每运行一个时间单位优先权-n,若数值越小优先权越高,没运行一个时间单位优先权+n),具体如下:

设置进程体:进程名,进程的到达时间,服务时间,初始优先权,进程状态(W——等待,R

——运行,F——完成),进程间的链接指针

进程初始化:由用户输入进程名、服务时间、初始优先权进行初始化,同时,初始化进程的

状态为W。

显示函数:在进程调度前、调度中和调度后进行显示。 排序函数:对就绪状态的进程按照优先权排序。优先权相同时进入就绪队列时间早的进程在

前。注意考虑到达时间

调度函数:每次从就绪队列队首调度优先权最高的进程执行,状态变化。并在执行一个时间

单位后优先权变化,服务时间变化,状态变化。当服务时间为0时,状态变为F。

删除函数:撤销状态为F的进行。 实验要求:

1、 测试数据可以随即输入或从文件中读入。 2、 必须要考虑到进程的到达时间

3、 最终能够计算每一个进程的周转时间的带权周转时间。

实验代码:

#include #include #include using namespace std; int n; class PCB {

public: int pri;

int runtime; int pieceOftime; string procname; string state; int needOftime; int Counter; PCB * next; };

PCB * run = NULL; PCB * ready = NULL; PCB * finish = NULL; PCB * tial = ready; void Dtime(int t); void Prinft(int a)

{

if(a==1) { cout<<\"\\n进程名\到达时间\服务时间\优先权\进程状态\"<void Prinft(int b,PCB * p) {

if(b==1) {

cout<procname<<\"\

\"<needOftime<<\"\\\"<runtime<<\"\\\"<pri<<\"\ } }

void display(int c) {

PCB *p;

if(run!=NULL) Prinft(c,run);

p=ready;

while(p!=NULL) {

Prinft(c,p); p=p->next; }

p=finish;

while(p!=NULL) {

Prinft(c,p); p=p->next; } }

void insert(PCB *p) {

PCB *S1,*S2; if(ready==NULL) {

p->next = NULL; ready = p; } else {

\"<state<S1 = ready; S2 = S1;

while(S1!=NULL) {

if(S1->pri >= p->pri) {

S2 = S1;

S1 = S1->next; } else

break; }

if(S2->pri >= p->pri) {

S2->next = p; p->next = S1; } else {

p->next = ready; ready = p; } } }

bool CTProcessOfPri() {

PCB * Node;

cout <<\"输入创建进程的数目:\"<>n;

for(int j = 0;j < n; j++) {

Node = new PCB; if(Node==NULL) return false; else {

cout <<\"输入进程的名称,进程需CPU时间:\"<>Node->procname>>Node->needOftime; Node->runtime = 0; Node->state =\"就绪\";

Node->pri =Node->needOftime;

cout <<\"进程\"<procname<<\"创建完毕!\"<insert(Node);

}

return true; }

void priority(int i) {

run = ready;

ready = ready->next; run->state = \"运行\"; Prinft(i);

while(run!=NULL) {

run->runtime=run->runtime+1;

run->needOftime=run->needOftime-1; run->pri=run->pri-1; if(run->needOftime==0) {

run->state = \"完成\"; run->next = finish; finish = run; run=NULL;

if(ready!=NULL) {

run = ready;

run->state = \"运行\"; ready = ready->next; } }

else if((ready!=NULL)&&(run->pripri)) {

run->state=\"就绪\"; insert(run); run = ready; run->state = \"运行\"; ready = ready->next; }

display(i); } }

void queue(PCB *p) {

if(ready==NULL) {

p->next = NULL; ready = p;

tial = p; } else {

tial->next = p; tial = p;

p->next = NULL; } }

void Runtime(int c) {

run = ready;

ready = ready->next;

run->state = \"运行\"; Prinft(c);

while(run!=NULL) {

run->runtime=run->runtime+1;

run->needOftime=run->needOftime-1; run->Counter = run->Counter + 1; if(run->needOftime==0) {

run->state = \"完成\"; run->next = finish; finish = run; run = NULL; if(ready!=NULL) {

run = ready;

ready = ready->next; } }

else if(run->Counter == run->pieceOftime) {

run->Counter = 0; run->state = \"就绪\"; queue(run); run=NULL;

if(ready!=NULL) {

run = ready;

run->state = \"运行\"; ready = ready->next;

} }

display(c); } }

int main() {

int i;

cout <<\"----------动态高优先权优先----------\"<cout <<\"--------- 1 开始 2 退出 ---------\\n\\n选择:\"<>i; switch(i) {

case 1:

CTProcessOfPri(); priority(i); break; case 2: break; }

return 0; }

void Dtime(int t) {

time_t current_time; time_t start_time; time(&start_time); do {

time(& current_time);

}while((current_time-start_time)实验结果:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo8.com 版权所有 湘ICP备2023022238号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务