动态内存分配模拟
#include<stdio.h>
#include<stdlib.h>
struct nodespace{int teskid; // 任務號 int begin; // 開始地址 int size; // 大小 int status; // 狀態 0代表占用,1代表空閑 struct nodespace *next; // 后指針
};void initNode(struct nodespace *p){if(p == NULL){ //如果為空則新創建一個 p = (struct nodespace*)malloc(sizeof(struct nodespace));}p->teskid = -1;p->begin = 0;p->size = 640;p->status = 1;p->next =NULL;
}/*
* 首次適應算法
*/
void myMalloc1(int teskid,int size,struct nodespace *node){while(node != NULL){if(node->status == 1){ //空閑的空間 if(node->size > size){ //當需求小于剩余空間充足的情況 //分配后剩余的空間 struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));p->begin = node->begin + size;p->size = node->size - size;p->status = 1;p->teskid = -1;//分配的空間 node->teskid = teskid; node->size = size;node->status = 0;//改變節點的連接 p->next = node->next; node->next = p;break; }else if(node->size == size){ //需求空間和空閑空間大小相等時 node->teskid = teskid; node->size = size;node->status = 0;break;} }if(node->next == NULL){printf("分配失敗,沒有足夠的空間!\n");break;}node = node->next;}
} /*
* 最佳適應算法
*/
void myMalloc2(int teskid,int size,struct nodespace *node){//最佳塊指針 struct nodespace *q = NULL;//首先找到第一個滿足條件的空閑塊 while(node != NULL){if(node->status == 1 && node->size >= size){q = node;break;}//如果下一個為空則說明沒有空閑區可以分配 if(node->next == NULL){printf("分配失敗,沒有足夠的空間!\n");break;} else{node = node->next; }} //遍歷尋找最佳的空閑塊 while(node != NULL){if(node->status == 1 && node->size >= size && node->size < q->size){ //空閑的空間 q = node;}node = node->next;}if(q->size > size){ //最佳空閑塊的大小大于需求大小 //分配后剩余的空間 struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));p->begin = q->begin + size;p->size = q->size - size;p->status = 1;p->teskid = -1;//分配的空間 q->teskid = teskid; q->size = size;q->status = 0;//改變節點的連接 p->next = q->next; q->next = p;}else if(q->size == size){ //最佳空閑塊空間大小和需求相等q->teskid = teskid; q->size = size;q->status = 0;}
}void myFree(int teskid,struct nodespace *node){if(node->next == NULL && node->teskid == -1){printf("還沒有分配任何任務!\n");}while(node != NULL){if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){ //釋放空間的上一塊空間空閑時 node->size = node->size + node->next->size;struct nodespace *q = node->next;node->next = node->next->next;free(q);if(node->next->status == 1){ //下一個空間是空閑空間時 node->size = node->size + node->next->size;struct nodespace *q = node->next;node->next = node->next->next;free(q);}break;}else if(node->status == 0 && node->teskid == teskid){ //釋放空間和空閑空間不連續時 node->status = 1;node->teskid = -1;if(node->next != NULL && node->next->status == 1){ //下一個空間是空閑空間時 node->size = node->size + node->next->size;struct nodespace *q = node->next;node->next = node->next->next;free(q);}break;}else if(node->next == NULL){ //任務id不匹配時 printf("沒有此任務!\n");break;}node = node->next;}}void printNode(struct nodespace *node){printf(" 內存情況 \n"); printf(" -------------------------------------------------------\n");printf("| 起始地址\t結束地址\t大小\t狀態\t任務id\t|\n");while(node != NULL){if(node->status==1){printf("| %d\t\t%d\t\t%dKB\tfree\t 無\t|\n", node->begin + 1, node->begin+node->size, node->size);}else{printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|\n", node->begin + 1, node->begin+node->size, node->size, node->teskid);}node = node->next;}printf(" -------------------------------------------------------\n");
}void destory(struct nodespace *node){struct nodespace *q = node;while(node != NULL){node = node->next;free(q);q = node;}
}void menu(){printf("1.分配內存\n");printf("2.回收內存\n");printf("3.查看內存情況\n");printf("4.退出\n");printf("請輸入選項:");
}int main(){// node為整個空間 struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace));struct nodespace *node = NULL;initNode(init); //初始化主鏈 node = init; //指向鏈表頭 int option; int teskid;int size;while(1){printf("請選擇模式:\n 1.演示模式\n 2.自由模式\n 3.退出\n");scanf("%d",&option);if(option == 1){ //演示模式 while(1){ //循環選擇實現的算法 printf("請選擇算法:\n 1.首次適應算法\n 2.最佳適應算法\n 3.退出\n");scanf("%d",&option);if(option == 1){ //首次適應算法 printf("作業1 申請130 KB\n");myMalloc1(1,130,node); //作業1 申請130 KBprintNode(node);printf("作業2 申請60 KB\n");myMalloc1(2,60,node); //作業2 申請60 KBprintNode(node);printf("作業3 申請100 KB\n");myMalloc1(3,100,node); //作業3 申請100 KBprintNode(node);printf("作業2 釋放60 KB\n");myFree(2,node); //作業2 釋放60 KBprintNode(node);printf("作業4 申請200 KB\n");myMalloc1(4,200,node); //作業4 申請200 KBprintNode(node);printf("作業3 釋放100 KB\n");myFree(3,node); //作業3 釋放100 KBprintNode(node);printf("作業1 釋放130 KB\n");myFree(1,node); //作業1 釋放130 KBprintNode(node);printf("作業5 申請140 KB\n");myMalloc1(5,140,node); //作業5 申請140 KBprintNode(node);printf("作業6 申請60 KB\n");myMalloc1(6,60,node); //作業6 申請60 KBprintNode(node);printf("作業7 申請50 KB\n");myMalloc1(7,50,node); //作業7 申請50 KBprintNode(node);printf("作業6 釋放60 KB\n");myFree(6,node); //作業6 釋放60 KBprintNode(node);destory(node); //銷毀鏈表initNode(init); //重新初始化 node = init; //重新指向開頭 }else if(option == 2){ //最佳適應算法 printf("作業1 申請130 KB\n");myMalloc2(1,130,node); //作業1 申請130 KBprintNode(node);printf("作業2 申請60 KB\n");myMalloc2(2,60,node); //作業2 申請60 KBprintNode(node);printf("作業3 申請100 KB\n");myMalloc2(3,100,node); //作業3 申請100 KBprintNode(node);printf("作業2 釋放60 KB\n");myFree(2,node); //作業2 釋放60 KBprintNode(node);printf("作業4 申請200 KB\n");myMalloc2(4,200,node); //作業4 申請200 KBprintNode(node);printf("作業3 釋放100 KB\n");myFree(3,node); //作業3 釋放100 KBprintNode(node);printf("作業1 釋放130 KB\n");myFree(1,node); //作業1 釋放130 KBprintNode(node);printf("作業5 申請140 KB\n");myMalloc2(5,140,node); //作業5 申請140 KBprintNode(node);printf("作業6 申請60 KB\n");myMalloc2(6,60,node); //作業6 申請60 KBprintNode(node);printf("作業7 申請50 KB\n");myMalloc2(7,50,node); //作業7 申請50 KBprintNode(node);printf("作業6 釋放60 KB\n");myFree(6,node); //作業6 釋放60 KBprintNode(node);destory(node); //銷毀鏈表initNode(init); //重新初始化 node = init; //重新指向開頭 }else if(option == 3){ //退出break;}else{printf("您的輸入有誤,請重新輸入!\n"); }} }else if(option == 2){ //自由模式 while(1){ //循環選擇使用的算法 printf("請選擇算法:\n 1.首次適應算法\n 2.最佳適應算法\n 3.退出\n");scanf("%d",&option);int n = option; //標記選擇的算法,n == 1 表示首次適應算法, n == 2表示最佳適應算法 if(option != 3){while(1){menu(); //打印想要進行的操作 scanf("%d",&option);if(option == 1 && n == 1){ //首次適應 printf("請輸入任務id以及申請的空間大小:\n");scanf("%d%d",&teskid,&size);myMalloc1(teskid,size,node);printNode(node);}else if(option == 1 && n == 2){ //最佳適應 printf("請輸入任務id以及申請的空間大小:\n");scanf("%d%d",&teskid,&size);myMalloc2(teskid,size,node);printNode(node);}else if(option == 2){printf("請輸入任務id:\n");scanf("%d",&teskid);myFree(teskid,node);printNode(node);}else if(option == 3){printNode(node);}else if(option == 4){destory(node); //銷毀鏈表initNode(init); //重新初始化 node = init; //重新指向開頭 break;}else{printf("您的輸入有誤,請重新輸入!\n");continue;}}}else if(option == 3){destory(node); //銷毀鏈表initNode(init); //重新初始化 node = init; //重新指向開頭 break;}else{printf("您的輸入有誤,請重新輸入!\n");}} }else if(option == 3){ //退出 destory(node);return 0;}else {printf("您的輸入有誤,請重新輸入!\n");continue;}}return 0;
}
總結
- 上一篇: 喝鲜奶拉肚子为什么?
- 下一篇: C语言 实现登录注册功能