• 操作系统伙伴算法仿真c++


    前言:

    链表,无指针,无解释,无检查,无正确性。

    刚刚看了看算法原理,然后根据自己的理解就写了,没有具体的学过,所以不能保证一定正确。

    如有错误,请各位大佬指正!


    代码:

    1. /**
    2. * autor: 末语
    3. * created: 2022.12.1 12:20:01
    4. */
    5. #include
    6. using namespace std;
    7. #define ll long long
    8. struct block{
    9. int start;
    10. int size;
    11. int occupy;
    12. int id;
    13. int number;
    14. };
    15. ll ksm(ll a , ll b){
    16. ll kase = 1;
    17. while(b){
    18. if(b & 1)kase = kase * a ;
    19. a = a * a ;
    20. b >>= 1;
    21. }
    22. return kase ;
    23. }
    24. int main(){
    25. ios::sync_with_stdio(false);
    26. cin.tie(0);
    27. cout<<"请输入分配内存大小:"<<'\n';
    28. ll n ; // 内存大小
    29. cin>>n;
    30. bitset<64>b(n);
    31. for(int i = 0 ;i < n; i ++){
    32. if(!b[i])continue;
    33. b[i] = 0 ;
    34. if(b.to_ullong() == 0){b[i] = 1;break;}
    35. }
    36. n = b.to_ullong(); // 更正输入内存大小, 保证是2^x
    37. cout<<"设计内存大小:"<'\n';
    38. int mxnumber = 0 ;
    39. while(((n >> mxnumber) & 1) == 0)mxnumber++;
    40. vector>head(20 , vector()); // 模拟链表
    41. //cout<
    42. head[mxnumber].emplace_back(block{0 , (int)ksm(2 , mxnumber) , 0 , 1 , mxnumber});
    43. vector>bit(20);
    44. mapma;
    45. while(1){
    46. cout<<"请选择需要进行的操作:(0 : 添加进程 ,1:释放进程 ,2:查询进程 , 3:查看所有进程信息)"<<'\n';
    47. int op ;
    48. cin>>op; // op = 0 分配内存 op = 1 释放内存
    49. auto print = [&](block now){
    50. int start = now.start;
    51. int size = now.size;
    52. int occupy = now.occupy;
    53. cout<<"start:"<" B"<<'\n';
    54. cout<<"block_size:"<" B"<<'\n';
    55. cout<<"occupy_size:"<" B"<<'\n';
    56. };
    57. if(op == 0){
    58. string name;
    59. int need ;
    60. cout<<"请输入进程名:"<<'\n';
    61. cin>>name;
    62. cout<<"请输入分配内存大小:"<<'\n';
    63. cin>>need;
    64. if(ma.find(name) != ma.end()){
    65. cout<<"该进程名已存在!"<<'\n';
    66. continue;
    67. }
    68. functionint , int )> put = [&](int i , int need ){
    69. if(i == 0 || ksm(2 , i - 1) < need){
    70. head[i].back().occupy = need;
    71. int id = head[i].back().id;
    72. ma[name] = head[i].back();
    73. head[i].pop_back();
    74. bit[i][id / 2] = bit[i][id / 2]^1;
    75. return ma[name];
    76. }
    77. else {
    78. int start = head[i].back().start;
    79. int size = head[i].back().size;
    80. int id = head[i].back().id;
    81. head[i - 1].emplace_back(block{start , size / 2 , 0 , id * 2 , i - 1});
    82. head[i - 1].emplace_back(block{start + size / 2 , size / 2 , 0 , id * 2 + 1 , i - 1});
    83. head[i].pop_back();
    84. bit[i][id / 2] = bit[i][id / 2] ^ 1;
    85. return put( i - 1 , need);
    86. }
    87. };
    88. bool ok = 0;
    89. block p ;
    90. for(int i = 0 ; i < mxnumber + 1; i ++){
    91. if(head[i].size() == 0 || ksm(2 , i) < need)continue;
    92. p = put(i , need);
    93. ok = 1;
    94. break;
    95. }
    96. if(ok){
    97. cout<<"成功分配!"<<'\n';
    98. cout<<"分配进程信息:"<<'\n';
    99. print(p);
    100. }
    101. else {
    102. cout<<"分配失败!"<<'\n';
    103. }
    104. }
    105. else if(op == 1){
    106. string name;
    107. cout<<"请输入需要释放的进程名"<<'\n';
    108. cin>>name;
    109. if(ma.find(name) == ma.end()){
    110. cout<<"无该进程!"<<'\n';
    111. continue;
    112. }
    113. block process = ma[name];
    114. function<void(block)>check = [&](block now){
    115. int id = now.id;
    116. int number = now.number;
    117. bit[number][id / 2] = bit[number][id / 2]^ 1;
    118. if(!bit[number][id / 2] && number != mxnumber){
    119. int target = (id % 2 == 0 ? id + 1 : id - 1);
    120. int start;
    121. for(int i = 0 ; i < head[number].size() ; i ++){
    122. if(head[number][i].id == target){
    123. start = min(head[number][i].start , now.start);
    124. head[number].erase(head[number].begin() + i);
    125. break;
    126. }
    127. }
    128. block fa = block{start , now.size * 2 , 0 , id / 2 , number + 1};
    129. check(fa);
    130. }
    131. else {
    132. head[number].emplace_back(now);
    133. }
    134. };
    135. check(process);
    136. ma.erase(ma.find(name));
    137. cout<<"释放成功!"<<'\n';
    138. }
    139. else if(op == 2){
    140. string name;
    141. cin>>name;
    142. if(ma.find(name) == ma.end()){
    143. cout<<"无该进程!"<<'\n';
    144. continue;
    145. }
    146. print(ma[name]);
    147. }
    148. else if(op == 3){
    149. cout<<"目前存在进程信息如下:"<<'\n';
    150. vector>pre;
    151. for(auto it : ma){
    152. pre.emplace_back(make_pair(it.first , it.second));
    153. }
    154. sort(pre.begin() , pre.end() , [&](pair a , pair b){
    155. return a.second.start < b.second.start;
    156. });
    157. for(auto it : pre){
    158. cout<<"进程:"<'\n';
    159. print(it.second);
    160. }
    161. }
    162. }
    163. return 0 ;
    164. }

  • 相关阅读:
    【学习笔记】DDD领域驱动设计篇
    SQL审核 | 如何利用 OpenAPI 实现自己的扫描任务
    【ROOTFS】1-构建rootfs与nfs调试
    Python logging 模块详解
    智慧安防解决方案-最新全套文件
    typedef的用法——c语言
    solidity部署和验证代理合约
    BigDecimal常用API
    基于STM32+物联网设计的货车重量检测系统(OneNet)
    Java系列之:var关键字
  • 原文地址:https://blog.csdn.net/weixin_50547586/article/details/128130270