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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

操作系统: 最佳适配算法和邻近适配算法的模拟实现(内存分配算法)

發布時間:2025/3/15 windows 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作系统: 最佳适配算法和邻近适配算法的模拟实现(内存分配算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現動態分區的分配算法。

(1)?最佳適配算法:選擇內存空閑塊中最適合進程大小的塊分配。

(2)?鄰近適配算法:從上一次分配的地址開始查找符合要求的塊,所查找到的第一個滿足要求的空閑塊就分配給進程。

模擬添加進程的時候,假定內存是一塊完整的空閑區,對于算法(1)來說,分配的時候遍歷所有的空閑內存塊,找出其中最適合的一塊,注意此時內存分區的總塊數可能已經發生了變化;

對于算法(2)來說,則需要從上次分配的內存塊(使用變量記錄即可)接著向下找到第一個滿足條件的塊即可,內存分區的總塊可能也已經發生了變化。

模擬刪除進程(釋放內存)的時候,則需要注意如果當前內存塊的上下有空閑塊,則需要將其合并為一整個空閑塊,也需要注意內存總塊的數量變化。

最佳適配算法

[cpp]?view plain?copy
  • #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<<"請使用add命令添加一個進程,delete命令刪除一個進程!"<<endl;??
  • ????while(1)??
  • ????{??
  • ???????string?s;??
  • ???????cin>>s;??
  • ???????if(s=="add")??
  • ???????{??
  • ????????cout<<"請輸入進程號及其的大小"<<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<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);??
  • ????????????????}??
  • ????????????}??
  • ????????????else??
  • ????????????TTemp.push_back(cluster[i]);??
  • ????????}??
  • ??????????
  • ????????cluster.swap(TTemp);??
  • ??????????
  • ??????????
  • ????????for(int?i=0;i<cluster.size();i++)??
  • ????????cout<<"大小?"<<cluster[i].num<<"??是否分配?"<<cluster[i].is_data<<endl;??
  • ??????????
  • ????}??
  • ????else??
  • ????{??
  • ????????cout<<"內存不足!"<<endl;??
  • ????}??
  • ?}??
  • ????????????
  • ?????else?if(s=="delete")??
  • ?????{??
  • ????????????int?deletenum;??
  • ????????????cout<<"請輸入刪除的進程:"<<endl;??
  • ????????????cin>>deletenum;??
  • ??
  • ????????int?i=mp[deletenum];??
  • ????????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<<"大小?"<<cluster[i].num<<"??是否分配?"<<cluster[i].is_data<<endl;??
  • ??
  • ???????}??
  • ???}??
  • ????return?0;??
  • }??


  • 鄰近適配算法

    [cpp]?view plain?copy
  • #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<<"請使用add命令添加一個進程,delete命令刪除一個進程!"<<endl;??
  • ????
  • ????while(1)??
  • ????{??
  • ???????string?s;??
  • ???????cin>>s;??
  • ???????if(s=="add")??
  • ???????{??
  • ????????cout<<"請輸入進程號及其的大小"<<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);??
  • ????????????????}??
  • ????????????}??
  • ????????????else??
  • ????????????TTemp.push_back(cluster[i]);??
  • ????????}??
  • ??????????
  • ????????cluster.swap(TTemp);??
  • ??????????
  • ????????for(int?i=0;i<cluster.size();i++)??
  • ????????cout<<"大小?"<<cluster[i].num<<"??是否分配?"<<cluster[i].is_data<<endl;??
  • ??????????
  • ????}??
  • ????else??
  • ????{??
  • ????????cout<<"內存不足!"<<endl;??
  • ????}??
  • ?}??
  • ????????????
  • ?????else?if(s=="delete")??
  • ?????{??
  • ????????????int?deletenum;??
  • ????????????cout<<"請輸入刪除的進程:"<<endl;??
  • ????????????cin>>deletenum;??
  • ??
  • ????????int?i=mp[deletenum];??
  • ????????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<<"大小?"<<cluster[i].num<<"??是否分配?"<<cluster[i].is_data<<endl;??
  • ??
  • ???????}??
  • ???}??
  • ????return?0;??
  • }??

  • 原文地址:?http://blog.csdn.net/nk_test/article/details/50413665

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的操作系统: 最佳适配算法和邻近适配算法的模拟实现(内存分配算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

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