⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 进程调度算法,优先级算法,静态优先级和运行时候优先级自动调整
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

const int TIMESLICE = 1;//时间片
const int FINISH=0;//完成状态
const int RUNNING=1;//运行状态
const int READY=2;//就绪状态
const int IO=3;//io状态
const int WAIT=4;//等待IO

typedef struct {
	long pid;
	char pname[10];
	int pstate;
	int pneedtime;
	int piostarttime;
	int pioneedtime;
	int ptime;
	int priority;
}pcbs;//pcb结构

class pcbnode;
//队列结点
class pcbnode
{
public:
	pcbs *pcb;
	pcbnode *link;
	pcbnode();
	~pcbnode();
	int run();//运行操作
	int runend();//运行结束?
	void runio();//IO操作
	int ioend();//io结束?
	int insertnode(pcbnode *p,pcbnode *q);//在q后插入结点p
	int deletenode(pcbnode *p,pcbnode *q);//删除p结点,q为p的前驱
	int addnode(pcbnode *p);//增加结点
	int pcbnode::isio();//是否开始IO
};
pcbnode::pcbnode()
{
	pcb=0;
	link=0;
}
pcbnode::~pcbnode()
{
	if (link)
		delete link;
	if (pcb)
		pcb->pstate=FINISH;
}
int pcbnode::run()
{
	pcb->pstate = RUNNING;
	++(pcb->ptime);
	pcb->priority --;//优先级降低
	return 0;
}
int pcbnode::runend()
{
	return (pcb->pneedtime <= pcb->ptime);
}
int pcbnode::isio()
{
	return (pcb->piostarttime == pcb->ptime && pcb->pioneedtime>0);
}
int pcbnode::ioend()
{
	return ((pcb->pioneedtime) <= 0);
}
void pcbnode::runio()
{
	pcb->pstate = IO;
	--(pcb->pioneedtime);
}
int pcbnode::addnode(pcbnode *p)
{
	pcbnode *q;
	q = this;
	p->pcb->pstate = READY;
	while(q->link)
	{
		q=q->link;
	}
	q->link=p;
	return 0;
}
int pcbnode::insertnode(pcbnode *p,pcbnode *q)
{
	p->link = q->link;
	q->link = p;
	return 0;
}
int pcbnode::deletenode(pcbnode *p,pcbnode *q)
{
	q->link = p->link;
	p->link = 0;
	return 0;
}
int randInt( int seed)
//随即函数:产生不大于seed的正整数
{
	int r;
	r=rand();
	while ( r>seed || r<0 )
		r=rand();
	return r;
}
void newpcb1(pcbs *pcb, int order)//随机生成进程
{
	char buf[10];
	pcb->pid = order;
	strcpy(pcb->pname,"proc");
	itoa(order,buf,10);
	strcat(pcb->pname,buf);
	pcb->pneedtime = randInt(10);
	pcb->piostarttime = randInt(pcb->pneedtime);
	pcb->pioneedtime = randInt(10);
	pcb->ptime = 0;
	pcb->priority = randInt(10);
}
void newpcb2(pcbs *pcb,int porder)//手动生成进程
{
	char buf[10];
	cout<<"Input the pid:          ";
	cin>>porder;
	pcb->pid = porder;
	strcpy(pcb->pname,"proc");
	itoa(porder,buf,10);
	strcat(pcb->pname,buf);
	cout<<"Input the pneedtime:    ";
	cin>>pcb->pneedtime;
	cout<<"Input the pioneedtime:  ";
	cin>>pcb->pioneedtime;
	cout<<"Input the priority:     ";
	cin>>pcb->priority;
	pcb->piostarttime = pcb->pioneedtime;
	pcb->ptime = 0;
}
void pprint(pcbs *pcb, int count)
{
//打印进程状态

	ofstream ofs("result.txt", ios::out|ios::app);
	cout<<"pcb:id name stat need iostart ioneed runtime pri"<<endl;
	ofs<<"pcb:id name stat need iostart ioneed runtime pri"<<endl;
	for (int i=0;i<count;i++)
	{
		cout<<pcb->pid<<','<< pcb->pname<<',';
		ofs<<pcb->pid<<','<< pcb->pname<<',';
		switch(pcb->pstate)
		{
case 3://IO
	cout<<"IO";
	ofs<<"IO";
	break;
case 1://RUNNING
	cout<<"RUNNING ";
	ofs<<"RUNNING ";
	break;
case 2://READY
	cout<<"READY";
	ofs<<"READY";
	break;
case 0://FINISH
	cout<<"FINISH";
	ofs<<"FINISH";
	break;
case 4://WAIT
	cout<<"WAIT";
	ofs<<"WAIT";
	break;
		}
		cout<<'"'<<pcb->pneedtime;
		ofs<<'"'<<pcb->pneedtime;
		cout<<'"'<<pcb->piostarttime<<'"'<<pcb->pioneedtime<<'"'<<pcb->ptime;
		ofs<<'"'<<pcb->piostarttime<<'"'<<pcb->pioneedtime<<'"'<<pcb->ptime;
		cout<<'"'<<pcb->priority;
		ofs<<'"'<<pcb->priority;
		cout<< endl;
		ofs<< endl;
		*pcb++;
	}
	ofs<<"---------------------------------------------"<<endl;
	cout<<"---------------------------------------------"<<endl;
}


void generalpriority(pcbs * pcb, int pcbsnum)
//普通的优先调度
/*
* 从就绪队列中取出最优先的进程送到运行队列
* 如果运行结束或要执行IO再重新调度
*/
{
	pcbnode running,ready,blocked;
	pcbnode *p,*f,*front;
	pcbnode *q;
/*将进程表中的进程加到就绪队列中*/
	for ( int i=0;i<pcbsnum;i++)
	{
		p = new pcbnode;
		p->pcb = pcb+i;
		ready.addnode(p);
	}
	while(ready.link || running.link || blocked.link)
	{//判断将运行队列中的进程是否要io或结束
		if (running.link)
		{
			if (running.link->isio())
			{//需要IO?
				blocked.addnode(running.link);
				running.link->pcb->pstate = WAIT;
				running.link = 0;
			}
			else if (running.link->runend())
			{//运行结束?
				p = running.link;
				running.link = 0;
				delete p;
			}
		}
		if (!running.link)
		{
			//当运行队列为空
			//寻找最大的一个优先级
			p = ready.link;
			q = p;
			f = &ready;
			front = f;
			if (p)
			{
				int maxpri = p->pcb->priority;
				while(p)
				{
					if (p->pcb->priority > maxpri)
					{
						maxpri = p->pcb->priority;
						front = f;
						q = p;
					}
					f = p;
					p = p->link;
				}
			}
			p = running.link;
			if (q)
			{
				running.addnode(q);
				ready.deletenode(q, front);
				q->pcb->pstate=RUNNING;
			}
		}
/*
* 将处理完IO的进程送到就绪队列
*/
		p = blocked.link;
		q = &blocked;
		if (p) 
		{
			int r;
			r = p->ioend();
			if (r) 
			{
				blocked.deletenode(p, q);
				ready.addnode(p);
				p=q->link;
			}
		}

/*
* 运行进程
*/
		p = running.link;
		q = &running;
		if (p)
		{
			int r;
			if (!(p->isio()))
				//is IO start?
				r=p->run();//RUN
		}
/*
* 进行IO
*/
		p = blocked.link;
		q = &blocked;
		if (p)
			p->runio();
		//print proc state
		pprint(pcb, pcbsnum);
	}
}
void main()
{
	int i,pcbsnum;
	pcbs *pcblist;//进程表
	remove("result.txt");
	cout<<"Frist,bulit the pcblist"<<endl;
	cout<<"Input the pcbsnum:   ";
	cin>>pcbsnum;
	pcblist = new pcbs[pcbsnum];//为进程表分配空间
	cout<<"1.自动生成进程 2.手动生成进程:        ";
	cin>>i;
	if(i==1)
	for(int i = 0; i < pcbsnum; i++)
		newpcb1(pcblist+i, i);
	else for(int i = 0; i < pcbsnum; i++)
		newpcb2(pcblist+i, i);
	cout<<"Second,print the pcblist:"<<endl;
	pprint(pcblist, pcbsnum);
	cout<<"Third,print the pcb dispatch:"<<endl;
	getchar();
	generalpriority(pcblist,pcbsnum);
	
	delete pcblist;//释放进程空间
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -