操作系统进程控制实验报告代码

#include<iostream>
#include<vector>
#include <ctime>
#include <queue>
using namespace std;
int n;
int method;

class random         //取随机数
{
public:
	static inline int getRandom(const int min, const int max, int match)
	{
		srand(time(0) + match);
		return rand() % (max - min - 1) + min;
	}
};
class PCB
{
	int id;
	int time;
	string state;
	int round_time;
	int priority;
public:
	int get_priority()
	{
		return priority;
	}
	int get_time()
	{
		return time;
	}
	int get_id()
	{
		return id;
	}
	int get_round_time()
	{
		return round_time;
	}
	string get_state()
	{
		return this->state;
	}
	void set_id(int n)
	{
		this->id = n;
	}
	void set_round_time(int n)
	{
		this->round_time = n;
	}
	void set_priority(int n)
	{
		this->priority = n;
	}
	void set_time(int random)
	{
		this->time = random;
	}
	void set_state(string random)
	{
		this->state = random;
	}
};
bool operator > (PCB p1, PCB p2) {
	return p1.get_priority() > p2.get_priority();
}

bool operator < (PCB p1, PCB p2) {
	return p1.get_priority() < p2.get_priority();
}
void show(priority_queue<PCB, vector<PCB>, less<PCB> >ready_queue, queue<PCB>running,queue<PCB>finish)
{
	queue<PCB>ready;
	while(!ready_queue.empty())
	{
		ready.push(ready_queue.top());
		ready_queue.pop();
	}
	cout << "就绪队列为:";
	while (!ready.empty())
	{
		cout << ready.front().get_id() << " ";
		ready.pop();
	}
	cout << endl;
	cout << "运行队列为:";
	while (!running.empty())
	{
		cout << running.front().get_id() << " ";
		running.pop();
	}
	cout << endl;
	cout << "完成队列为:";
	while (!finish.empty())
	{
		cout << finish.front().get_id() << " ";
		finish.pop();
	}
	cout << endl;
}
void show(queue<PCB>ready, queue<PCB>running, queue<PCB>finish)
{
	cout << "就绪队列为:";
	while (!ready.empty())
	{
		cout << ready.front().get_id() << " ";
		ready.pop();
	}
	cout << endl;
	cout << "运行队列为:";
	while (!running.empty())
	{
		cout  << running.front().get_id() << " ";
		running.pop();
	}
	cout << endl;
	cout << "完成队列为:";
	while (!finish.empty())
	{
		cout  << finish.front().get_id() << " ";
		finish.pop();
	}
	cout << endl;
}
int main()
{
	cout << "请输入进程数目" << endl;
	cin >> n;
	cout << endl;
	cout << "请选择调度方法(1为优先权法,2为轮转法)" << endl;
	cin >> method;
	cout << endl;
	if (method == 1)
	{
		priority_queue<PCB, vector<PCB>, less<PCB> >ready_queue;
		queue<PCB>running;
		queue<PCB>finish;
		for (int i = 1; i <= n; ++i)
		{
			PCB pcb;
			pcb.set_id(i);
			pcb.set_state("Ready");
			pcb.set_time(random::getRandom(1, 20, i));
			pcb.set_priority(random::getRandom(70, 120, i));
			cout << "初始化进程 " << pcb.get_id() << '\t'
				<< " 状态为: " << pcb.get_state()
				<< "   所需时间片数为:  " << pcb.get_time()
				<< "   优先权为: " << pcb.get_priority() << endl;
			ready_queue.push(pcb);
		}
		while (!ready_queue.empty())
		{
			PCB temp = ready_queue.top();
			running.push(temp);
			ready_queue.pop();
			show(ready_queue, running, finish);
			temp.set_priority(temp.get_priority() - 3);
			temp.set_time(temp.get_time() - 1);
			if (temp.get_time() == 0)
			{
				finish.push(temp);
				running.pop();
				show(ready_queue, running, finish);
			}
			else
			{
				running.pop();
				ready_queue.push(temp);
			}
		}
	}
	else
	{
		queue<PCB>ready;
		queue<PCB>running;
		queue<PCB>finish;
		for (int i = 0; i < n; i++)
		{
			PCB pcb;
			pcb.set_id(i+1);
			pcb.set_time(random::getRandom(10, 20, i));
			pcb.set_round_time(random::getRandom(1, 5, i));
			cout << "初始化进程 " << pcb.get_id() << '\t'
				<< " 状态为: " << pcb.get_state()
				<< "   所需时间片数为:  " << pcb.get_time()
				<< "   轮转时间片为: " << pcb.get_round_time() << endl;
			ready.push(pcb);
		}
		while (!ready.empty())
		{
			PCB temp = ready.front();
			ready.pop();
			int temp_round_time = temp.get_round_time();
			if (temp_round_time >= temp.get_time())
			{
				finish.push(temp);
				show(ready, running, finish);
				continue;
			}
			else
			{
				running.push(temp);
				temp.set_time(temp.get_time() - temp_round_time);
				ready.push(temp);
				running.pop();
				show(ready, running, finish);

			}
		}
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#include<iostream> #include <iomanip> using namespace std; typedef struct page { int num; int bl_num; int i; struct page *next; }p_list,*p_ptr; typedef struct memory { int data; struct memory *next; }m_list,*m_ptr; int wst[8][8]; int v,z,qy_f=0,qy_l=0,yh; void change_add(int bl_num,int me_s,int lo_ad) { int b,c; yh=lo_ad/me_s; b=lo_ad%me_s; c=bl_num*me_s+b; cout<<"页号和偏移量:"<<yh<<"---"<<b<<endl; cout<<"物理地址为:"<<hex<<c<<endl; } void init_page(p_ptr &l,m_ptr &k) { int m; m_ptr s,q; p_ptr r,p; k=new m_list; k->next=NULL; s=k; l=new p_list; l->next=NULL; r=l; for(m=0;m<v;m++) { p=new p_list; p->num=m; p->bl_num=-1; p->i=0; r->next=p; r=p; } r->next=NULL; for(m=0;m<z;m++) { q=new m_list; q->data=-1; s->next=q; s=q; } s->next=NULL; } void show_page(p_ptr l) { p_ptr r; r=l->next; cout<<"页号"<<" "<<"块号"<<" "<<"状态位"<<endl; while(r!=NULL) { cout<<" "<<r->num<<" "<<setw(2)<<r->bl_num<<" "<<r->i<<endl; r=r->next; } } void show_memory(m_ptr k) { m_ptr s; s=k->next; cout<<"主存"<<endl; while(s!=NULL) { cout<<s->data<<endl; s=s->next; } } void init_wst() { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { wst[i][j]=rand()%2; } } } void print_wst() { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { cout<<wst[i][j]<<" "; } cout<<endl; } } int rand_bl() { int bl_nu; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(wst[i][j]==0) { wst[i][j]=1; bl_nu=8*i+j+1; return bl_nu; } } } return bl_nu; } int pdk(m_ptr k) { int i=0; m_ptr s; s=k->next; while(s!=NULL) { if(s->data==-1) { i++; s=s->next; } else { return i; } } return i; } int mzf(m_ptr k,int page_num) { int i=0; m_ptr s; s=k->next; while(s!=NULL) { if(s->data==page_num) { return 1; } else { s=s->next; } } return 0; } int FIFO(p_ptr &l,m_ptr &k,int page_num,int bl_nu) { int m; p_ptr r; m_ptr s,t,u; u=new m_list; s=k->next; r=l->next; while(r!=NULL) { if(r->num==page_num&&r->i!=0) { break; } if(r->num==page_num&&r->i==0) { r->i=1; r->bl_num=bl_nu; qy_f++; } r=r->next; } if(pdk(k)!=0&&pdk(k)==z) { while(s!=NULL) { if(s->data==page_num) { show_page(l); show_memory(k); return 0; } s=s->next; } s=k->next; for(m=0;m<z-1;m++) { s=s->next; } s->data=page_num; z--; show_page(l); show_memory(k); return 0; } if(pdk(k)==0) { if(mzf(k,page_num)==1) { show_page(l); show_memory(k); return 0; } if(mzf(k,page_num)==0) { while(s->next!=NULL) { t=s; s=s->next; } t->next=NULL; r=l->next; while(r!=NULL) { if(r->num==s->data) { r->bl_num=-1; r->i=0; } r=r->next; } delete s; u->data=page_num; u->next=k->next; k->next=u; show_page(l); show_memory(k); } } } /*int LRU(p_ptr &l,m_ptr &k,int page_num,int bl_nu) { int m; p_ptr r; m_ptr s,t,u; u=new m_list; s=k->next; r=l->next; while(r!=NULL) { if(r->num==page_num&&r->i!=0) { break; } if(r->num==page_num&&r->i==0) { r->i=1; r->bl_num=bl_nu; qy_l++; } r=r->next; } if(pdk(k)!=0&&pdk(k)==z) { while(s!=NULL) { if(s->data==page_num) { show_page(l); show_memory(k); return 0; } s=s->next; } s=k->next; for(m=0;m<z-1;m++) { s=s->next; } s->data=page_num; z--; show_page(l); show_memory(k); return 0; } if(pdk(k)==0) { if(mzf(k,page_num)==1) { while(s->next!=NULL) { t=s; if(s->data==page_num) { } } show_page(l); show_memory(k); return 0; } if(mzf(k,page_num)==0) { while(s->next!=NULL) { t=s; s=s->next; } t->next=NULL; r=l->next; while(r!=NULL) { if(r->num==s->data) { r->bl_num=-1; r->i=0; } r=r->next; } delete s; u->data=page_num; u->next=k->next; k->next=u; show_page(l); show_memory(k); } } }*/ void main() { int lo_ad,bl_nu,bl_sz,ra_bl; p_ptr page; m_ptr memory; cout<<"请输入页表长度:"<<endl; cin>>v; cout<<"请输入块数:"<<endl; cin>>z; cout<<"请输入块的长度(b):"<<endl; cin>>bl_sz; init_wst(); init_page(page,memory); show_page(page); show_memory(memory); while(lo_ad!=-1) { ra_bl=rand_bl(); cout<<"请输入逻辑地址:"<<endl; cin>>hex>>lo_ad; change_add(ra_bl,bl_sz,lo_ad); if(yh>v-1) { cout<<"error"<<endl; continue; } cout<<dec; cout<<"FIFO:"<<endl; FIFO(page,memory,yh,ra_bl); cout<<"缺页数:"<<qy_f<<endl; } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值