模拟进程的的三个状态,以及内存的分配和回收
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <list>
#include <deque>
#include <vector>
#include <set>
using namespace std;
class Process{
public:
int name;
int memory_start_addr;
int length;
};
class memory{
public:
int start;
int length;
memory(int a ,int b):start(a),length(b){}
};
const int fragment_size = 2;
deque<Process> ready,exe,block;
list<memory> left_memory;
set<int> Process_id;
int memory_start_addr;
void help(){
cout<<"1:H/h帮助命令,显示命令作用"<<endl;
cout<<"2: G/g创建进程,给出进程名和大小"<<endl;
cout<<"3: E/e终止进程"<<endl;
cout<<"4: T/t时间片到"<<endl;
cout<<"5: B/b阻塞进程"<<endl;
cout<<"6: W/w唤醒进程"<<endl;
cout<<"7: S/s查看进程队列情况"<<endl;
cout<<"8: K/k:查看剩余内存情况"<<endl;
cout<<"9: Q/q:终止程序"<<endl;
}
void init(int start , int end){
left_memory.push_back(memory(start,end));
}
bool distribute_memory(Process &temp,int name,int length){
list<memory>::iterator ite,min;
bool ok = false;
for(ite = left_memory.begin(); ite != left_memory.end(); ++ite){
if((*ite).length >= length){
if(ok == false){
min = ite;
}
else min = (*min).length < (*ite).length ? min : ite;
ok = true;
}
}
if(!ok){
cout<<"内存已耗尽"<<endl;
return false;
}
else {
if((*min).length - length > fragment_size){
temp.name = name;
temp.length = length;
temp.memory_start_addr = (*min).start;
memory temp2((*min).start + length, (*min).length - length);
left_memory.insert(min,temp2);
left_memory.erase(min);
}
else {
temp.name = name;
temp.length = (*min).length;
temp.memory_start_addr = (*min).start;
left_memory.erase(min);
}
return true;
}
}
void creat_process(){
int name;
int length;
cin>>name>>length;
if(Process_id.count(name)){
cout<<"该进程已经存在"<<endl;
}
else {
Process_id.insert(name);
Process temp;
bool flag = distribute_memory(temp,name,length);
if(flag == 0){
cout<<"进程创建失败"<<endl;
}
else {
ready.push_back(temp);
}
}
}
void auto_manage_memory(){
if(exe.empty() && !ready.empty()){
// cout<<ready.size()<<endl;
exe.push_back(ready.front());
ready.pop_front();
}
}
void end_process(){
if(exe.empty()){
cout<<"无正在运行的进程"<<endl;
return;
}
Process temp = exe.front();
Process_id.erase(temp.name);
exe.pop_front();
list<memory>::iterator ite;
for(ite = left_memory.begin(); ite != left_memory.end(); ++ite){
if(temp.memory_start_addr + temp.length == (*ite).start){
(*ite).start = temp.memory_start_addr;
(*ite).length = (*ite).length + temp.length;
break;
}
else if((*ite).start + (*ite).length == temp.memory_start_addr){
(*ite).length = (*ite).length + temp.length;
break;
}
}
if(ite == left_memory.end()){
if(left_memory.empty()){
left_memory.push_back(memory(temp.memory_start_addr,temp.length));
}
else {
for(ite = left_memory.begin(); ite != left_memory.end(); ++ite){
if(temp.memory_start_addr < (*ite).start){
left_memory.insert(ite,memory(temp.memory_start_addr,temp.length));
}
}
}
}
auto_manage_memory();
}
void time_end(){
if(exe.empty()){
cout<<"无正在运行的进程"<<endl;
return;
}
ready.push_back(exe.front());
exe.pop_front();
auto_manage_memory();
}
void block_process(){
if(exe.empty()){
cout<<"无进程可以阻塞"<<endl;
return;
}
cout<<"阻塞进程"<<exe.front().name<<endl;
block.push_back(exe.front());
exe.pop_front();
auto_manage_memory();
}
void wake_process(){
if(block.empty()){
cout<<"无可唤醒的进程"<<endl;
return;
}
cout<<"唤醒进程:"<<block.front().name<<endl;
ready.push_back(block.front());
block.pop_front();
auto_manage_memory();
}
void check_queue(){
auto_manage_memory();
deque<Process>::iterator ite;
cout<<"就绪状态:";
for(ite = ready.begin(); ite != ready.end(); ++ite){
cout<<"<"<<(*ite).name<<","<<(*ite).length<<">";
}
cout<<endl;
cout<<"执行状态:";
if(!exe.empty()) cout<<"<"<<exe.front().name<<","<<exe.front().length<<">";
cout<<endl;
cout<<"阻塞状态:";
for(ite = block.begin(); ite != block.end(); ++ite){
cout<<"<"<<(*ite).name<<","<<(*ite).length<<">";
}
cout<<endl;
}
void check_left_memory_memory(){
list<memory>::iterator ite,temp;
for(ite = left_memory.begin(); ite != left_memory.end();){
temp = ite;
if((*ite).start + (*ite).length == (*(++ite)).start){
(*temp).length = (*temp).length + (*ite).length;
left_memory.erase(ite);
ite = ++temp;
}
}
for(ite = left_memory.begin(); ite != left_memory.end(); ++ite){
cout<<"<"<<(*ite).start<<","<<(*ite).length<<">"<<endl;
}
// cout<<endl;
}
int main()
{
cout<<"请给内存分配起始地址和大小:"<<endl;
int Size;
cin>>memory_start_addr>>Size;
// memory_start_addr = 1;
// Size = 500;
init(memory_start_addr,Size);
char ch;
bool flag = true;
while(1){
cout<<"$:";
cin>>ch;
if(ch >= 'A' && ch <= 'Z') ch += 32;
switch(ch){
case 'h': help();break;
case 'g': creat_process();break;
case 'e': end_process();break;
case 't': time_end();break;
case 'b': block_process();break;
case 'w': wake_process();break;
case 's': check_queue();break;
case 'k': check_left_memory_memory();break;
case 'q': flag = false;break;
default : cout<<"无此命令"<<endl;
}
if(!flag) break;
}
return 0;
}