Operating system: Simulation implementation of best fit algorithm and proximity adaptation algorithm (memory allocation algorithm)

Source: Internet
Author: User

Implementation of dynamic partitioning allocation algorithm.

(1) best fit Algorithm : Select the block allocation that best fits the process size in the memory free block.

(2) proximity adaptation algorithm: from the last assigned address to find the block that meets the requirements, the first found to meet the requirements of the free block allocated to the process.

When adding a process, assume that the memory is a complete idle area, for the algorithm (1), the allocation of time to traverse all the free memory block, find the most suitable piece, note that the memory partition of the total number of blocks may have changed;

For the algorithm (2), it is necessary to find the first block that satisfies the condition from the last allocated block of memory (using the variable record), and the total block of the memory partition may have changed.

When simulating the removal process (freeing memory), it is important to note that if there are free blocks up and down the current memory block, you need to merge them into an entire free block, and also pay attention to the number of total memory blocks.

Optimal adaptation algorithm

#include <iostream> #include <vector> #include <string> #include <cstring> #include <map> #include <algorithm>using namespace std;struct node{int num;int is_data;}; int main () {vector<node> cluster;vector<node> ttemp;map <int,int> mp;mp.clear (); node Temp;int r_num;       int Curnum;temp.is_data=0;temp.num=10;cluster.push_back (temp); cout<< "Please add a process using the Add command, delete command to remove a process!"    <<endl;      while (1) {string S;      cin>>s;   if (s== "add") {cout<< "Please enter the process number and its size" <<endl;    Cin>>r_num>>curnum;int flag=0;int Curindex;int cmp=11; for (int i=0;i<cluster.size (); i++) {if (cluster[i].is_data==0&&cluster[i].num>=curnum) {flag=1;if ( CLUSTER[I].NUM-CURNUM&LT;CMP) {curindex=i;cmp=cluster[i].num-curnum;}}} if (flag) {Cluster[curindex].is_data=1;mp[r_num]=curindex;node op; Ttemp.clear (); int is_flag=0;if (cluster[curindex].num>curnum) {Op.is_data=0;op.num=cluster[curindex]. Num-curnum;is_flag=1;} For(int i=0;i<cluster.size (); i++) {if (i==curindex) {cluster[curindex].num=curnum; Ttemp.push_back (Cluster[curindex]); Mp[r_num]=i;if (Is_flag) {ttemp.push_back (OP);}} Elsettemp.push_back (Cluster[i]);} Cluster.swap (ttemp); for (int i=0;i<cluster.size (); i++) cout<< size <<cluster[i].num<< whether assign <<cluster[i].is_data<<endl;} else{cout<< "Not enough memory!              "<<endl;}} else if (s== "delete") {int deletenum;cout<< "Please enter the deleted process:" <<endl;cin>>deletenum;int I=mp[deletenu M];while (--i>=0 && cluster[i].is_data==0) {}int j=mp[deletenum];while (++j<cluster.size () && cluster[j].is_data==0) {}node kk;kk.num=0;for (int e=i+1;e<=j-1;e++) {kk.num+=cluster[e].num;} kk.is_data=0; Ttemp.clear (); for (int p=0;p<cluster.size ();) {if (p==i+1) {p=j; Ttemp.push_back (KK);}      Else{ttemp.push_back (Cluster[p]); p++;}} Cluster.swap (ttemp); for (int i=0;i<cluster.size (); i++) cout<< size <<cluster[i].num<< whether assign <<cluster[i].is_data<<endl; }}return 0;}


Proximity adaptation Algorithm

#include <iostream> #include <vector> #include <string> #include <cstring> #include <map> #include <algorithm>using namespace std;struct node{int num;int is_data;}; int main () {vector<node> cluster;vector<node> ttemp;map <int,int> mp;mp.clear (); node Temp;int r_num;       int Curnum;int Last_id=0;temp.is_data=0;temp.num=10;cluster.push_back (temp); cout<< "Please add a process using the Add command, delete command to remove a process!"      <<endl;      while (1) {string S;      cin>>s;   if (s== "add") {cout<< "Please enter the process number and its size" <<endl;    Cin>>r_num>>curnum;int Flag=0;int Curindex;    for (int i=beg_pos;i<cluster.size (); i++) int i=last_id+1;    while (1) {if (I==cluster.size ()) i=0;if (cluster[i].is_data==0&&cluster[i].num>=curnum) {flag=1;    Curindex=i; break;} i++;} if (flag) {Cluster[curindex].is_data=1;mp[r_num]=curindex;node op; Ttemp.clear (); int is_flag=0;if (cluster[curindex].num>curnum) {Op.is_data=0;op.num=cluster[curindeX].num-curnum;is_flag=1;} for (int i=0;i<cluster.size (); i++) {if (i==curindex) {cluster[curindex].num=curnum;                Ttemp.push_back (Cluster[curindex]); mp[r_num]=i; Last_id=i; if (Is_flag) {ttemp.push_back (OP);}} Elsettemp.push_back (Cluster[i]);} Cluster.swap (ttemp); for (int i=0;i<cluster.size (); i++) cout<< size <<cluster[i].num<< whether assign <<cluster[i].is_data<<endl;} else{cout<< "Not enough memory!              "<<endl;}} else if (s== "delete") {int deletenum;cout<< "Please enter the deleted process:" <<endl;cin>>deletenum;int I=mp[deletenu M];while (--i>=0 && cluster[i].is_data==0) {}int j=mp[deletenum];while (++j<cluster.size () && cluster[j].is_data==0) {}node kk;kk.num=0;for (int e=i+1;e<=j-1;e++) {kk.num+=cluster[e].num;} kk.is_data=0; Ttemp.clear (); for (int p=0;p<cluster.size ();) {if (p==last_id) last_id=ttemp.size (); if (p==i+1) {p=j; Ttemp.push_back (KK);}      Else{ttemp.push_back (Cluster[p]); p++;}} Cluster.swap (ttemp); for (int i=0;i<cluSter.size (); i++) cout<< size <<cluster[i].num<< whether assign <<cluster[i].is_data<<endl; }}return 0;}


Operating system: Simulation implementation of best fit algorithm and proximity adaptation algorithm (memory allocation algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.