模擬動態分區分配

介紹

list.h

#ifndef _List_h_
#define _List_h_#include "Data.h"//*******           鏈表            *******//
Status InitLinkList(LinkList *L);
void PCBAssign(PCBType *e1, PCBType e2);
Status GetElemt_L(LinkList L,int i,PCBType *e);
Status ListInsert_L(LinkList L,PCBType e);
Status ListDelete_L(LinkList L,int i,PCBType *e);//******         動態順序表           ******//
void PartiAssign(PartiType *e1, PartiType e2);
Status InitList_Sq(SqList *L);
Status ListInsert_Sq(SqList *L,int i,PartiType e);
Status ListDelete_Sq(SqList *L,int i,PartiType *e);#endif

MemoryManage.h

#ifndef _MemoryManage_h_
#define _MemoryManage_h_#include "List.h"//*****         PCB鏈表操作        *****//
Status InsertProcess(LinkList Q,PCBType e);
Status DeleteProsess(LinkList Q,int i,PCBType *e);
//*****         分區表操作        *****//
Status InsertTable(SqList *L, int i, PartiType e);
Status DeleteTable(SqList *L, int i, PartiType *e);
int SelectPart(PCB* pPCB, SqList *pPartTable, AllocatStrategy AS);
int MallocMemory(PCB *pe, SqList *pPartTable,int i);
void SearchSpace(PCBList PCBdata, SqList *partTable, AllocatStrategy AS);
void FreeMemory(int pos, SqList *pPartTable);
void InitAllocation(PCBList PCBdata, PartTable *partTable, AllocatStrategy AS);
void PrintProQueue(LinkList L);
void PrintPartTable(PartTable L);#endif

實現

list.c

#include "List.h"Status InitLinkList(LinkList *L)
{*L = (LinkList)malloc(sizeof(LNode));strcpy((*L)->data.Name, "");(*L)->Next = NULL;return OK;
}void PCBAssign(PCBType *e1, PCBType e2)
{strcpy(e1->Name,e2.Name);e1->DistbutSt = e2.DistbutSt;e1->MemorySize = e2.MemorySize;e1->StartAddress = e2.StartAddress;
}Status GetElemt_L(LinkList L,int i,PCBType *e)
{LinkList p = L->Next;	//指向第j個結點int j = 1;				//從第一個開始往后找while ( p && j < i )	//p不為空且j < i{p = p->Next;++j;}						//p為空,說明鏈表循環結束,也沒有到第i個結點   j==iif (!p || j > i)		//因為此處對i   沒有做判斷   如果 i==0  或 負數  條件成立//對于 i == j == 1 的情況則不用循環正好  返回{return ERROR;}*e = p->data;			//通過尋址改變了 該地址內存中元素的值return OK;
}
//鏈表中按照優先級:從大到小排序插入
Status ListInsert_L(LinkList L,PCBType e)	//這樣修改應該不對 p = *L出錯
{LinkList p = L, s;while (p->Next)	p = p->Next;s = (LinkList)malloc(sizeof(LNode));PCBAssign(&s->data, e);s->Next = p->Next;p->Next = s;return OK;
}
//鏈表中頭部刪除
Status ListDelete_L(LinkList L,int i,PCBType *e)
{LinkList p = L, q;int j = 0;while (p->Next && j < i-1){p = p->Next; ++j;}if(!p->Next || j > i - 1)return ERROR;q = p->Next;p->Next = q->Next;PCBAssign(e, q->data);free(q);return OK;
}//           初始化         ///
void PartiAssign(PartiType *e1, PartiType e2)
{e1->PartitionSize = e2.PartitionSize;e1->PartStartAddr = e2.PartStartAddr;strcpy(e1->Name, e2.Name);
}Status InitList_Sq(SqList *L)
{//構造一個空的線性表LL->elem = (PartiType *)malloc((LIST_INIT_SIZE)*sizeof(PartiType));if(!L->elem) return ERROR;        //存儲分配失敗L->length = 0;                 //空表長度為0L->listsize = LIST_INIT_SIZE;  //初始存儲的容量return OK;
}//在順序線性表L中第i個位置之前插入新的元素e
Status ListInsert_Sq(SqList *L,int i,PartiType e)
{//在順序線性表L中第i個位置之前插入新的元素e//i的合法值為1 <= i <= ListLength_Sq(L)+1PartiType *q, *p, *newbase;if(i < 1 || i > L->length + 1 ) return ERROR;     //i值不合法if(L->length >= L->listsize){               //當前存儲空間已滿,增加分配newbase = (PartiType *)realloc(L->elem,(L->listsize + LISTINCREMENT)*sizeof(PartiType));if(!newbase) return ERROR;				//存儲分配失敗L->elem = newbase;						//新基址L->listsize += LISTINCREMENT;			//增加存儲容量} q = &(L->elem[i - 1]);			         	//q為插入位置for(p = &(L->elem[L->length-1]);p >= q; --p)PartiAssign((p+1),*p); 					//插入位置及之后的元素右移PartiAssign(q ,e);							//插入eL->length++;return OK;
}//在順序線性表L中刪除第i個元素,并用e返回其值
Status ListDelete_Sq(SqList *L,int i,PartiType *e)
{//在順序線性表L中刪除第i個元素,并用e返回其值//i的合法值為1 <= i <= ListLength_Sq(L)PartiType *p,*q;if((i < 1) || (i > L->length))	return ERROR;							 //i值不合法p = &(L->elem[i-1]);						 //p為被刪除元素的位置PartiAssign(e, *p);							 //將被刪除元素的值賦給e (待定)q = L->elem + L->length-1;					 //移動到表尾元素的位置for (++p;p<=q;++p)PartiAssign((p-1), *p);					 //被刪除元素之后的元素左移L->length--;return OK;
}

?

#include "MemoryManage.h"
extern int CF_i;//*****         PCB鏈表操作        *****//
Status InsertProcess(LinkList Q,PCBType e)
{return ListInsert_L(Q, e);
}Status DeleteProsess(LinkList Q,int i,PCBType *e)
{return ListDelete_L(Q ,i,e);
}//*****         分區表操作        *****//
Status InsertTable(SqList *L, int i, PartiType e) 
{return ListInsert_Sq(L,i, e);
}Status DeleteTable(SqList *L, int i, PartiType *e)
{return ListDelete_Sq(L, i, e);
}//返回第幾個內存塊,從1開始,若返回0,則代表錯誤
int SelectPart(PCB* pPCB, SqList *pPartTable,AllocatStrategy AS)
{int i;int BestArr[20] = {0}, k = 0, min = 500, min_i = -1;if(AS == FirstPriority){for (i = 0; i < pPartTable->length; ++i)if(!strcmp(pPartTable->elem[i].Name, "") && pPartTable->elem[i].PartitionSize >= pPCB->MemorySize)return i + 1;}else if(AS == BestAdapt){以下補充   /for(i = 0; i < pPartTable->length; ++i){if(!strcmp(pPartTable->elem[i].Name, "") && pPartTable->elem[i].PartitionSize >= pPCB->MemorySize)if(pPartTable->elem[i].PartitionSize - pPCB->MemorySize < min){min = pPartTable->elem[i].PartitionSize - pPCB->MemorySize;min_i = i;}}return min_i+1;}else if(AS == CycleFirst){int flag = 0;以下補充   /for(i = CF_i; i < pPartTable->length; i = (i+1)%(pPartTable->length)){if(!strcmp(pPartTable->elem[i].Name, "") && pPartTable->elem[i].PartitionSize >= pPCB->MemorySize){CF_i = (i+1)%pPartTable->length;return i + 1;}if(flag && i == CF_i){break;}if(i == CF_i){flag = 1;}}return 0;}else{printf("算法選擇有誤!\n");}return ERROR;
}//通過SelectPart查找是否存在可以分配的分區,在main函數中進行調用本方法進行內存的分配
int MallocMemory(PCB *pe, SqList *pPartTable,int i)
{PartiType se = {0, 0, {0}};以下補充   ///修改PCBpe->DistbutSt = Allocated;pe->StartAddress = pPartTable->elem[i].PartStartAddr;if(pPartTable->elem[i].PartitionSize == pe->MemorySize){strcpy(pPartTable->elem[i].Name, pe->Name);} else {//修改分區使用說明表strcpy(pPartTable->elem[i].Name, "");pPartTable->elem[i].PartitionSize -= pe->MemorySize;pPartTable->elem[i].PartStartAddr += pe->MemorySize;//新建一個表目, 并插入分區表使用說明表strcpy(se.Name, pe->Name);se.PartitionSize = pe->MemorySize;se.PartStartAddr = pe->StartAddress;InsertTable(pPartTable, i+1, se);}return OK;
}void InitAllocation(PCBList PCBdata, PartTable *pPartTable,AllocatStrategy AS)
{LNode *p;int pos;p = PCBdata->Next;while (p){if(p->data.DistbutSt == Unallocated){pos = SelectPart(&(p->data), pPartTable, AS);//從1開始if(pos){MallocMemory( &(p->data), pPartTable, pos - 1);}}p = p->Next;}
}//回收指定位置的內存空間
void FreeMemory(int pos, SqList *pPartTable)//沒考慮  pos為0情況,沒考慮刪除后修改起始地址情況
{PartiType se = {0, 0, {0}};int flag = 0;以下補充   /if(pos != pPartTable->length-1){//為后一塊分配if(!strcmp(pPartTable->elem[pos+1].Name, "")){strcpy(pPartTable->elem[pos].Name, "");pPartTable->elem[pos].PartitionSize += pPartTable->elem[pos+1].PartitionSize;strcpy(se.Name, pPartTable->elem[pos+1].Name);se.PartitionSize = pPartTable->elem[pos+1].PartitionSize;se.PartStartAddr = pPartTable->elem[pos+1].PartStartAddr;DeleteTable(pPartTable, pos+1, &se);flag = 1;}}if(pos != 0){//為前一塊分配if(!strcmp(pPartTable->elem[pos-1].Name, "")){strcpy(pPartTable->elem[pos-1].Name, "");pPartTable->elem[pos-1].PartitionSize += pPartTable->elem[pos].PartitionSize;strcpy(se.Name, pPartTable->elem[pos-1].Name);se.PartitionSize = pPartTable->elem[pos-1].PartitionSize;se.PartStartAddr = pPartTable->elem[pos-1].PartStartAddr;DeleteTable(pPartTable, pos-1, &se);flag = 1;}}if(!flag){strcpy(pPartTable->elem[pos].Name, "");}
}void SearchSpace(PCBList PCBdata, SqList *partTable, AllocatStrategy AS)
{int pos;LNode *p;p = PCBdata->Next;while (p){if(p->data.DistbutSt == Unallocated){pos = SelectPart(&(p->data), partTable, AS);//從1開始if(pos){MallocMemory(&(p->data), partTable, pos - 1);}}p = p->Next;}}void PrintProQueue(LinkList L)
{int i = 0;L = L->Next;printf(" ----------------------------------------\n");printf("|進程名 | 起始位置 | 申請大小 | 是否分配 |\n");while(L){printf("|  %s   |  %4d    |  %4d    |  %4s    |\n",L->data.Name, L->data.StartAddress, L->data.MemorySize, L->data.DistbutSt == Allocated?  "是" : "否");L = L->Next;}printf(" ----------------------------------------\n");
}void PrintPartTable(PartTable L)
{int i = 0, j = 0;printf(" ----------------------------------------\n");printf("|分區號 | 起始位置 | 分區大小 | 是否分配 |\n");for (i = 0; i < L.length; ++i)printf("|  %2d   |  %4d    |  %4d    |  %4s    |\n",i + 1 , L.elem[i].PartStartAddr, L.elem[i].PartitionSize , strcmp(L.elem[i].Name, "") ? L.elem[i].Name :"否");printf(" ----------------------------------------\n");
}

main

#include "MemoryManage.h"/*實驗06 動態分區分配
*/int CF_i;void InputPCBData(PCBList * pPCBdata)
{PCBType e = {{0}, 0, 0, Unallocated};strcpy(e.Name,"P1");e.MemorySize = 16;InsertProcess(*pPCBdata,e);strcpy(e.Name,"P2");e.MemorySize = 32;InsertProcess(*pPCBdata,e);strcpy(e.Name,"P3");e.MemorySize = 48;InsertProcess(*pPCBdata,e);strcpy(e.Name,"P4");e.MemorySize = 96;InsertProcess(*pPCBdata,e);strcpy(e.Name,"P5");e.MemorySize = 100;InsertProcess(*pPCBdata,e);
}void SetFixedZone(PartTable * pPartdata)
{PartiType se = {0, 0, {0}};se.PartStartAddr = 16;se.PartitionSize = 512 - 16;strcpy(se.Name, "");InsertTable(pPartdata, 1, se);
}
//0 - 15Kb 操作系統占用  總大小512KB
int main(void)
{PCBList PCBdata;		//PCBdata里面存放原始PCB數據PartTable partTable;	//分區表char PcbName[NAME_MAXSIZE] = {0}, choice;PCBType PCBe = {{0}, 0, 0, Unallocated};PartiType Parte = {0, 0};PCBType *pcb = NULL;LNode *p; AllocatStrategy AS = CycleFirst; //FirstPriority, BestAdapt, CycleFirst//AllocatStrategy AS = BestAdapt;int i, size, pos;//分區表InitList_Sq(&partTable);SetFixedZone(&partTable);//進程表InitLinkList(&PCBdata);InputPCBData(&PCBdata);//初始化InitAllocation(PCBdata, &partTable, AS);CF_i = 0;PrintProQueue(PCBdata);PrintPartTable(partTable);while(true){system("cls");PrintProQueue(PCBdata);PrintPartTable(partTable);printf(" ================================================\n");printf("|           1.結 束 進 程                        |\n");printf("|           2.添 加 進 程                        |\n");printf("|           3.退 出 系 統                        |\n");printf(" ================================================\n");printf("請選擇:");fflush(stdin);scanf("%c",&choice);switch (choice){case '1':printf("要結束的進程名:");scanf("%s",PcbName);for (p = PCBdata->Next, i = 1; p && strcmp(PcbName, p->data.Name); i++, p = p->Next);if(!p){printf("進程名輸入錯誤!\n");break;}DeleteProsess(PCBdata, i, &PCBe);for(i = 0; i < partTable.length; i++){if(!strcmp(PcbName, partTable.elem[i].Name)){FreeMemory(i ,&partTable);break;}}SearchSpace( PCBdata, &partTable, AS);break;case '2':printf("請輸入添加的進程名,進程所占內存大小:");scanf("%s%d",PcbName , &size);PCBe.DistbutSt = Unallocated;PCBe.StartAddress = 0;strcpy(PCBe.Name, PcbName);PCBe.MemorySize = size;pos = SelectPart(&(PCBe), &partTable, AS);//從1開始if(pos)MallocMemory(&(PCBe), &partTable, pos - 1);InsertProcess(PCBdata, PCBe);break;case '3':return 0;default:printf("選擇項輸入錯誤,重新選擇!\n");break;}PrintProQueue(PCBdata);PrintPartTable(partTable);system("pause");}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445092.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445092.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445092.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…

學點數學(3)-函數空間

函數空間1.距離&#xff1a;從具體到抽象2.范數3.內積4.拓撲本博文為觀看《上海交通大學公開課-數學之旅-函數空間 》所整理筆記&#xff0c;公開課視頻連接&#xff1a;http://open.163.com/newview/movie/free?pidM8PTB0GHI&midM8PTBUHT0數學中的空間 是 大家研究工作的…

Makefile編寫詳解--項目開發

預備知識&#xff1a; gcc 的3個參數&#xff1a; 1. -o 指定目標文件 gcc sources/main.c -o bin/main 2. -c 編譯的時候只生產目標文件不鏈接 gcc -c sources/main.c -o obj/main.o 3. -I 主要指定頭文件的搜索路徑 gcc -I headers -c main.c -o main.o 4. -l 指定靜…

如何判斷對象已經死亡

引用計數 給對象中添加一個引用計數器&#xff0c;每當有一個地方引用它&#xff0c;計數器就加 1&#xff1b;當引用失效&#xff0c;計數器就減 1&#xff1b;任何時候計數器為 0 的對象就是不可能再被使用的。 這個方法實現簡單&#xff0c;效率高&#xff0c;但是目前主流…

XML常見的操作

1. 創建XML文檔 &#xff08;1&#xff09;創建一個XML文檔非常簡單&#xff0c;其流程如下&#xff1a; ① 用xmlNewDoc函數創建一個文檔指針doc。 ② 用xmlNewNode函數創建一個節點指針root_node。 ③ 用xmlDocSetRootElement將root_node設置為doc的根結點。…

算法(2)-二叉樹的遍歷(遞歸/迭代)python實現

二叉樹的遍歷1.深度優先DFS1.1 DFS 遞歸解法1.1.1先序遍歷1.1.2中序遍歷1.1.3后序遍歷1.2 DFS迭代解法1.2.1先序遍歷1.2.2中序遍歷1.2.3后序遍歷2.廣度優先BFS3.二叉樹的最大深度3.1遞歸3.2迭代4.翻轉二叉樹4.1遞歸4.1迭代5.合并兩棵二叉樹5.1遞歸5.2迭代有兩種通用的遍歷樹的策…

libxml的安裝和相關數據結構詳解

1安裝 一般如果在安裝系統的時候選中了libxml開發庫的話&#xff0c;系統會默認安裝。如果沒有安裝&#xff0c;可以按如下步驟進行手工安裝。 ① 從xmlsoft站點或ftp(ftp.xmlsoft.org)站點下載libxml壓縮包 (libxml2-xxxx.tar.gz) ② 對壓縮包進行解壓縮 tar xvzf …

內核中的 likely() 與 unlikely()

在 2.6 內核中&#xff0c;隨處可以見到 likely() 和 unlikely() 的身影&#xff0c;那么為什么要用它們&#xff1f;它們之間有什么區別&#xff1f; 首先要明確&#xff1a; if(likely(value)) 等價于 if(value) if(unlikely(value)) 也等價于 if(value) 也就是說 likely()…

python外卷(12)-sort(),sorted(),ord(),chr()

Python內置函數1.sort()&#xff0c;sorted()2.ord(), chr()1.sort()&#xff0c;sorted() sort() 是list的方法&#xff0c;對已經存在的列表進行操作&#xff0c;無返回值 a[3,2,4,1] b["c","a","b"] print (a.sort(),b.sort()) # 輸出 (Non…

利用posix_fadvise清理系統中的文件緩存

利用posix_fadvise清理系統中的文件緩存leoncom c/c,unix2011-08-03當我們需要對某段讀寫文件并進行處理的程序進行性能測試時&#xff0c;文件會被系統cache住從而影響I/O的效率&#xff0c;必須清理cache中的對應文件的才能正確的進行性能測試。通常清理內存可以采用下面的這…

空間分配

目前主流的垃圾收集器都會采用分代回收算法&#xff0c;因此需要將堆內存分為新生代和老年代&#xff0c;這樣我們就可以根據各個年代的特點選擇合適的垃圾收集算法。 大多數情況下&#xff0c;對象在新生代中 eden 區分配。當 eden 區沒有足夠空間進行分配時&#xff0c;虛擬…

關于uint32_t uint8_t uint64_t 的問題

怎么又是u又是_t的?u代表的是unsigned相信大家都知道,那么_t又是什么呢?我認為它就是一個結構的標注,可以理解為type/typedef的縮寫,表示它是通過typedef定義的,而不是其它數據類型。 uint8_t,uint16_t,uint32_t等都不是什么新的數據類型,它們只是使用typedef給類型起…

學點數學(4)-協方差矩陣

協方差矩陣協方差矩陣&#xff08;從隨機變量講起&#xff09;隨機變量x&#xff1a;表示隨機試驗各種結果的 實值 單值函數&#xff0c;就是說隨機變量x是一個函數映射&#xff0c;其取值為標量。隨機變量有離散型和連續型&#xff0c;離散型&#xff1a;拋10次硬幣&#xff…