日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

模拟固定分区分配

發布時間:2023/12/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟固定分区分配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

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; }

?

總結

以上是生活随笔為你收集整理的模拟固定分区分配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。