#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
struct PCB{
char name[8];
int start;
int sizes;
struct PCB *next;
};
struct SPACE{
char name[8];
bool PH;
int start;
int length;
struct SPACE *next;
struct SPACE *prev;
};
PCB *ready = (PCB *)malloc(sizeof(PCB));
PCB *running = (PCB *)malloc(sizeof(PCB));
PCB *blocked = (PCB *)malloc(sizeof(PCB));
SPACE *head = (SPACE *)malloc(sizeof(SPACE));
int sizee;
void print1();
void add(PCB *head,PCB *process){
PCB *tmp = head;
while(tmp->next!=NULL)
tmp = tmp->next;
tmp->next = process;
process->next = NULL;
}
int SIZEX[200];
int id = 0;
void SORT_()
{
SPACE *p=head->next;
id = 0;
if(p == NULL)
{
SIZEX[0] = NULL;//标记空区表为空
}
while(p!=NULL){//排序
if(p->PH == 0)
{
SIZEX[id++] = p->length;
}
p=p->next;
}
if(id>=2)
sort(SIZEX,SIZEX+id);
}
void sett(){
SORT_();
int i = 0;
int a=0;
if(SIZEX[0] != NULL)
{
PCB *p = (PCB *)malloc(sizeof(PCB));
cout<<"输入进程名字和大小:";
cin>>p->name>>p->sizes;
p->next=NULL;
for(i =0; i<id ; i++)
{
if(SIZEX[i] >= p->sizes)
{
PCB *tmp=running;
if(tmp->next==NULL){
tmp->next=p;
}
else{
add(ready,p);
}
cout<<"进程创建中"<<endl;
break;
}
}
SPACE *temp=head->next;
while(temp!=NULL&&a==0){
if(temp->length == SIZEX[i] && temp ->PH == 0)//找到那块空区
{
if(temp->length - p->sizes <= 2)//如果可以把这块区域整块给出去
{
temp->PH=1;
strcpy(temp->name,p->name);
p->start = temp->start;
a=1;
}
else{//有碎片,新建一个空间块
temp->PH=1;
p->start = temp->start;
strcpy(temp->name,p->name);
SPACE *sp= (SPACE *)malloc(sizeof(SPACE));
strcpy(sp->name,"free");
sp->PH=0;
sp->length=temp->length-p->sizes;
sp->next=temp->next;
temp->length=p->sizes;
temp->next=sp;
sp->start=temp->start+temp->length;
sp->prev=temp;
a=1;
}
}
else
{
temp=temp->next;
}
}
}
if(a==1) cout<<"创建成功"<<endl; print1();
if(a==0) cout<<"error"<<endl;
}
void print1(){
// SPACE *p=head->next;
// cout<<"----------------------------------"<<endl;
// cout<<"内存链表:"<<endl;
// while(p!=NULL){
// cout<<"名字:"<<p->name<<" 起始地址:"<<p->start<<" 内存大小:"<<p->length<<endl;
// p=p->next;
// }
}
void time(){//时间片到,执行到就绪
PCB *tmp=ready;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=running->next;
running->next=ready->next;
ready->next=ready->next->next;
running->next->next=NULL;
}
void block(){//阻塞一个进程,执行到阻塞
PCB *tmp=blocked;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=running->next;
running->next=ready->next;
if(ready->next!=NULL){
ready->next=ready->next->next;
running->next->next=NULL;
}
}
void awaken(){//唤醒一个进程,阻塞到就绪
if(blocked->next!=NULL){
PCB *tmp=ready;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=blocked->next;
blocked->next=blocked->next->next;
tmp->next->next=NULL;
if(running->next == NULL)
{
running->next=ready->next;
ready->next=ready->next->next;
running->next->next=NULL;
}
}
else
cout<<"出错!"<<endl;
}
void stop(){//终止一个进程,回收
int flag = 0;
SPACE *p=head->next;
//cout<<"--------------0------------"<<endl;
while(p!=NULL){
if(strcmp(p->name,running->next->name)==0){//找到正在执行的进程
//cout<<"--------------1------------"<<endl;
p->PH=0;
if(p->next == NULL)//删最后面
{
if(p->prev->PH==1)//上占
strcpy(p->name,"free");
else//上空
{
p=p->prev;
p->length=p->length+p->next->length;
p->next=p->next->next;
}
break;
}
if(p->prev->PH==1&&p->next->PH==1){//上占下占
strcpy(p->name,"free");
break;
}
if(p->prev->PH==1&&p->next->PH==0){//上占下空,长度相加,删下区
p->length=p->length+p->next->length;
if(p->next->next==NULL){//下是最后一个,直接赋空
p->next=NULL;
}
else{
p->next=p->next->next;
p->next->prev=p;
}
strcpy(p->name,"free");
//cout<<"--------------2------------"<<endl;
break;
}
if(p->prev->PH==0 && p->next->PH==1){//上空下占,长度相加,删本区(类似删下区)
p=p->prev;
p->length=p->length+p->next->length;
p->next=p->next->next;
p->next->prev=p;
//cout<<"--------------3------------"<<endl;
break;
}
if(p->prev->PH==0&&p->next->PH==0){//上空下空
p=p->prev;
p->length=p->length+p->next->length+p->next->next->length;
if(p->next->next->next==NULL){
p->next=NULL;
}
else{
p->next=p->next->next->next;
p->next->prev=p;
}
//cout<<"--------------4------------"<<endl;
break;
}
}
p=p->next;
}
print1();
if(ready->next!=NULL){
running->next=ready->next;
ready->next=ready->next->next;
running->next->next=NULL;
}
else
running->next=NULL;
}
void beginn(){
head->length=0;
head->start=0;
head->PH=1;
cout<<"请输入起始地址:";
cin>>head->start;
cout<<"请输入总内存:";
cin>>sizee;
// cout<<"请输入进程数量:";
// cin>>pros;
SPACE *p = (SPACE *)malloc(sizeof(SPACE));
strcpy(p->name,"free");
p->PH=0;
p->start=head->start;
p->length=sizee;
head->next=p;
p->prev=head;
p->next=NULL;
ready->next=NULL;
running->next=NULL;
blocked->next=NULL;
}
void print(){
cout<<"---------------------------------------"<<endl;
cout<<"就绪队列为:";
PCB *tmp=ready;
if(tmp->next==NULL)
cout<<"空"<<endl;
while(tmp->next!=NULL)
{
tmp=tmp->next;
cout<<"名字:"<<tmp->name<<" 大小:"<<tmp->sizes<<" ";
}
cout<<endl<<endl;
cout<<"执行队列为:";
tmp=running;
if(tmp->next==NULL)
cout<<"空"<<endl;
while(tmp->next!=NULL)
{
tmp=tmp->next;
cout<<"名字:"<<tmp->name<<" 大小:"<<tmp->sizes<<" ";
}
cout<<endl<<endl;
cout<<"阻塞队列为:";
tmp=blocked;
if(tmp->next==NULL)
cout<<"空"<<endl;
while(tmp->next!=NULL)
{
tmp=tmp->next;
cout<<"名字:"<<tmp->name<<" 大小:"<<tmp->sizes<<" ";
}
cout<<endl<<endl;
cout<<"---------------------------------------"<<endl;
}
main()
{
cout<<"-----------------------------------------------"<<endl;
cout<<"1、创建新进程"<<endl;
cout<<"2、执行进程时间片到"<<endl;
cout<<"3、阻塞执行进程"<<endl;
cout<<"4、唤醒第一个阻塞进程"<<endl;
cout<<"5、终止执行进程"<<endl;
cout<<"6、结束"<<endl;
cout<<"-----------------------------------------------"<<endl;
int a;
beginn();
cout<<"请输入序号:";
cin>>a;
while(a!=6){
if(a==1)
{
// pro++;
// if(pro<=pros)
// sett();
// else{
// cout<<"进程已达上限!"<<endl;
// break;
// }
sett();
}
if(a==2) time();
if(a==3) block();
if(a==4) awaken();
if(a==5) stop();
print();
cout<<endl;
cout<<"请输入序号:";
cin>>a;
}
}