操作系统——实验一调度算法

一、实验目的

多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。因而引起进程调度。本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。

二、实验内容

  1. 优先权法、轮转法

简化假设

  1. 进程为计算型的(无I/O)
  2. 进程状态:ready、running、finish
  3. 进程需要的CPU时间以时间片为单位确定
  1. 算法描述
  1. 优先权法——动态优先权

当前运行进程用完时间片后,其优先权减去一个常数。

  1. 轮转法

三、实验要求

1.如采用随机数,随机数的取值范围应加以限制,如所需的CPU时间限制在1~20之间。

2.进程数n不要太大通常取4~8个

3.独立编程

4.二种调度算法

5.若有可能请在图形方式下,将PCB的调度用图形成动画显示。

四、实验方案

1.设计一个pcb类表示pcb,其中每个pcb有pcb的id、

pcb的需要时间、优先级数、三种状态。

2.设计一个process类表示调度,调度类含有pcb数据和pcb数量,

含有两种方法,优先权法、轮转法。

五、实验代码

//主函数
#include <iostream>
using namespace std;
#include"process.h"
int main()
{
    string condition;
    cout << "Which way you want to work? circle or precedence?" << endl;
    cin >> condition;
    if (condition == "circle")
    {
        process obj(process::circle);
        obj.start();
        return 0;
    }
    else
    {
        process obj(process::precedence);
        obj.start();
    }
    return 0;
}
//pcb类头文件
#pragma once
#include<iostream>
using namespace std;
class pcb
{
	typedef unsigned pcb_u;
public:
	enum state { ready, running, finished };
	pcb(pcb_u i = 0, state s = ready, pcb_u n = 0,pcb_u p = 0 ) :
		shread_id(i), status(s), need_time(n), priority(p) {}
	~pcb() {}
	void change_status(state s) { status = s; }
	void need_minus(pcb_u t) { need_time -= t; }
	void priority_minus(pcb_u p) { priority -= p; }
	void print_pcb();
	pcb_u pri_num()const { return priority; }
	pcb_u need_num()const { return need_time; }
private:
	//优先权法需要优先级,采用优先队列,根据优先级数排列
	//轮转法一股脑先轮转,并不需要优先级数,普通队列来实现先来先服务
	pcb_u shread_id;//进程id
	state status;//进程状态
	pcb_u need_time;//当前进程需要时间
	pcb_u priority;//当前进程优先级
};
//pcb实现源文件
#include "pcb.h"
void pcb::print_pcb()
{
	cout << "shread_id:" << shread_id << " ";
	switch (status)
	{
		case ready:cout << "status:" << "ready" << " "; break;
		case running:cout << "status:" << "running" << " "; break;
		case finished:cout << "status:" << "finished" << " "; break;
	}
	cout << "need_time:" << need_time << " ";
	cout << "priority:" << priority << " " << endl;
}
//进程process类头文件
#pragma once
#include"pcb.h"
class process
{
	typedef unsigned process_u;
public:
	enum way { circle, precedence};
	process(way m= precedence);
	~process();
	void start();
	void print() const;
private:
	pcb* data;//数据
	process_u pcb_num;
	way manage;
};
//process实现源文件
#include "process.h"
#include<cstdlib>
#include<ctime>
#include<memory>
#include<queue>
#include<functional>
process::process(way m)
{
	srand((unsigned)time(0));
	manage = m;
	//取4到8个进程
	allocator<pcb> alloc;
	pcb_num = rand() % 5 + 4;
	data = alloc.allocate(pcb_num);
	pcb* p = data;
	for (process_u i = 0; i < pcb_num; i++)
		alloc.construct(p++, i+1,pcb::ready,rand()%20+1,rand()%20+80);
}
process::~process()
{
	allocator<pcb> alloc;
	pcb* p = data + pcb_num;
	for (int i = pcb_num; i > 0; i--)
		alloc.destroy(--p);
	alloc.deallocate(data, pcb_num);
}
void process::start()
{
	std::cout << "进入状态:" << endl;
	print();
	if (manage == precedence)
	{
		std::cout<<"优先权法开始:"<<endl;
		auto f = [](pcb* a, pcb* b)
			{return a->pri_num() < b->pri_num(); };
		priority_queue < pcb*, vector<pcb*>, decltype(f)> p(f);
		for (process_u i = 0; i < pcb_num; i++)
			p.push(&(data[i]));
		while (!p.empty())
		{
			pcb* q = p.top();
			p.pop();
			q->need_minus(1);
			q->priority_minus(3);
			q->change_status(pcb::running);
			if (q->need_num() == 0)
			{
				q->change_status(pcb::finished);
				print();
				continue;
			}
			print();
			q->change_status(pcb::ready);
			p.push(q);
		}
		std::cout << "优先权法结束" << endl;
		return;
	}
	std::cout << "轮转法开始:" << endl;
	queue<pcb*> p;
	for (process_u i = 0; i < pcb_num; i++)
		p.push(&(data[i]));
	while (!p.empty())
	{
		int temp = p.size();
		while (temp--)
		{
			pcb* t = p.front();
			p.pop();
			t->need_minus(1);
			if (t->need_num() == 0)
			{
				t->change_status(pcb::finished);
				print();
				continue;
			}
			t->change_status(pcb::running);
			print();
			t->change_status(pcb::ready);
			p.push(t);
		}
	}
	std::cout << "轮转法结束" << endl;
}
void process::print() const
{
	for (process_u i = 0; i < pcb_num; i++)
		data[i].print_pcb();
	std::cout << endl;
}

六、预习中问题及对应思考

  1. 优先权法,需要每次排列,使用了优先队列。
  2. 轮转法,只需要普通队列就行。
  3. pcb使用了三种状态,为了防止使用其他类型范围溢出,也使便于理解,采用了枚举类型。
  4. process两种方法同上。
  5. 优先级数为了防止类型溢出,最起码要加上需要时间*优先权法每次减去优先级数。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值