In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you a detailed explanation of the principle and code testing of dynamic partition allocation in the windows operating system. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
The dynamic partition allocation mode of the operating system is one of the continuous allocation modes in the memory management of the operating system. The other three allocation methods are: single continuous allocation, fixed partition allocation, dynamic partition allocation and dynamic relocation partition allocation. Continuous allocation means that a user program is allocated a continuous memory space.
Content:
(1). The dynamic partition allocation process and recovery process using the first adaptation algorithm and the optimal adaptation algorithm are realized in C or other languages respectively.
(2). Set the initial state and show the free memory partition chain after each allocation and recycling.
Principle:
Partition allocation, first adaptation algorithm, best adaptation algorithm and so on.
Source code (only a small-scale data test was conducted):
# include#include#define SIZE 640 / / initial memory size # define MINSIZE 5 / / fragment minimum enum STATE {Free, Busy}; struct subAreaNode {intaddr; / / starting address intsize; / / partition size inttaskId; / / Job number STATEstate; / / partition status subAreaNode * pre / / Partition forward pointer subAreaNode* nxt; / / Partition backward pointer} subHead;// initialize idle partition chain void intSubArea () {/ / assign initial partition memory subAreaNode* fir = (subAreaNode*) malloc (sizeof (subAreaNode)); / / assign the first partition fir- > addr = 0; fir- > size = SIZE; fir- > state = Free; fir- > taskId =-1; fir- > pre = & subHead Fir- > nxt = NULL; / / initialize partition header information subHead.pre = NULL; subHead.nxt = fir;} / / first adaptation algorithm int firstFit (int taskId, int size) {subAreaNode * p = subHead.nxt; while (packs = NULL) {if (p-> state = = Free & & p-> size > = size) {/ / find the free partition if (p-> size-sizestate = Busy) to assign P-> taskId = taskId;} else {/ / Distribution interval subAreaNode* node = (subAreaNode*) malloc (sizeof (subAreaNode)); node- > addr = p-> addr + size; node- > size = p-> size-size; node- > state = Free; node- > taskId =-1 / / modify the partition chain node pointer node- > pre = p; node- > nxt = p-> nxt; if (p-> nxt! = NULL) {p-> nxt- > pre = node;} p-> nxt = node; / / allocate idle interval p-> size = size P-> state = Busy; p-> taskId = taskId;} printf ("memory allocated successfully! N "); return 1;} p = p-> nxt;} printf (" failed to find a suitable memory partition... n "); return0;} / / Best adaptation algorithm int bestFit (int taskId, int size) {subAreaNode * tar = NULL; inttarSize = SIZE + 1; subAreaNode * p = subHead.nxt While (pause = NULL) {/ / find the best free interval if (p-> state = = Free & & p-> size > = size & & p-> size
< tarSize) { tar = p; tarSize = p->Size;} p = p-> nxt;} if (else = NULL) {/ / find the free partition if to be allocated (tar- > size-size state = Busy; tar- > taskId = taskId;} else {/ / the interval subAreaNode* node = (subAreaNode*) malloc (sizeof (subAreaNode)) with the assigned size of size) Node- > addr = tar- > addr + size; node- > size = tar- > size-size; node- > state = Free; node- > taskId =-1; / / modify partition chain node pointer node- > pre = tar; node- > nxt = tar- > nxt; if (tar- > nxt! = NULL) {tar- > nxt- > pre = node } tar- > nxt = node; / / allocate free interval tar- > size = size; tar- > state = Busy; tar- > taskId = taskId;} printf ("memory allocated successfully! N "); return 1;} else {/ / cannot find a suitable free partition printf (" failed to find a suitable memory partition. N "); return 0;} / reclaim memory int freeSubArea (int taskId) {intflag = 0; subAreaNode * p = subHead.nxt, * pp While (packs = NULL) {if (p-> state = = Busy & & p-> taskId = = taskId) {flag = 1 If ((p-> pre! = & subHead & & p-> pre- > state = = Free) & & (p-> nxt! = NULL&&p- > nxt- > state = = Free) {/ / case 1: merge the upper and lower partitions / / merge the upper interval pp = p; p = p-> pre; p-> size + = pp- > size P-> nxt = pp- > nxt; pp- > nxt- > pre = p; free (pp); / / the interval pp = p-> nxt; p-> size + = pp- > size; p-> nxt = pp- > nxt; if (pp- > nxt! = NULL) {pp- > nxt- > pre = p } free (pp);} else if ((p-> pre = = & subHead | | p-> pre- > state = = Busy) & & (p-> nxt! = NULL&&p- > nxt- > state = = Free) {/ / case 2: merge only the following partitions pp = p-> nxt; p-> size + = pp- > size; p-> state = Free P-> taskId =-1; p-> nxt = pp- > nxt; if (pp- > nxt! = NULL) {pp- > nxt- > pre = p;} free (pp) } else if ((p-> pre! = & subHead & & p-> pre- > state = = Free) & & (p-> nxt = = NULL | | p-> nxt- > state = = Busy) {/ / case 3: merge only the above partitions pp = p; p = p-> pre; p-> size + = pp- > size; p-> nxt = pp- > nxt If (pp- > nxt! = NULL) {pp- > nxt- > pre = p;} free (pp);} else {/ / case 4: upper and lower partitions do not need to merge p-> state = Free; p-> taskId =-1;}} p = p-> nxt } if (flag== 1) {/ / successfully recycled printf ("memory partition recycled successfully.. n"); return 1;} else {/ / failed to find the target job, recycling failed printf ("failed to find the target job, memory partition recycling failed. N"); return 0 }} / / shows void showSubArea () {printf ("* n"); printf ("* * the current memory allocation is as follows: * * n") Printf ("* n"); printf ("* * starting address | Space size | work status | Job number * * n"); subAreaNode * p = subHead.nxt While (packs = NULL) {printf ("* *-* n"); printf ("* *"); printf ("% d k |", p-> addr); printf ("% d k |", p-> size); printf ("% s |", p-> state = = Free? "Free": "Busy"); if (p-> taskId > 0) {printf ("% d", p-> taskId);} else {printf (");} printf (" * * n "); p = p-> nxt } printf ("* n");} int main () {intoption, ope, taskId, size; / / initialize the free partition chain intSubArea () / / Select the allocation algorithm while (1) {printf ("Please select the allocation algorithm to be simulated: 0 for the first adaptation algorithm, 1 for the best adaptation algorithm n"); scanf ("% d", & option); if (option = = 0) {printf ("you chose the first adaptation algorithm, let's simulate the algorithm n"); break } else if (option = = 1) {printf ("you have chosen the best adaptive algorithm, let's simulate the algorithm n"); break;} else {printf ("error: please enter 0/1nn");} / / simulate dynamic partition allocation algorithm while (1) {printf ("n") Printf ("* n"); printf ("* * 1: allocate memory 2: reclaim memory 0: exit * * n") Printf ("* n"); scanf ("% d", & ope); if (ope = = 0) break; if (ope = = 1) {/ / Analog memory allocation printf ("Please enter the assignment number:") Scanf ("% d", & taskId); printf ("Please enter the amount of memory to be allocated (KB):"); scanf ("% d", & size); if (size
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.