时间片轮转算法

时间片轮转算法:主要用于分时系统中的进程调度。为了实现轮转调度,系统把所有就绪进程按先入先出的原则排成一个队列。新来的进程加到就绪队列末尾。每当执行进程调度时,进程调度程序总是选出就绪队列的队首进程,让它在CPU上运行一个时间片的时间。时间片是一个小的时间单位,通常为10~100ms数量级。当进程用完分给它的时间片后,系统的计时器发出时钟中断,调度程序便停止该进程的运行,把它放入就绪队列的末尾;然后,把CPU分给就绪队列的队首进程,同样也让它运行一个时间片,如此往复。

测试案例:任务A和任务B的周期时间分别为2s和5s,每个周期的处理时间为1s和2.5s

代码:

#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<queue>
#include<list>
#include<thread>
#include<mutex>
#include<Windows.h>
using namespace std;
#define A_RUN_TIME 10
#define B_RUN_TIME 25
#define A_STOP_TIME 20
#define B_STOP_TIME 50
#define time_film  10 //时间片
#define MAX_TIME 99999
int g_time = 0;
mutex g_mutex_time;



struct process_node
{
	int prcess_id;  //进程编号
	int _start;  //进入时间
	void(*main_doing)(int args, char *ptr_argv[]);//本进程完成的任务
	int begin;   //开始时间
	int finish;  //完成时间
	int _function; //需要运行时间
	int function; //已经运行的时间
	bool complete;	 //是否完成   true 完成
};

list<process_node*>q_list;//进程队列

void main_doing(int args, char *ptr_argv[])
{
	cout << args << "这是一个运行的实例" << endl;
	Sleep(50);
}

void Come_Init_Prcess(void(*main_doing)(int args, char *ptr_argv[]),  int _function) //模拟进程到来并且初始化
{
	static int id = 0;
	process_node *p = new process_node;
	p->prcess_id = ++id;
	p->_start = g_time;
	p->main_doing = main_doing;
	p->_function = _function;
	p->begin = MAX_TIME;
	p->finish = MAX_TIME;
	p->function = 0;
	p->complete = false;
	q_list.push_back(p);
}
void Time_End_Work(process_node & current_process)//时间片结束的前的工作
{



	if (current_process.function >= current_process._function)//判断是否完成
	{
		current_process.complete = true;
		current_process.finish = g_time;
		cout << "***********进程" << current_process.prcess_id << "完成任务*********" << endl;
		cout << "***********进入时间:" << current_process._start << endl;
		cout << "***********开始时间:" << current_process.begin << endl;
		cout << "***********完成时间:" << current_process.finish << endl;
		q_list.remove(&current_process);
	}
	else
	{
		cout << "\t\t\t\t\t\t未完成放到队尾" << current_process .prcess_id<<"***********"<< endl;
		q_list.remove(&current_process);
		cout << "\t\t\t\t\t\t放入的是"<< current_process.prcess_id << endl;
		q_list.push_back(&current_process);
	}
}

void Ahead_Of_Time(process_node & current_process)//进程提前完成 让出时间片
{
	int current_point = g_time;
	cout << "当前时间" << current_point << endl;
	while (current_point + current_process._function - current_process.function > g_time)
	{
		current_process.main_doing(current_process.prcess_id, NULL);
	}
	current_process.function += g_time - current_point;
	Time_End_Work(current_process);
}
void One_Of_Time(process_node & current_process)
{
	int current_point = g_time;
	cout << "当前时间" << current_point << endl;
	while (current_point + time_film > g_time)
	{
		current_process.main_doing(current_process.prcess_id, NULL);
	}
	current_process.function += g_time - current_point;
	Time_End_Work(current_process);
}
void Time_File_Doing(process_node & current_process) //时间片完成的工作
{
	//cout << "+++++++++++++++++" << current_process._function - current_process.function<<"+++++++++++++++++++++++" << endl;
	if (current_process._function - current_process.function < time_film)//不到一个时间片的处理
	{
		Ahead_Of_Time(current_process);
	}
	else
	{
		One_Of_Time(current_process);
	}
}


process_node& Obtain_Obtain()//获取优先者
{
	process_node *p_temp = nullptr;
	while (q_list.size() == 0);
	p_temp = q_list.front();
	cout << "优先的是程序" << p_temp->prcess_id << endl;
	cout << "\t\t\t队列中的元素有" << q_list.size() << endl;
	if (p_temp->begin==MAX_TIME)
	{
		p_temp->begin = g_time;
	}
	return *p_temp;
}
void Run_begin()
{

	while (1)
	{
		process_node &p_temp = Obtain_Obtain();
		Time_File_Doing(p_temp);
	}
}
//时间实例到达
void pthread_model()
{
	while (1)
	{
		lock_guard<mutex>ldg(g_mutex_time);
		cout << g_time << endl;
		if (g_time % 20 == 0)
		{
			Come_Init_Prcess(main_doing,  A_RUN_TIME);
			cout << "A实例到达" << endl;
		}
		if (g_time % 50 == 0)
		{
			Come_Init_Prcess(main_doing,  B_RUN_TIME);
			cout << "B实例到达" << endl;
		}
		Sleep(100);
		g_time++;
	}
}
int main()
{
	thread th_model(pthread_model);
	thread th_main(Run_begin);
	th_model.join();
	th_main.join();
	cin.get();
}

测试结果

 

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值