📄 os2.cpp
字号:
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>
#define free 0;//可用
#define busy 1;//不可用
#define null 0;
struct node{ int name;
int size;
int address_begin;
int address_end;
int state;
};
struct queue{ struct node p;
struct queue *front;
struct queue *rear;
};
struct queue *head,*find;
void initial()//初始化一个size1000的空间
{
head=(struct queue*)malloc(sizeof(struct node));
find=head;
head->p.name=1;
head->p.size=1000;
head->p.address_begin=0;
head->p.address_end=1000;
head->front=head;
head->rear=null;
head->p.state=free;
}
void creat()//创建一个进程并为该进程申请一个空间
{
struct queue *t;
int n;
cout<<"please enter the size"<<endl;
cin>>n;
t=find;
while(n<t->p.size)
{
if(!t)
cout<<"分配失败,找不到足够大的空间"<<endl;
else if(n>t->p.size) t=t->rear; // 最佳适应算法
else
{
if((t->p.size-n)>10)
{
//先处理剩余空间然后修改原来的节点的信息即可
struct queue *k;
//把剩余的空间化为一个节点并连入队列
k=(struct queue*)malloc(sizeof(struct node));
k->p.size=t->p.size-n;
k->p.address_begin=t->p.address_begin+n;
k->p.address_end=t->p.address_end;
k->p.state=free;
//节点进栈
if(t==head)
{ find=k;
find->front=t;
t->rear=find;
find->rear=null;
}
else
{
struct queue *r;
r=find;
while((r->p.size<k->p.size)&&(r->rear))
r=r->rear;
if(r==find)
{ find=k;
find->front=t;
t->rear=find;
find->rear=null;
}
else
{
r->front=k;
k->front=r->front;
k->rear=r;
r->front->rear=k;
}
}
// 修改使用节点的信息
int name1;
cout<<"enter the program's name"<<endl;
cin>>name1;
t->p.name=name1;
t->p.size=n;
t->p.address_end=t->p.address_begin+n;
t->p.state=busy;
}
else
{
//如果剩余空间过小就不再划分,直接修改节点信息
int name1;
cout<<"enter the program's name"<<endl;
cin>>name1;
t->p.name=name1;
t->p.size=n;
t->p.state=busy;
}
}
}
}
//-------------------------------------------
void exit(int e)//结束一个进程并释放它发空间
{ struct queue *t,*p1,*p2;
t=head;
while(t->p.name!=e)
t=t->rear;
if(!t) cout<<"找不到该进程"<<endl;
else
{
p1=t->front;//要释放的节点的前一个节点
p2=t->rear;//要释放的节点的后一个节点
if((p1->p.state==0)&&(p2->p.state==0))//要释放的节点的前后节点都free
{
p1->p.size=t->p.size+p2->p.size+p1->p.size;
p1->rear=p2->rear;
p2->rear->front=p1;
p1->p.address_end=p2->p.address_end;
}
else if(p1->p.state==0)//要释放的节点的前节点free
{
p1->p.size=t->p.size+p1->p.size;
p1->rear=p2;
p2->front=p1;
p1->p.address_end=p2->p.address_end;
}
else if(p2->p.state==0)//要释放的节点的后节点free
{
t->p.state=free;
t->p.size=t->p.size+p2->p.size;
t->rear=p2->rear;
if(p2->rear)
p2->rear->front=t;
t->p.address_end=p2->p.address_end;
}
else t->p.state=free;//一般情况,直接修改state状态即可
}
}
//-------------------------------------------
void show() //显示内存的使用情况
{
struct queue *r;
r=head;
cout<<"show the information"<<endl;
while(r)
{
cout<<"the name is "<<r->p.name<<endl;
cout<<"the size is "<<r->p.size<<endl;
cout<<"the address is from "<<r->p.address_begin;
cout<<" to "<<r->p.address_end<<endl;
cout<<"the state is "<<r->p.state<<endl;
r=r->rear;
cout<<"-------------------------------"<<endl;
}
}
//-------------------------------------------
void main()
{
int i=1;
initial();
char s;
while(i)
{
cout<<"************************************"<<endl;
cout<<"creat a program please enter c "<<endl;
cout<<"stop a progam please enter e "<<endl;
cout<<"show the queue information enter s"<<endl;
cout<<"************************************"<<endl;
cin>>s;
switch(s)
{
case 'c': creat();break;
case 'e': int name0;
cout<<"输进你要结束的进程的名字"<<endl;
cin>>name0;
exit(name0);
break;
case 's': show();
default: break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -