首次适应算法 C语言实现
生活随笔
收集整理的這篇文章主要介紹了
首次适应算法 C语言实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
廣東工業(yè)大學(xué) 操作系統(tǒng)實(shí)驗(yàn)
用C語言實(shí)現(xiàn)采用首次適應(yīng)算法的動(dòng)態(tài)分區(qū)分配過程和回收過程。
其中,空閑分區(qū)通過空閑分區(qū)鏈(表)來管理;在進(jìn)行內(nèi)存分配時(shí),系統(tǒng)優(yōu)先使用空閑區(qū)低端的空間,要求每次分配和回收后顯示出空閑內(nèi)存分區(qū)鏈的情況。
#include <stdio.h> #include <stdlib.h> #include <stdbool.h>#define FREE 0 #define BUSY 1 #define Max_length 640typedef struct freearea//空閑區(qū)的結(jié)構(gòu)體 {int ID;//分區(qū)號(hào)int size;//分區(qū)大小int address;//分區(qū)地址bool isUsed;//使用狀態(tài),0為未占用,1為已占用 } freearea;typedef struct DuNode//首尾不互連的雙向鏈表結(jié)點(diǎn) {freearea data;//數(shù)據(jù)域struct DuNode *prior;//指針域struct DuNode *next; } DuNode, *DuLinkList;DuLinkList m_rid; DuLinkList m_last;void init()//空閑區(qū)隊(duì)列初始化 {m_rid = (DuLinkList)malloc(sizeof(DuNode));m_last = (DuLinkList)malloc(sizeof(DuNode));m_rid->prior = NULL;m_rid->next = m_last;m_last->prior = m_rid;m_last->next = NULL;m_rid->data.size = 0;m_rid->data.isUsed = BUSY; //首結(jié)點(diǎn)不會(huì)被使用,定義為占用狀態(tài)防止分區(qū)合并失敗m_last->data.address = 0;m_last->data.size = Max_length;m_last->data.ID = 0;m_last->data.isUsed = 0; }int first_fit(int ID,int size)//首次適應(yīng)算法 {DuLinkList temp = (DuLinkList)malloc(sizeof(DuNode));DuNode *p = m_rid->next;temp->data.ID=ID;temp->data.size=size;temp->data.isUsed=BUSY;while(p){if(p->data.ID == ID)//不允許存在同名作業(yè){printf("該作業(yè)號(hào)對(duì)應(yīng)的作業(yè)已經(jīng)在內(nèi)存中!");return 0;}if (p->data.isUsed==FREE && p->data.size==size)//請(qǐng)求大小剛好滿足{p->data.isUsed=BUSY;p->data.ID=ID;return 1;}if (p->data.isUsed==FREE && p->data.size>size)//空閑區(qū)比所需內(nèi)存大,則需要將多的內(nèi)存作回收處理{temp->next=p;temp->prior=p->prior;temp->data.address=p->data.address;p->prior->next=temp;p->prior=temp;p->data.address=temp->data.address+temp->data.size;p->data.size-=size;return 1;}p=p->next;}return 0; }void alloc()//分配內(nèi)存 {int ID,size1;printf("請(qǐng)輸入作業(yè)號(hào):");scanf("%d", &ID);printf("請(qǐng)輸入所需內(nèi)存大小:");scanf("%d", &size1);if (ID<=0 || size1<=0)printf("錯(cuò)誤!請(qǐng)輸入正確的作業(yè)號(hào)和請(qǐng)求的內(nèi)存大小");if(first_fit(ID,size1))printf("分配內(nèi)存成功!\n");elseprintf("分配內(nèi)存失敗!\n"); }void freeNode()//釋放內(nèi)存 {int ID;DuNode *p = m_rid->next;printf("輸入需要釋放內(nèi)存的作業(yè)號(hào):");scanf("%d", &ID);while (p){if (p->data.ID == ID){p->data.ID = 0;p->data.isUsed = FREE;if (!p->prior->data.isUsed && p->next->data.isUsed)//與前一個(gè)空閑區(qū)相鄰,則合并{p->prior->data.size += p->data.size;p->prior->next = p->next;p->next->prior = p->prior;}if (!p->next->data.isUsed && p->prior->data.isUsed) //與后一個(gè)空閑區(qū)相鄰,則合并{p->data.size += p->next->data.size;if(p->next->next){p->next->next->prior=p;p->next = p->next->next;}elsep->next = p->next->next;}if(!p->prior->data.isUsed && !p->next->data.isUsed) //前后的空閑區(qū)均為空{p->prior->data.size += p->data.size + p->next->data.size;if(p->next->next){p->next->next->prior = p->prior;p->prior->next = p->next->next;}elsep->prior->next = p->next->next;}printf("釋放內(nèi)存成功!\n");break;}p = p->next;if(!p)printf("內(nèi)存中沒有該需要釋放內(nèi)存的作業(yè)!");} }void show() {printf("------------------");printf("內(nèi)存分配情況");printf("------------------\n");DuNode *p = m_rid->next;while(p){printf("分區(qū)號(hào):");if (p->data.ID==FREE)printf("FREE\n");elseprintf("%d \n", p->data.ID);printf("起始地址:%d\n", p->data.address);printf("內(nèi)存大小:%d\n", p->data.size);printf("分區(qū)狀態(tài):");if (p->data.isUsed==FREE)printf("空閑\n");elseprintf("已分配\n");printf("------------------\n");p=p->next;} }int main() {printf("------------------");printf("首次適應(yīng)算法");printf("------------------\n");init();int tag = 1;while(tag < 3 && tag > 0){printf("輸入要進(jìn)行的操作");printf("(1-分配內(nèi)存,2-內(nèi)存釋放,其他-退出程序):");scanf("%d", &tag);switch(tag){case 1:alloc();show();break;case 2:freeNode();show();break;default:printf("程序已退出!");}} }總結(jié)
以上是生活随笔為你收集整理的首次适应算法 C语言实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python Network(二)绘图d
- 下一篇: 中国富人的身影:世界最大楼市泡沫在加拿大