-
大小: 7KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-06-05
- 語(yǔ)言: C/C++
- 標(biāo)簽:
資源簡(jiǎn)介
能夠輸入給定的內(nèi)存大小,進(jìn)程的個(gè)數(shù),每個(gè)進(jìn)程的段數(shù)及段大小;
要求當(dāng)某進(jìn)程提出申請(qǐng)空間的大小后,顯示能否滿足申請(qǐng),以及為該進(jìn)程分配資源后有關(guān)內(nèi)存空間使用的數(shù)據(jù)(注意回收后的合并)。
代碼片段和文件信息
#include
#include
#include
#define??neicun??1024
using?namespace?std;
struct?kongxian{
unsigned?int?length;
unsigned?int?addr;
????????int?flag;
????????kongxian?*next;
};
struct?Duanbiao{
int?jincheng;
unsigned?int?duannum;
unsigned?int?addr;
unsigned?int?length;
int?flag;?//標(biāo)識(shí)所請(qǐng)求的段在內(nèi)存還是外存中
????????Duanbiao?*next;
};
kongxian?*Khead;//空閑鏈表頭指針
kongxian?*creatK();//創(chuàng)建初始空閑區(qū)
Duanbiao?*Dhead;//請(qǐng)求段表頭指針
Duanbiao?*Dfenpei;//已分配鏈表頭指針
Duanbiao?*qingqiuD(Duanbiao?*head);//輸入請(qǐng)求的進(jìn)程及各段
kongxian?*zuixian(Duanbiao*?head);
kongxian*?sort(kongxian?*head);
int?huishou();
int?select;
char?ch;
int?jinchengming;
int?jinchenghuishou();
int?duanhuishou();
int?Display();
void?space(int?n);
int?main()
{
cout<<“\t******************************************************“< ????cout<<“\t***????????模擬段式存儲(chǔ)管理的分配與回收!???????????***“< ????cout<<“\t******************************************************“< Dfenpei=NULL;?//已分配鏈表頭指針
Khead=NULL;??//空閑鏈表頭指針
Khead=creatK();??//創(chuàng)建初始空閑區(qū)
Dhead=NULL;??//請(qǐng)求段表頭指針
do
{
cout< space(5);
cout<<“1.分配存儲(chǔ)空間“< space(5);
cout<<“2.回收存儲(chǔ)空間“< space(5);
cout<<“3.顯示存儲(chǔ)資源狀況“< ????????space(5);
cout<<“4.退出“<
cin>>select;
switch(?select?)
{
case?1:
Dhead=qingqiuD(Dhead);//創(chuàng)建申請(qǐng)鏈表
Khead=zuixian(Dhead);//為申請(qǐng)鏈表分配空間
break;
case?2:
huishou();
break;
case?3:
Display();
break;
case?4:
break;
default:
cout<<“輸入錯(cuò)誤請(qǐng)重新選擇操作!“< break;
}
}while(select!=4);
return?0;
}
kongxian?*creatK()
{
kongxian?*p=new?kongxian;
p->length=neicun;
p->addr=0;
p->flag=0;
p->next=NULL;
return?p;
}
Duanbiao*?qingqiuD(Duanbiao*?head)
{
Duanbiao?*p1*p2;
p1=p2=new?Duanbiao;
cout<<“請(qǐng)輸入請(qǐng)求資源的進(jìn)程號(hào):“;
cin>>jinchengming;
cout<<“請(qǐng)輸入段號(hào)及長(zhǎng)度\n“;
cout<<“段號(hào)“< cin>>p1->duannum>>p1->length;
p1->next=NULL;
if(head!=NULL)
{
p2=head;
while(p2->next!=NULL)p2=p2->next;//p2指向已分配表的最后一個(gè)結(jié)點(diǎn)
}
while(p1->duannum!=-1)
{
p1->jincheng=jinchengming;
p1->flag=0;
if(head==NULL)head=p1;
else?p2->next=p1;
p2=p1;
p1=new?Duanbiao;
p1->next=NULL;
????????cin>>p1->duannum>>p1->length;
}
delete?p1;
return?head;
}
kongxian?*zuixian(Duanbiao*?head)
{
kongxian?*pK;//空閑鏈表頭指針
Duanbiao?*p1*p2;??//已分配鏈表結(jié)點(diǎn)
Duanbiao?*pD=head;//請(qǐng)求段表頭指針
Duanbiao?*pf=Dfenpei;
p1=p2=new?Duanbiao;
p1->next=NULL;
if(Dfenpei!=NULL)
{
p2=Dfenpei;
while(p2->next!=NULL)p2=p2->next;//p2指向已分配表的最后一個(gè)結(jié)點(diǎn)
}
for(pD;pD!=NULL;pD=pD->next)
{??
pK=Khead;
for(pK;pK!=NULL;pK=pK->next)//從空閑區(qū)表順序查找
{
if(pK->length>=pD->length)
{
p1->flag=1;//表示在內(nèi)存中
p1->jincheng=pD->jincheng;
p1->duannum=pD->duannum;
p1->addr=pK->addr;
p1->length=pD->length;
if(Dfenpei==NULL)Dfenpei=p1;
else?p2->next=p1;
p2=p1;
p1=new?Duanbiao;
p1->nex
評(píng)論
共有 條評(píng)論