資源簡介
設計程序模擬內存的動態分區法存儲管理。內存空閑區使用自由鏈管理,采用最壞適應算法從自由鏈中尋找空閑區進行分配,內存回收時假定不做與相鄰空閑區的合并。
假定系統的內存共640K,初始狀態為操作系統本身占用64K。在t1時間之后,有作業A、B、C、D分別請求8K、16K、64K、124K的內存空間;在t2時間之后,作業C完成;在t3時間之后,作業E請求50K的內存空間;在t4時間之后,作業D完成。要求編程序分別輸出t1、t2、t3、t4時刻內存的空閑區的狀態。
代碼片段和文件信息
#include?
#include?
#include?
#define?NULL?0
struct?freelink{
?????int?len?address;??//??len為分區長度??address為分區起始地址?
?????struct?freelink?*next;
};
//內存占用區用鏈表描述,其結點類型描述如下:
struct?busylink{
?????char?name;???//?作業或進程名??name=‘S‘?表示OS占用?
?????int?len??address;
?????struct?busylink?*next;
};
//并設全程量:
struct?freelink?*free_head=NULL;??????//自由鏈隊列(帶頭結點)隊首指針?????????
struct?busylink?*busy_head=NULL????//占用區隊列隊(帶頭結點)首指針?????????????????
????????????????*busy_tail=NULL;???//占用區隊列隊尾指針
void?start(void);???/*?設置系統初始狀態*/?
void?requireMemo(char??name?int??require);?/*模擬內存分配*/
void?freeMemo(char?name);??/*?模擬內存回收*/?
void?past(int??time);??/*?模擬系統過了time?時間*/
void?printlink();??/*?輸出內存空閑情況(自由鏈的結點)?*/?
void?main()
{???
int?t1=1t2=2t3=3t4=4;
start();
????past(t1);
????requireMemo(‘A‘8);??requireMemo(‘B‘16);?
????requireMemo(‘C‘64);?requireMemo(‘D‘124);//A、B、C、D分別進入
????printlink();//顯示t1時刻內存空閑區
????past(t2);
????freeMemo(‘C‘);//C結束
????printlink();//顯示t2時刻內存空閑區
????past(t3);
????requireMemo(‘E‘50);//E進入
????printlink();//顯示t3時刻內存空閑區
past(t4);
????freeMemo(‘D‘);//D結束
????printlink();//顯示t4時刻內存空閑區
}
void?start()
{?????
struct?freelink?*p;
????struct?busylink?*q;
????free_head=(struct??freelink*)malloc(sizeof(struct??freelink));
????free_head->next=NULL;??//?創建自由鏈頭結點
????busy_head=busy_tail=(struct??busylink*)malloc(sizeof(struct??busylink));
????busy_head->next=NULL;??//?創建占用鏈頭結點
????p=(?struct?freelink?*)malloc(sizeof(struct?freelink));
????p->address=64;
????p->len=640-64;??//(OS占用了64K)
????p->next=NULL;?
????free_head->next=p;
????q=(?struct??busylink?*)malloc(sizeof(struct?busylink));
????q->name=‘S‘;??/*??
- 上一篇:atl連接點、c++接收器、js接收器
- 下一篇:車位管理系統
評論
共有 條評論