首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >进程调度时间片轮转例题_进程调度算法java

进程调度时间片轮转例题_进程调度算法java

作者头像
全栈程序员站长
发布2022-11-17 11:26:44
发布2022-11-17 11:26:44
1.5K0
举报

大家好,又见面了,我是你们的朋友全栈君

一、实验目的

(1) 加深对进程的理解

(2) 理解进程控制块的结构

(3) 理解进程运行的并发性

(4) 掌握时间片轮转法进程调度算法

二、实验原理

(1)建立进程控制块

(2)设计两个链队列,分别表示就绪队列和完成队列

(3)用户输入进程标识符,进程到达时间,进程所需的时间,申请空间存放进程,PCB信息。

(4)每一个时间片结束输出各进程的进程标识符,CPU运行时间

,进程所需时间,达到时间,周转时间,以及状态(运行完成或者就绪)

三、实验步骤、数据记录及处理

1.算法流程

本程序中用到抽象数据类型的定义。

代码语言:javascript
复制
struct _PCBNode{	char name[20];       //进程名称	int arrive_time;     //到达时间	int need_time;      //服务时间	int cputime;         //CPU已经执行的时间	int accomplish_Time; //完成时间	int turn_over_time;  //周转时间	bool tag;            //是否完成};  class _PCB{	private:	queue<struct _PCBNode> _PNode;	queue<struct _PCBNode> _CNode;	int TIME;}

代码语言:javascript
复制
 主程序的流程以及各程序模块之间的层次(调用)关系。  

实现概要设计中定义的主要函数,对主要函数写出核心算法(要求注释);并尽可能画程

序流程图)

本程序写着的就绪队列中放着客户外界输入未到达的进程,所以在进行时间片轮转时要判断当前时间和到达时间,到达时间大于当前时间时才能CPU的调度

代码语言:javascript
复制
void Pop()//模仿时间轮转调度
	{
		struct _PCBNode node;
		unsigned int n = _PNode.size();
		unsigned int  i = 0;
		for(; i< n; ++i)// for循环找队列中到达时间大于当前时间时的进程
		{
			node = _PNode.front();_PNode.pop();
			if(node.arrive_time <= TIME)
			{
				break;
			}
			_PNode.push(node);

		}

		TIME = TIME+Time_Pice;//模拟进程运行中
		
		if(i == n)//没找到到达时间大于当前时间时的进程		{
			Display_PCB();
			return ;
		}

		node.cputime +=Time_Pice; //进程占有CPU运行的时间

		if(node.cputime >= node.need_time)//完成运行,进入完成队列
		{
			node.accomplish_Time = TIME;
			node.tag = true;
			node.turn_over_time  = node.accomplish_Time -node.arrive_time ;
			_CNode.push(node);  
			cout<<node.name<<"   run over"<<endl;
		}
		else
		{
			_PNode.push(node);
		}
		Display_PCB(); 
	}

2.运行结果分析

时间片被设置为 2,打印TIME时间时就绪队列和运行完成队列的进程状态

四、总结与体会

通过做本次实验,我模拟了CPU进程调度中的时间片轮转调度算法。时间片轮状调度算法可以实现进程共享CPU。在试验中,我发现时间片不能太大,否则会导致大部分的进程在一个时间片中就能运行完成,不能实现进程对CPU资源的共享。

附录:源程序

代码语言:javascript
复制
#include<iostream> #include<queue> #include<string> using namespace std; #define Time_Pice 2 struct _PCBNode { char name[20]; //进程名称 int arrive_time; //到达时间 int need_time; //服务时间 int cputime; //CPU已经执行的时间 int accomplish_Time; //完成时间 int turn_over_time; //周转时间 bool tag; //是否完成 }; class _PCB { public: void Push(struct _PCBNode& node) { _PNode.push(node); } void init_time() { TIME = 0; } bool empty() { return _PNode.empty(); } void Pop() { struct _PCBNode node; unsigned int n = _PNode.size(); unsigned int i = 0; for(; i< n; ++i) { node = _PNode.front();_PNode.pop(); if(node.arrive_time <= TIME) { break; } _PNode.push(node); } TIME = TIME+Time_Pice; if(i == n) { Display_PCB(); return ; } node.cputime +=Time_Pice; if(node.cputime >= node.need_time) { node.accomplish_Time = TIME; node.tag = true; node.turn_over_time = node.accomplish_Time -node.arrive_time ; _CNode.push(node); cout<<node.name<<" run over"<<endl; } else { _PNode.push(node); } Display_PCB(); } void Display_PCB() { cout<<" TIME "<<TIME<<endl; int n = _PNode.size(); if(n != 0) { cout<<"ready......."<<endl; cout<<"name arrive_time need_time cputime tag"<<endl; for(int i = 0;i<n; ++i) { struct _PCBNode node = _PNode.front();_PNode.pop(); if(node.arrive_time <= TIME) { cout<<node.name<<" "<<node.arrive_time<<" " <<node.need_time<<" "<<node.cputime <<" "<<node.tag <<endl; } _PNode.push(node); } cout<<endl; } n = _CNode.size(); if(n != 0) { cout<<"run over"<<endl; cout<<"name arrive_time need_time accomplish_Time turn_over_time tag"<<endl; for(int i = 0;i<n; ++i) { struct _PCBNode node = _CNode.front();_CNode.pop(); cout<<node.name<<" "<<node.arrive_time<<" " <<node.need_time<<" "<<node.accomplish_Time<<" "<< node.turn_over_time<<" "<<node.tag <<endl; _CNode.push(node); } cout<<endl; } cout<<"**************************************************"<<endl; } private: queue<struct _PCBNode> _PNode; queue<struct _PCBNode> _CNode; int TIME; }; void main() { _PCB pcb; pcb.init_time(); int PCB_Numble; char PCBname[20]; int arrive_time; int need_time; cout<<"input PCB_Numble : "; cin>>PCB_Numble; cout<<"input PCBname arrive_time need_time example A 0 5\n"; for(int i = 0; i<PCB_Numble; ++i) { cin>>PCBname>>arrive_time>>need_time; struct _PCBNode node; strcpy(node.name ,PCBname); node.arrive_time = arrive_time; node.need_time = need_time; node.accomplish_Time = 0; node.cputime = 0; node.tag = 0; pcb.Push(node); } pcb.Display_PCB(); while(!pcb.empty()) { pcb.Pop(); } }  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/218958.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档