操作系统实验C++实现

depend.h

#pragma once

#ifndef _DEPEND_
#define _DEPEND_

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<Windows.h>
#include<algorithm>
#include<sstream>
#include<list>
#include<cstdlib>
using namespace std;


#define READY 1//就绪
#define RUN 2//运行
#define STOP 3//阻塞
#define DISTRIBUTION_MAX 200	//可分配空间最大值
#define START_ADDRESS 60		//起始地址

class PCB;
PCB* run = NULL;
//bool isRun = false;
class SHOW {
    
    
private:
	short x, y;
	string dialog;
public:
	SHOW(short x, short y, const string dialog);
	//打印dialog
	void gotoxy();
	static void gotoxy(short x, short y, string infor) {
    
    
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD p;
		p.X = x;
		p.Y = y;
		SetConsoleCursorPosition(handle, p);
		cout << infor;
	}
};

class PCB {
    
    
private:
	string processName;
	int priority;		//优先级		数大的优先级高
	int runTime;		//运行时间
	int state;			//状态
	string runState;
	PCB* next;			//指向下一个PCB
public:
	//构造函数	next一般初始为NULL
	PCB(string processName, int priority, int runTime, int state, PCB* next);
	~PCB();
	void setState(int state);
	//新建的PCB放入就绪队列
	bool putReady(vector<PCB*>& ready, vector<PCB*>& stop);//false为队列已满
	bool stopToReady(vector<PCB*>& stop, vector<PCB*>& ready);
	int putStop(vector<PCB*>& ready, vector<PCB*>& stop, int former);//放入阻塞队列 0 队列已满
	bool deletePCB(vector<PCB*>& s);//从向量s中删除PCB
	/*static bool deletePCB(vector<PCB*>& ready, vector<PCB*>& stop, int state) {
		vector<PCB*>& s = ;
	}*/
	bool putRun(vector<PCB*>& s);//将已有的进程放入运行状态
	bool lookForward(vector<PCB*>& s);
	static int lookForward(vector<PCB*>& s, string processName) {
    
    //0 不存在
		//返回处于的状态
		//if (run->processName == processName) return RUN;
		if (s.empty()) return 0;
		PCB* temp = s.front();
		while (temp != NULL) {
    
    
			if (temp->processName == processName)
				return temp->state;
			temp = temp->next;
		}
		return 0;
	}
	string getName();
	int getState();
	int getRunTime();
	int getPriority();
	void setPriority(int size);
	void setRunTime(int size);
	string toString(int count=0);
	PCB* getNext();
	void setNext(PCB* s);
	//friend PCB operator-(PCB& obj, int a);
};

//PCB operator-(PCB& obj, int a) {
    
    
//	obj.runTime -= a;
//	return obj;
//}

class TASK {
    
    //作业
public:
	string name;
	int length;
	int startAddress;
	TASK(string name, int length);
	TASK(string name, int length, int startAddress);
	~TASK();
	bool putlist(list<TASK>& distribution,list<TASK>& unDistribution);//true 为不能插入队尾
	void erase(list<TASK>::iterator& it, list<TASK>& distribution, list<TASK>& unDistribution);
	string toString();
};

//itos=int to string
string itos(int value, int Radix) {
    
    //Radix为进制 
	string s;
	char temp[10] = "";
#pragma warning(disable : 4996)
	itoa(value, temp, 10);//10指的是10进制
#pragma warning(default : 4996)
	s = temp;
	return s;
}
#endif // !_DEPEND_

depend.cpp

#include "depend.h"
//#ifdef _DEPEND_
SHOW::SHOW(short x, short y, const string dialog) {
    
    
	this->x = x;
	this->y = y;
	this->dialog = dialog;
}
void SHOW::gotoxy() {
    
    
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD p;
	p.X = this->x;
	p.Y = this->y;
	SetConsoleCursorPosition(handle, p);
	cout << dialog;
}
PCB::PCB(string processName, int priority, int runTime, int state, PCB* next) {
    
    
	//runTime单位为s
	this->processName = processName;
	this->priority = priority;
	this->runTime = runTime;
	this->state = state;
	this->next = next;
	if (state == READY) {
    
    
		this->runState = "就绪";
	}
	else if (state == RUN) {
    
    
		this->runState = "运行";
	}
	else if (state == STOP) {
    
    
		this->runState = "阻塞";
	}
}
PCB::~PCB() {
    
    
}
void PCB::setState(int state) {
    
    
	this->state = state;
	if (state == READY) {
    
    
		this->runState = "就绪";
	}
	else if (state == RUN) {
    
    
		this->runState = "运行";
	}
	else if (state == STOP) {
    
    
		this->runState = "阻塞";
	}
}
bool PCB::putReady(vector<PCB*>& ready, vector<PCB*>& stop) {
    
    //false为队列已满
	this->state = READY;
	this->runState = "就绪";
	if (ready.size() == 0) ready.push_back(this);
	else if (ready.size() == 10 || lookForward(ready) || lookForward(stop)) return false;
	else {
    
    
		ready.back()->next = this;
		ready.push_back(this);
	}
	return true;
}
bool PCB::stopToReady(vector<PCB*>& stop, vector<PCB*>& ready) {
    
    
	if (stop.empty()) return false;
	int i;
	this->state = READY;
	vector<PCB*>::iterator it = stop.begin();
	for (i = 0; it != stop.end(); it++, i++) {
    
    
		if (stop[i]->processName == this->processName) {
    
    
			if (i - 1 >= 0 && i + 1 < stop.size())
				stop[i - 1]->next = stop[i]->next;
			else if (i - 1 < 0 && i + 1 < stop.size())
				this->next = NULL;
			else if (i - 1 >= 0 && i + 1 >= stop.size())
				stop[i - 1]->next = NULL;
			if (!ready.empty()) ready.back()->next = this;
			ready.push_back(this);
			stop.erase(it);
			return true;
		}
	}
	return false;
}
int PCB::putStop(vector<PCB*>& ready, vector<PCB*>& stop, int former) {
    
    //放入阻塞队列 0 队列已满
	this->state = STOP;
	this->runState = "阻塞";
	if (stop.size() == 10) return 0;
	if (former == READY) {
    
    
		int i = 0;
		for (vector<PCB*>::iterator it = ready.begin(); it != ready.end(); it++, i++) {
    
    
			if (ready[i]->processName == this->processName) {
    
    
				if (i - 1 >= 0 && i + 1 < ready.size())
					ready[i - 1]->next = ready[i]->next;
				else if (i - 1 < 0 && i + 1 < ready.size())
					this->next = NULL;
				else if (i - 1 >= 0 && i + 1 >= ready.size())
					ready[i - 1]->next = NULL;
				if (!stop.empty()) stop.back()->next = this;
				stop.push_back(this);
				ready.erase(it);
				break;
			}
		}
	}
	if (former == RUN) {
    
    
		if (!stop.empty())
			stop.back()->next = run;
		stop.push_back(run);
		run = NULL;
	}
	if (former == STOP) {
    
    
		if (!stop.empty())
			stop.back()->next = this;
		stop.push_back(this);
	}
	return STOP;
}
bool PCB::deletePCB(vector<PCB*>& s) {
    
    //从向量s中删除PCB
	if (s.empty()) return false;
	int i;
	vector<PCB*>::iterator it = s.begin();
	for (i = 0; it != s.end(); it++, i++) {
    
    
		if (s[i]->processName == this->processName) {
    
    
			if (i - 1 >= 0 && i + 1 < s.size())
				s[i - 1]->next = s[i]->next;
			else if (i - 1 < 0 && i + 1 < s.size())
				this->next = NULL;
			else if (i - 1 >= 0 && i + 1 >= s.size())
				s[i - 1]->next = NULL;
			s.erase(it);
			return true;
		}
	}
	return false;
}
bool PCB::putRun(vector<PCB*>& s) {
    
    //将已有的进程放入运行状态
	if (s.empty()) return false;
	int i;
	vector<PCB*>::iterator it = s.begin();
	for (i = 0; it != s.end(); it++, i++) {
    
    
		if (s[i]->processName == this->processName) {
    
    
			if (i - 1 >= 0 && i + 1 < s.size())
				s[i - 1]->next = s[i]->next;
			else if (i - 1 < 0 && i + 1 < s.size())
				this->next = NULL;
			else if (i - 1 >= 0 && i + 1 >= s.size())
				s[i - 1]->next = NULL;
			run = s[i];
			run->state = RUN;
			run->runState = "运行";
			run->next = NULL;
			s.erase(it);
			return true;
		}
	}
	return false;
}
bool PCB::lookForward(vector<PCB*>& s) {
    
    //看是否有this
	if (s.empty()) return false;
	PCB* temp = s.front();
	while (temp != NULL) {
    
    
		if (temp->processName == this->processName)
			return true;
		temp = temp->next;
	}
	return false;
}
string PCB::getName() {
    
    
	return this->processName;
}
int PCB::getState() {
    
    
	return this->state;
}
int PCB::getRunTime()
{
    
    
	return this->runTime;
}
int PCB::getPriority(){
    
    
	return this->priority;
}
void PCB::setPriority(int size){
    
    
	this->priority = size;
}
void PCB::setRunTime(int size){
    
    
	this->runTime = size;
}
string PCB::toString(int count) {
    
    
	string ans, s;
	stringstream ss1, ss2, ss3;
	if (count != 0) {
    
    
		ss3 << count;
		ss3 >> s;
		ans += s + '\t';
	}
	ans += processName + '\t' + '\t';
	ss1 << priority;
	ss1 >> s;
	ans += s + '\t';
	ss2 << runTime;
	ss2 >> s;
	ans += s + '\t' + '\t';
	ans += runState;
	return ans;
}
PCB* PCB::getNext() {
    
    
	return this->next;
}
void PCB::setNext(PCB* s) {
    
    
	this->next = s;
}
TASK::TASK(string name, int length) {
    
    
	this->name = name;
	this->length = length;
}
TASK::TASK(string name, int length,int startAddress) {
    
    
	this->name = name;
	this->length = length;
	this->startAddress = startAddress;
}

TASK::~TASK(){
    
    
}

bool TASK::putlist(list<TASK>& distribution, list<TASK>& unDistribution){
    
    
	for (list<TASK>::iterator it = distribution.begin(); it != distribution.end(); it++)
		if ((*it).name == this->name)
			return true;
	for (list<TASK>::iterator it = unDistribution.begin(); it != unDistribution.end(); it++) {
    
    
		if (this->length <= (*it).length) {
    
    
			this->startAddress = (*it).startAddress;
			for (list<TASK>::iterator it1 = distribution.begin(); it1 != distribution.end(); it1++) {
    
    
				if ((*it1).startAddress >= this->startAddress+this->length) {
    
    
					distribution.insert(it1, *this);
					(*it).length -= this->length;
					if ((*it).length == 0) unDistribution.erase(it);
					(*it).startAddress += this->length;
					return false;
				}
			}
			distribution.push_back(*this);
			(*it).length -= this->length;
			(*it).startAddress += this->length;
			if ((*it).length == 0) unDistribution.erase(it);
			return false;
		}
	}
	return true;
}

void TASK::erase(list<TASK>::iterator& it, list<TASK>& distribution, list<TASK>& unDistribution){
    
    
	for (list<TASK>::iterator i = unDistribution.begin(); i != unDistribution.end(); i++) {
    
    
		if ((this->length + this->startAddress) == (*i).startAddress) {
    
    
			(*i).length += (*it).length;
			(*i).startAddress -= (*it).length;
			if ((*i).length == 0) unDistribution.erase(i);
			distribution.erase(it);
			return;
		}
		if ((*i).startAddress > this->startAddress) {
    
    
			unDistribution.insert(i, *this);
			distribution.erase(it);
			return;
		}
	}
	if (unDistribution.empty()||(this->startAddress != unDistribution.back().startAddress+unDistribution.back().length)) {
    
    
		unDistribution.push_back(*this);
		distribution.erase(it);
	}
	else {
    
    
		unDistribution.back().length += this->length;
		distribution.erase(it);
	}
}

string TASK::toString(){
    
    
	string ans = name + "\t" + itos(startAddress, 10) + "\t" + itos(length, 10) + "\t1";
	return ans;
}

main.cpp

#define _CRT_SECURE_NO_WARNINGS

#include"depend.h"

list<TASK> distribution;//已分配区
list<TASK> unDistribution;//未分配区
vector<PCB*> ready;
vector<PCB*> stop;
int length;//每回处理的时间片长度
int manageState = 1;//进程调度状态
//int startAddress = START_ADDRESS;
//int distributionMax = DISTRIBUTION_MAX;


void connectReady() {
    
    //连接ready内的next
	unsigned int i;
	for (i = 0; i < ready.size() - 1; i++)
		ready[i]->setNext(ready[i + 1]);
	ready[i]->setNext(NULL);
}

void deletePCB(vector<PCB*>& s, string processName) {
    
    
	unsigned int i = 0;
	for (vector<PCB*>::iterator it = s.begin(); it != s.end(); it++, i++) {
    
    
		if (s[i]->getName() == processName) {
    
    
			if (i - 1 >= 0 && i + 1 < s.size())
				s[i - 1]->setNext(s[i + 1]);
			else if (i - 1 < 0 && i + 1 < s.size())
				s[i]->setNext(NULL);
			else if (i - 1 >= 0 && i + 1 >= s.size())
				s[i - 1]->setNext(NULL);
			delete s[i];
			s.erase(it);
			break;
		}
	}
}

void fresh(short x, short y, int len) {
    
    
	SHOW::gotoxy(x, y, "");
	for (int i = 0; i < len; i++)
		cout << " ";
}

string menu() {
    
    
	vector<string> word(8);
	word[0] = "1………创建";
	word[1] = "2………阻塞";
	word[2] = "3………唤醒";
	word[3] = "4………终止";
	word[4] = "5………显示";
	word[5] = "6………调度";
	word[6] = "7………动态分区";
	word[7] = "0………退出";
	SHOW::gotoxy(31, 3, "系统主菜单");
	short x = 30, y = 5;
	for (unsigned int i = 0; i < word.size(); i++, y += 2) {
    
    
		SHOW::gotoxy(x, y, word[i]);
	}
	SHOW::gotoxy(25, y, "请输入您需要的功能(0--7)");
	SHOW::gotoxy(30, y + 2, "输入选择 = ");
	string ans;
	cin >> ans;
	return ans;
}

string menuManage(short x, short y) {
    
    
	string input;
	SHOW::gotoxy(x, y, "0…………返回主菜单");
	y += 2;
	SHOW::gotoxy(x, y, "1…………优先级调度");
	y += 2;
	SHOW::gotoxy(x, y, "2…………时间片调度");
	y += 2;
	SHOW::gotoxy(x - 2, y, "请选择您需要的功能选项:");
	cin >> input;
	return input;
}

string menuDp(short x, short y) {
    
    
	string ans;
	SHOW::gotoxy(x, y, "0………退出");
	y += 2;
	SHOW::gotoxy(x, y, "1………分配");
	y += 2;
	SHOW::gotoxy(x, y, "2………回收");
	y += 2;
	SHOW::gotoxy(x, y, "3………显示");
	y += 2;
	SHOW::gotoxy(x - 4, y, "请选择您需要的功能:");
	cin >> ans;
	return ans;
}

void processShow(string title, short& x, short& y, void (*p)(short& x, short& y, string title)) {
    
    //打表
	SHOW::gotoxy(x, y++, title);
	SHOW::gotoxy(x, y++, "-------------------------------------------------------");
	SHOW::gotoxy(x, y++, "序号\t进程名称\t优先级\t运行时间\t运行状态");
	SHOW::gotoxy(x, y++, "-------------------------------------------------------");
	p(x, y, title);
	SHOW::gotoxy(x, y++, "-------------------------------------------------------");
	SHOW::gotoxy(x, y, "");
	system("pause");
}

void processPageUp(int& count, short& x, short& y, string title) {
    
    
	if (count == 10) {
    
    
		count %= 10;
		y++;
		SHOW::gotoxy(x, y++, "-------------------------------------------------------");
		SHOW::gotoxy(x, y + 1, "");
		system("pause");
		x = 25, y = 4;
		fresh(0, 0, 3000);
		SHOW::gotoxy(x, y++, title);
		SHOW::gotoxy(x, y++, "-------------------------------------------------------");
		SHOW::gotoxy(x, y++, "序号\t进程名称\t优先级\t运行时间\t运行状态");
		SHOW::gotoxy(x, y++, "-------------------------------------------------------");
	}
}

void distributionPageUp(int& count, short& x, short& y, string title) {
    
    
	if (count == 10) {
    
    
		count %= 10;
		y++;
		SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
		SHOW::gotoxy(x, y + 1, "");
		system("pause");
		x = 25, y = 4;
		fresh(0, 0, 3000);
		SHOW::gotoxy(x, y++, title);
		SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
		SHOW::gotoxy(x, y++, "序号\t始址\t长度\t状态\t\t\t作业名\t始址\t长度\t状态");
		SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
	}
}

void distributionShow(string title, short& x, short& y, void (*p)(short& x, short& y, string title)) {
    
    
	SHOW::gotoxy(x, y++, title);
	SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
	SHOW::gotoxy(x, y++, "序号\t始址\t长度\t状态\t\t\t作业名\t始址\t长度\t状态");
	SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
	p(x, y, title);
	SHOW::gotoxy(x, y++, "-----------------------------\t\t\t----------------------------");
	SHOW::gotoxy(x, y, "");
	system("pause");
}

int main() {
    
    
	string input = menu();
	SHOW error(30, 21, "输入错误,请重新输入..");
	unDistribution.push_back(TASK("剩余空间", DISTRIBUTION_MAX, START_ADDRESS));
	while (1) {
    
    
		if (input == "0")
			return 0;
		else if (input == "1") {
    
    //创建
			fresh(0, 0, 3000);
			short x = 25, y = 8;
			int prior, rt, state;
			string processName;
			SHOW in(x, y, "请输入要创建的进程名:"), priority(x, y + 2, "请输入优先级:"), runTime(x, y + 4, "请输入运行时间:");
			in.gotoxy();
			cin >> processName;
			priority.gotoxy();
			cin >> prior;
			runTime.gotoxy();
			cin >> rt;
			y += 6;
			SHOW::gotoxy(x, y, "1-就绪\t2-执行\t3-阻塞");
			y += 2;
			SHOW::gotoxy(x, y, "请输入运行状态:");
			y += 2;
			cin >> state;
			if (state <= 0 || state > 3) {
    
    
				SHOW::gotoxy(x, y, "没有该状态");
				goto end;
			}
			if (state == RUN && run) SHOW::gotoxy(x, y, "已经有程序处于运行状态,请重试。");
			else if (state == RUN && run == NULL && !PCB::lookForward(ready, processName) && !PCB::lookForward(stop, processName)) {
    
    
				run = new PCB(processName, prior, rt, RUN, NULL);
				SHOW::gotoxy(x, y, "进程创建成功!!!");
				run->setState(RUN);
			}
			else {
    
    
				PCB* create = new PCB(processName, prior, rt, state, NULL);
				if ((run && (run->getName() == processName)) || (state == RUN && run != NULL))
					SHOW::gotoxy(x, y, "已有同名进程正在执行!!!");
				else if (state == READY && !run) {
    
    
					char ch;
					SHOW::gotoxy(x, y, "当前没有进程处于执行状态,该状态是否转为执行状态(Y/N):");
					y += 2;
					cin >> ch;
					if (ch == 'Y' || ch == 'y') {
    
    
						run = create;
						run->setState(RUN);
						SHOW::gotoxy(x, y, "已将当前程序转为执行状态。");
					}
					else if (ch == 'N' || ch == 'n') {
    
    
						create->putReady(ready, stop);
						SHOW::gotoxy(x, y, "当前程序已存储为就绪状态。");
					}
					else SHOW::gotoxy(x, y, "输入错误,请重试。");
				}
				else if (state == READY && create->putReady(ready, stop))
					SHOW::gotoxy(x, y, "就绪进程创建成功!!!");
				else if (state == STOP && create->putStop(ready, stop, STOP))
					SHOW::gotoxy(x, y, "阻塞进程创建成功!!!");
				else SHOW::gotoxy(x, y, "队列中已有同名进程!!!");
			}
		end:y += 2;
			SHOW::gotoxy(x, y, "");
			system("pause");
			goto refreash;
		}
		else if (input == "2") {
    
    //阻塞
			short x = 25, y = 8;
			string processName;
			fresh(0, 0, 3000);
			SHOW::gotoxy(x, y, "请输入要阻塞的进程名称:");
			y += 2;
			cin >> processName;
			int readyState = PCB::lookForward(ready, processName), stopState = PCB::lookForward(stop, processName);
			if (readyState || stopState || (run && run->getName() == processName)) {
    
    
				if (run && run->getName() == processName) {
    
    
					run->putStop(ready, stop, RUN);
					SHOW::gotoxy(x, y, "已将运行的进程挪入阻塞队列");
					y += 2;
					if (ready.empty()) SHOW::gotoxy(x, y, "就绪队列已空,没有进程正在执行。");
					else {
    
    //将就绪队列第一个变为运行状态
						run = ready[0];
						run->setNext(NULL);
						run->setState(RUN);
						vector<PCB*>::iterator it = ready.begin();
						ready.erase(it);
					}
				}
				else if (stopState == STOP) SHOW::gotoxy(x, y, "该进程处于阻塞状态。。");
				else if (readyState == READY) SHOW::gotoxy(x, y, "该进程处于就绪状态。。");
			}
			else SHOW::gotoxy(x, y, "没有该进程。");
			y += 2;
			SHOW::gotoxy(x, y, "");
			system("pause");
			goto refreash;
		}
		else if (input == "3") {
    
    //唤醒
			short x = 25, y = 8;
			fresh(0, 0, 3000);
			if (stop.empty()) SHOW::gotoxy(x, y, "阻塞队列为空,不能执行此操作");
			else if (ready.size() == 10) SHOW::gotoxy(x, y, "就绪队列已满,不能执行此操作");
			else {
    
    
				string processName;
				SHOW::gotoxy(x, y, "请输入要唤醒的进程名称:");
				y += 2;
				cin >> processName;
				int readyState = PCB::lookForward(ready, processName), stopState = PCB::lookForward(stop, processName);
				if (readyState || stopState || (run && run->getName() == processName)) {
    
    
					if (run && run->getName() == processName)
						SHOW::gotoxy(x, y, "该进程已经处于运行状态");
					else if (stopState == STOP) {
    
    
						stop.back()->stopToReady(stop, ready);
						SHOW::gotoxy(x, y, "已将该进程移入就绪状态。。");
					}
					else if (readyState == READY) SHOW::gotoxy(x, y, "该进程已处于就绪状态。。");
				}
				else SHOW::gotoxy(x, y, "没有该进程。");
			}
			y += 2;
			SHOW::gotoxy(x, y, "");
			system("pause");
			goto refreash;
		}
		else if (input == "4") {
    
    //终止
			short x = 25, y = 8;
			fresh(0, 0, 3000);
			string processName;
			SHOW::gotoxy(x, y, "请输入要结束的进程名称:");
			y += 2;
			cin >> processName;
			int readyState = PCB::lookForward(ready, processName),
				stopState = PCB::lookForward(stop, processName);
			if (run && run->getName() == processName) {
    
    
				delete run;
				run->setNext(NULL);
				SHOW::gotoxy(x, y, "该进程已释放。");
				y += 2;
				SHOW::gotoxy(x, y, "该进程为运行状态,已经结束。");
				y += 2;
				if (ready.empty()) {
    
    
					SHOW::gotoxy(x, y, "就绪队列已空,没有进程正在执行。");
					run = NULL;
				}
				else {
    
    //将就绪队列第一个变为运行状态
					run = ready[0];
					run->setNext(NULL);
					run->setState(RUN);
					vector<PCB*>::iterator it = ready.begin();
					ready.erase(it);
					SHOW::gotoxy(x, y, run->getName() + " 正在执行。");
				}
			}
			else if (readyState == READY) {
    
    
				deletePCB(ready, processName);
				SHOW::gotoxy(x, y, "该进程为就绪状态,已经结束。");
			}
			else if (stopState == STOP) {
    
    
				deletePCB(stop, processName);
				SHOW::gotoxy(x, y, "该进程为阻塞状态,已经结束。");
			}
			else SHOW::gotoxy(x, y, "没有该进程");
			y += 2;
			SHOW::gotoxy(x, y, "");
			system("pause");
			goto refreash;
		}
		else if (input == "5") {
    
    //显示
			short x = 25, y = 4;
			fresh(0, 0, 3000);
			string title = (manageState == 1) ? "所有进程\t优先级调度状态" : "所有进程\t时间片调度状态";
			processShow(title, x, y, [](short& x, short& y, string title) {
    
    
				int count = 0;
				if (run) {
    
    
					count++;
					SHOW::gotoxy(x, y++, run->toString(count));
				}
				for (unsigned int i = 0; i < ready.size(); i++, y++) {
    
    
					count++;
					SHOW::gotoxy(x, y, ready[i]->toString(count));
					processPageUp(count, x, y, title);
				}
				for (unsigned int i = 0; i < stop.size(); i++, y++) {
    
    
					count++;
					SHOW::gotoxy(x, y, stop[i]->toString(count));
					processPageUp(count, x, y, title);
				}
				});
			goto refreash;
		}
		else if (input == "6") {
    
    //调度
			short x = 25, y = 4;
			fresh(0, 0, 3000);
			if (ready.empty()) {
    
    
				SHOW::gotoxy(25, 6, "无就绪进程无法调度");
				SHOW::gotoxy(25, 8, "");
				system("pause");
				goto refreash;
			}
			string inputManage = menuManage(x, y);
			y += 2;
			while (1) {
    
    
				if (inputManage == "0")
					break;
				else if (inputManage == "1") {
    
    //优先级调度
					fresh(0, 0, 3000);
					string title = (manageState == 1) ? "调度顺序\t优先级调度状态" : "调度顺序\t时间片调度状态";
					processShow(title, x, y, [](short& x, short& y, string title) {
    
    
						int count = 0;
						sort(ready.begin(), ready.end(), [](PCB* a, PCB* b) {
    
    
							return a->getPriority() > b->getPriority();
							});
						connectReady();
						if (!run && ready.size() >= 1) ready[0]->putRun(ready);
						while (run) {
    
    
							count++;
							SHOW::gotoxy(x, y++, run->toString(count));
							delete run;
							if (!ready.empty()) ready[0]->putRun(ready);
							else run = NULL;
							processPageUp(count, x, y, title);
						}
						});
					manageState = 1;
					break;
				}
				else if (inputManage == "2") {
    
    //时间片调度
					fresh(0, 0, 3000);
					SHOW::gotoxy(x, y, "请输入每回处理的时间片长度:");
					cin >> length;
					fresh(0, 0, 3000);
					string title = (manageState == 1) ? "调度顺序\t优先级调度状态" : "调度顺序\t时间片调度状态";
					processShow(title, x, y, [](short& x, short& y, string title) {
    
    
						int count = 0;
						if (!run && ready.size() >= 1) ready[0]->putRun(ready);
						while (run) {
    
    
							count++;
							SHOW::gotoxy(x, y++, run->toString(count));
							int size = run->getRunTime() - length;
							run->setRunTime(size);
							if (run->getRunTime() <= 0) delete run;
							else run->putReady(ready, stop);
							if (!ready.empty()) ready[0]->putRun(ready);
							else run = NULL;
							processPageUp(count, x, y, title);
						}
						});
					break;
				}
				else {
    
    
					fresh(0, 0, 3000);
					inputManage = menuManage(x, y);
				}
			}
			goto refreash;
		}
		else if (input == "7") {
    
    //动态分区
			short x = 25, y = 4;
			fresh(0, 0, 3000);
			string inputDp = menuDp(x, y);
			while (1) {
    
    
				if (inputDp == "0")
					break;
				else if (inputDp == "1") {
    
    //分配
					short x = 25, y = 4;
					string taskName;
					int taskLength;
					bool isVic = false;//是否能够成功插入队尾
					fresh(0, 0, 3000);
					SHOW::gotoxy(x, y, "作业名:");
					cin >> taskName;
					y += 2;
					SHOW::gotoxy(x, y, "作业长度:");
					cin >> taskLength;
					y += 2;
					isVic = TASK(taskName, taskLength).putlist(distribution, unDistribution);//地址有问题
					if (isVic) {
    
    
						SHOW::gotoxy(x, y, "剩余空间不足或作业名重复,请回收作业后再试。");
						y += 2;
						SHOW::gotoxy(x, y, "");
						system("pause");
					}
					else {
    
    
						SHOW::gotoxy(x, y++, "创建成功。。。");
						y++;
						SHOW::gotoxy(x, y, "");
						system("pause");
					}
					goto distri;
				}
				else if (inputDp == "2") {
    
    //回收
					short x = 25, y = 4;
					fresh(0, 0, 3000);
					string taskName;
					SHOW::gotoxy(x, y++, "回收的作业名称:");
					y++;
					cin >> taskName;
					for (list<TASK>::iterator it = distribution.begin(); it != distribution.end(); it++) {
    
    
						if ((*it).name == taskName) {
    
    
							(*it).erase(it, distribution, unDistribution);
							SHOW::gotoxy(x, y, "回收成功。。。");
							y += 2;
							SHOW::gotoxy(x, y, "");
							system("pause");
							goto distri;
						}
					}
					SHOW::gotoxy(x, y, "没有该作业...");
					y += 2;
					SHOW::gotoxy(x, y, "");
					system("pause");
					goto distri;
				}
				else if (inputDp == "3") {
    
    //显示
					short x = 15, y = 4;
					fresh(0, 0, 3000);
					distributionShow("未分配区说明表\t\t\t\t\t已分配区说明表", x, y, [](short& x, short& y, string title) {
    
    
						//显示表的数据部分
						int count = 0;
						list<TASK>::iterator it1 = distribution.begin(), it2 = unDistribution.begin();
						for (; it1 != distribution.end() || it2 != unDistribution.end(); ) {
    
    
							count++;
							string s, s1, s2;
							if (it1 == distribution.end())
								s2 = "";
							else if (it2 == unDistribution.end())
								s1 = "\t\t\t";
							if (it1 != distribution.end()) {
    
    
								s2 = (*it1).toString();
								it1++;
							}
							if (it2 != unDistribution.end()) {
    
    
								s1 = itos(count, 10) + "\t" + itos((*it2).startAddress, 10) + "\t" + itos((*it2).length, 10) + "\t1";
								it2++;
							}
							if (s1 == "\t\t\t") s1 += "\t";
							s = s1 + "\t\t\t" + s2;
							SHOW::gotoxy(x, y++, s);
							distributionPageUp(count, x, y, "未分配区说明表\t\t\t\t\t已分配区说明表");
						}
						});
					goto distri;
				}
				else {
    
    
				distri:fresh(0, 0, 3000);
					inputDp = menuDp(x, y);
				}
			}
			goto refreash;
		}
		else {
    
    
			error.gotoxy();
			SHOW::gotoxy(30, 23, "");
			system("pause");
		refreash:fresh(0, 0, 3000);
			if (manageState == 1 && ready.size() >= 1) {
    
    
				sort(ready.begin(), ready.end(), [](PCB* a, PCB* b) {
    
    
					return a->getPriority() > b->getPriority();
					});
				connectReady();
				ready[0]->putRun(ready);
			}
			input = menu();
		}
	}
	return 0;
}

效果图

效果图
这是一半的效果图,完整的一共有7个选项,表格是可翻页的

猜你喜欢

转载自blog.csdn.net/weixin_43891802/article/details/113091678