模擬固定分區分配

介紹

data.h

#ifndef _Data_h_
#define _Data_h_#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT  2
#define true				1
#define false				0
#define PCBType				PCB
#define Status				int
#define OK					1
#define	ERROR				0
#define NAME_MAXSIZE		20
#define PCB_Num				5
#define LIST_INITSIZE		10
#define PartiType			PartitionInfo
#define TotalMemory			512	//KBtypedef enum 
{Unallocated, Allocated
}DistributState, PartitionSt;typedef enum
{FirstPriority, BestAdapt
}AllocatStrategy;typedef struct 
{char Name[NAME_MAXSIZE];	//進程名int	MemorySize;				//內存的大小int StartAddress;			//內存起始地址DistributState DistbutSt;	//分配狀態
}PCB;typedef struct Node
{PCBType data;struct Node * Next;		
}LNode, *LinkList, *PCBList;			//typedef struct {//分區號用數組下標代替int PartitionSize;int PartStartAddr;char Name[NAME_MAXSIZE];//若為空,則分區空閑
}PartitionInfo;typedef struct
{PartiType *elem;int listsize;		//表容量int length;			//元素個數
}SqList, PartTable;		//分區表#endif 

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

?

#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);
int MallocMemory(PCB *pe, SqList *pPartTable, int pos);
void SearchSpace(PCBList PCBdata, SqList partTable);
void FreeMemory(int pos, SqList *pPartTable);
void InitAllocation(PCBList PCBdata, PartTable partTable);
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;
}

memoryManage.c

#include "MemoryManage.h"//*****         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)
{int i,Start;if(pPCB->MemorySize <= 16)Start = 0;else if(pPCB->MemorySize <= 32)Start = 1;else if(pPCB->MemorySize <= 64)Start = 2;else if(pPCB->MemorySize <= 128)Start = 3;else if(pPCB->MemorySize <= 256)Start = 4;else{printf("內存過大,無法裝入!\n");return ERROR;}for (i = Start; i < pPartTable->length; ++i)if(!strcmp(pPartTable->elem[i].Name, ""))return i + 1;return ERROR;
}//i傳遞的是下標
int MallocMemory(PCB *pe, SqList *pPartTable,int i)
{///   以下需要補充    /pe->DistbutSt = Allocated;pe->StartAddress = pPartTable->elem[i].PartStartAddr;strcpy(pPartTable->elem[i].Name, pe->Name);return OK;
}/*** PCBdata:表示PCB鏈* partTable:分區表* 將每一個PCB取出,查找是否有合適的分區可以分配給他,如果有分配,如果沒有不分配*/ 
void InitAllocation(PCBList PCBdata, PartTable partTable)
{///   以下需要補充    /PCBList L = PCBdata->Next;int pos;while(L){pos = SelectPart(&L->data, &partTable);if(pos == 0) {printf("無法為%s進程分配空間!!!\n", L->data.Name);} else {L->data.DistbutSt = Allocated;L->data.StartAddress = partTable.elem[pos-1].PartStartAddr;strcpy(partTable.elem[pos-1].Name, L->data.Name);}L = L->Next;}//SearchSpace(PCBdata, partTable);
}void FreeMemory(int pos, SqList *pPartTable)
{///   以下需要補充    /strcpy(pPartTable->elem[pos].Name, "");
}void SearchSpace(PCBList PCBdata, SqList partTable)
{int pos;LNode *p;p = PCBdata->Next;while (p){if(p->data.DistbutSt == Unallocated){pos = SelectPart(&(p->data), &partTable);//從1開始if(pos){MallocMemory(&(p->data), &partTable, pos - 1);break;}}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 固定分區分配
* 分配策略:
* ①離隊首最近,能夠裝入該分區的進程;
* ②搜索能夠裝入該分區最大的進程。
*/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, Unallocated };se.PartStartAddr = 16;se.PartitionSize = 16;InsertTable(pPartdata, 1, se);se.PartStartAddr = 32;se.PartitionSize = 32;InsertTable(pPartdata, 2, se);se.PartStartAddr = 64;se.PartitionSize = 64;InsertTable(pPartdata, 3, se);se.PartStartAddr = 128;se.PartitionSize = 128;InsertTable(pPartdata, 4, se);se.PartStartAddr = 256;se.PartitionSize = 256;InsertTable(pPartdata, 5, 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 tmp;PCBType *pcb = NULL;LNode *p; PCBList pl = NULL;int tpos = 0;int startAddress;int i, size, pos, j;InitList_Sq(&partTable);SetFixedZone(&partTable);InitLinkList(&PCBdata);InputPCBData(&PCBdata);InitAllocation(PCBdata, partTable);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("%d",&choice);//printf("haha");switch (choice){///   以下需要補充    /case 1:printf("要結束的進程名:");scanf("%s", PcbName);//找到指定進程的位置,pl = PCBdata->Next;startAddress = -1;tpos = 0;while(pl){tpos++;if(!strcmp(pl->data.Name, PcbName) && pl->data.DistbutSt == Allocated){startAddress = pl->data.StartAddress;break;}pl = pl->Next;}if(startAddress == -1){printf("進程不存在!!!\n");break;}//刪除進程DeleteProsess(PCBdata, tpos, &tmp);//根據起始地址找到要回收的分區for(j = 0; j < partTable.length; ++j){if(partTable.elem[j].PartStartAddr == startAddress){tpos = j;break;}}//回收內存FreeMemory(tpos, &partTable);//重新檢查是否可以為其他進程分配SearchSpace(PCBdata, partTable);break;case 2:printf("請輸入添加的進程名和所占分區的大小:");scanf("%s %d", PcbName, &size);strcpy(PCBe.Name, PcbName);PCBe.MemorySize = size;PCBe.DistbutSt = Unallocated;PCBe.StartAddress = 0;InsertProcess(PCBdata, PCBe);SearchSpace(PCBdata, partTable);break;case 3:exit(0);break;}PrintProQueue(PCBdata);PrintPartTable(partTable);system("pause");}return 0;
}

?

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

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

相關文章

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 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 ListIn…

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 …