操作系统进程调度模拟——优先级调度算法

#include 

#define N 10
using namespace std;

int iflinenull(int head,int tail);  //判断队列是否为空

int writePCB(int id, int status, int priority,int runtime);//向pcb中填写数据

int deletepre(int x);  //从就绪队列中删除
int insertpre(int x);  //插入就绪队列
int showpre();   //打印就绪队列
int deletefree(int x); //从空闲队列中删除
int insertfree(int x); //插入空闲队列 


int create(int x, int priority, int runtime);  //创建进程
int sheduling();    //进程调度

int dosth(int x);  //占用cpu执行任务

//空闲队列
struct free{
	int fhead;  //空闲队列头
	int ftail;  //空闲队列尾
}free01;
struct prepare{
	int phead;  //就绪队列头
    int ptail;  //就绪队列尾
}prepare01;
struct pcb 
{
	int id;  
	int status; //进程状态
	int priority; //优先级  数字越小优先级越高
	int runtime;//进程运行时间
	int pre;  //向前指
	int next; //向后指
	

};
pcb line[10];
int running; //当前正在运行

int iflinenull(int head,int tail) {  //判断队列是否为空
	if (head == -1 && tail == -1) return 1;
	else return 0;
}

int showpre() {
	int p = prepare01.phead;  //正序显示就绪队列
	cout << "就绪队列:";
	while (p != prepare01.ptail) {
		cout << p;
		p = line[p].next;

	}
	cout << p << endl;
	return 0;
}

int writePCB(int id, int status, int priority, int runtime) {  向pcb中填写数据
	line[id].id = id;
	line[id].status = status;
	line[id].priority = priority;
	line[id].runtime = runtime;
	return 0;
}


int deletepre(int x) {  //从就绪队列中删除
	if (iflinenull(prepare01.phead, prepare01.ptail)) return -1;
	if (x == prepare01.phead) {  //当删除头结点
		prepare01.phead = line[prepare01.phead].next;
		line[prepare01.phead].pre = -1;
		return 0;
	}
	if (x == prepare01.ptail) {  //当删除尾结点
		prepare01.ptail = line[prepare01.ptail].pre;
		line[prepare01.ptail].next = -1;
		return 0;
	}

	//删除中间的节点
	line[line[x].pre].next = line[x].next;
	line[line[x].next].pre = line[x].pre;
	return 0;

}

int insertpre(int x) {  //插入就绪队列
	int p = prepare01.phead;  //插入该指针的前方
	if(iflinenull(prepare01.phead,prepare01.ptail)) {  //当就绪队列为空时
		line[x].next = -1;
		line[x].pre = -1;
		prepare01.phead = x;
		prepare01.ptail = x;
		return 0;
    }
	if (prepare01.phead == prepare01.ptail) {  //当就绪队列只有一个节点时
		if (line[x].priority <= line[prepare01.phead].priority) { //该节点之前
			line[x].next = prepare01.phead;
			line[x].pre = -1;
			line[prepare01.phead].pre = x;
			prepare01.phead = x;
			return 0;
		}
		else {  //该节点之后
			line[prepare01.ptail].next = x;
			line[x].pre = prepare01.ptail;
			line[x].next = -1;
			prepare01.ptail = x;
			return 0;
		}
	}
	while(line[x].priority>line[p].priority && line[p].next!=-1){
		p = line[p].next;
	}
	if(p == prepare01.phead){  //插入到头结点之前
		line[x].next = prepare01.phead;
		line[x].pre = -1;
		line[prepare01.phead].pre = x;
		prepare01.phead = x;
		return 0;
	}
	if(line[x].priority>line[prepare01.ptail].priority){  //插入到尾结点
		line[prepare01.ptail].next = x;
		line[x].pre = prepare01.ptail;
		line[x].next = -1;
		prepare01.ptail = x;
		return 0;
		
	}
	//当在中间时
	line[line[p].pre].next = x;
	line[x].pre = line[p].pre;
	line[x].next = p;
	line[p].pre = x;
	return 0;

	


}

int deletefree(int x) {  //从空闲队列中删除
	if (iflinenull(free01.fhead, free01.ftail)) return -1;
	if (x == free01.fhead) {  //当删除头结点
		free01.fhead = line[free01.fhead].next;
		line[free01.fhead].pre = -1;
		return 0;
	}
	if (x == free01.ftail) {  //当删除尾结点
		free01.ftail = line[free01.ftail].pre;
		line[free01.ftail].next = -1;
		return 0;
	}

	//删除中间的节点
	line[line[x].pre].next = line[x].next;
	line[line[x].next].pre = line[x].pre;
	return 0;

}

int insertfree(int x) {  //插入空闲队列 
	if (iflinenull(free01.fhead,free01.ftail)) {  //当空闲队列为空时
		line[x].pre = -1;
		line[x].next = -1;
		free01.fhead = x;
		free01.ftail = x;
		//cout << "为空";
		return 0;
	}
	line[x].next = free01.fhead;  //当空闲队列不空时
	line[x].pre = -1;
	line[free01.fhead].pre = x;
	free01.fhead = x;
	//cout << "不为空";
	return 0;
}

int dosth(int x) {     //占用cpu执行任务
	line[x].priority++;
	line[x].runtime--;
	return 0;
}



int create(int x,int priority,int runtime) {  //创建进程
	if (iflinenull(free01.fhead,free01.ftail)) {  //判断空闲队列是否为空
		cout << "ERROR" << endl;
		return -1;
    }
	writePCB(x, 0, priority,  runtime); //向pcb中填写信息
	deletefree(x);    //x从空闲队列中删除
	insertpre(x);     //将x插入就绪队列
	return 0;

}


int sheduling() {   //进程调度
	running = prepare01.phead;
	dosth(running);  //x占用cpu干活
	if (line[running].runtime == 0) {
		line[running].status = -1;
		deletepre(running);  //从就绪队列中删除
		insertfree(running); //插入空闲队列
		return 0;
	}
	deletepre(running);  //从就绪队列中取出  重新放入就绪队列
	insertpre(running);  //插入到就绪队列
	
	if(iflinenull(prepare01.phead,prepare01.ptail)){
		cout << "就绪队列为空" << endl;
		return 0;
	}
	else {
		running = prepare01.phead;  //将就绪队列第一个交给处理机
		return 0;
	}

	return 0;
}

int main() {
	int id;
	int prituroty;
	int runtime;
	//初始化
	free01.fhead = -1;
	free01.ftail = -1;
	prepare01.phead = -1;
	prepare01.ptail = -1;
	for (int i = 0; i < N; i++) {
		insertfree(i);
	}
	cout << "请输入id,优先级,运行时间" << endl;
	cin >> id;
	cin >> prituroty;
	cin >> runtime;
	while (id >-1) {
		create(id,prituroty,runtime);
		cin >> id;
		cin >> prituroty;
		cin >> runtime;
    }
	showpre();
	for (int i = 0; i < 10;i++) {  //进行五次调度
		sheduling();
		cout << "运行进程:" << running << endl;
		showpre();
		}
		
		cout << endl;

		return 0;
}

/*int main() {
	//int id;
	//int prituroty;
	//int runtime;
	//初始化
	free01.fhead = -1;
	free01.ftail = -1;
	prepare01.phead = -1;
	prepare01.ptail = -1;
	int j = 0;
	for (int i = 0; i < N; i++) {  //为优先级赋值
		line[i].priority = i;
		

	
	}
	
	for (int i = 0; i -1; i--) {
		insertfree(i);
		cout << free01.fhead;
		cout << free01.ftail;

	}*/
	/*int p= free01.fhead;                  测试插入空闲队列,显示空闲队列
		while (p != free01.ftail ) {
			cout << p;
			p = line[p].next;
			
	}
		cout << p << endl;
		 p = free01.ftail;
		 
		while (p != free01.fhead) {
			cout << p;
			p = line[p].pre;

		}
		cout << p << endl;  */

	//测试从空闲队列中删除
	/*deletefree(0);
	int p = free01.fhead;                  //显示空闲队列
		while (p != free01.ftail) {
			cout << p;
			p = line[p].next;

		}
	cout << p << endl;
	p = free01.ftail;

		
		return 0;
} 
*/

操作系统进程调度模拟——优先级调度算法_第1张图片

你可能感兴趣的:(操作系统,c++,队列)