[C++] 模拟可变分区管理(操作系统) →→→→→进入此内容的聊天室

来自 , 2020-11-25, 写在 C++, 查看 184 次.
URL http://www.code666.cn/view/dd055f53
  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. struct  freelink{  //空闲链表
  9.          int  len, address;  //   len为分区长度
  10.                              //   address为分区起始地址
  11.          struct  freelink  *next;
  12.   };
  13.  
  14. struct  busylink{    //忙碌链表
  15.          char name;         //  作业或进程名  name='S' 表示OS占用
  16.          int  len , address;
  17.          struct  busylink  *next;
  18.   };
  19. //全程量
  20.   struct  freelink   *free_head=NULL;    //空闲链队列(带头结点)队首指针        
  21.   struct  busylink   *busy_head=NULL;    //占用区队列队(带头结点)首指针                
  22.   struct  busylink   *busy_tail=NULL;    //占用区队列队尾指针
  23.  
  24.  
  25.  
  26.  
  27.  
  28.    void  start()     /* 设置系统初始状态*/
  29.   {    
  30.           struct freelink *p;
  31.       struct busylink *q;
  32.       free_head=(struct  freelink*)malloc(sizeof(struct freelink));
  33.       free_head->next=NULL;  // 创建空闲链头结点
  34.       busy_head=busy_tail=(struct busylink*)malloc(sizeof(struct  busylink));
  35.       busy_head->next=NULL;  // 创建忙碌链头结点
  36.       p=(struct freelink *)malloc(sizeof(struct freelink));
  37.       p->address=64;
  38.       p->len=640-64;
  39.       p->next=NULL; //分配操作系统占用的64K内存
  40.       free_head->next =p;
  41.       q=( struct  busylink *)malloc(sizeof(struct busylink));
  42.       q->name='S';  /*  S表示操作系统占用  */
  43.       q->len=64;  q->address=0;  q->next=NULL;
  44.       busy_head->next=q;  busy_tail=q;
  45.   }
  46.  
  47.  
  48.  
  49.  
  50.       void requireMemo(char name,int require)     /*模拟内存分配*/
  51.           {
  52.                   struct freelink *p,*u,*v;
  53.           struct busylink *q;
  54.                   if(free_head->next->len<require)
  55.                   {
  56.                           cout<<"Can't allocate"<<endl;
  57.                           exit(1);
  58.                   }
  59.                   else
  60.                   {
  61.                           q=(struct busylink*)malloc(sizeof(struct busylink));
  62.               q->name=name;
  63.               q->address=free_head->next->address;
  64.               q->len=require;
  65.                           q->next=NULL;
  66.               busy_tail->next=q;
  67.                           busy_tail=q;
  68.  
  69.                   }
  70.                   p=free_head->next;
  71.           free_head->next=p->next;
  72.                   if(p->len==require)
  73.                   {
  74.                           free(p);
  75.                   }
  76.                   else
  77.                   {
  78.                           p->address=p->address+require;
  79.               p->len=p->len-require;
  80.               u=free_head;
  81.               v=free_head->next;
  82.  
  83.                   }
  84.  
  85.                   while((v!=NULL)&&v->len>p->len)
  86.                   {
  87.                           u=v;
  88.               v=v->next;
  89.                   }
  90.                   u->next=p;
  91.           p->next=v;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.        void  freeMemo(char name)   // 模拟内存回收
  99.           {
  100.          struct freelink *p,*u,*v;
  101.          struct busylink *q,*w;
  102.                  q=busy_head;
  103.          w=q->next;
  104.                  while((w!=NULL)&&(w->name!=name))
  105.                  {
  106.                          q=w;
  107.              w=w->next;
  108.                  }
  109.                  if(w==NULL)
  110.                          printf("%c is not exit",name);
  111.                  else if(w==busy_tail)
  112.                  {
  113.                          busy_tail=q;
  114.              q->next=w->next;
  115.              q->len=w->len;
  116.                          q->address=w->address;
  117.                          free(w);
  118.                  }
  119.                  p=( struct freelink*) malloc(sizeof(struct freelink));
  120.          p->len=p->len+q->len;
  121.          p->address=p->address-q->address;
  122.                  u=free_head;
  123.          v=free_head->next;
  124.                  if((v!=NULL)&&(v->len>p->len))
  125.                  {
  126.                          u=v;
  127.              v=v->next;
  128.                  }
  129.                  else
  130.                  {
  131.                          u->next=p;
  132.              p->next=v;
  133.  
  134.                  }
  135.           }
  136.  
  137.  
  138.       void  printlink()  /* 输出内存空闲情况(自由链的结点) */
  139.           {
  140.                   struct freelink *p;
  141.                   p=free_head->next;
  142.                   cout<<"分区长度        "<<"分区起始地址"<<endl;
  143.                  while(p!=NULL)
  144.                  {
  145.                          cout<<p->len<<"                "<<p->address<<endl;
  146.                          p=p->next;
  147.                  }
  148.           }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. void main()
  155. {
  156.     int n=1;
  157.         start();
  158.         char time;
  159.         while(n)
  160.         {
  161.                 cout<<"输入时刻:";
  162.         cin>>time;
  163.                 switch(time)
  164.                 {
  165.                 case'1':
  166.                          requireMemo('A',8);
  167.                  requireMemo('B',16);
  168.              requireMemo('C',64);
  169.                  requireMemo('D',124);
  170.                           printlink();
  171.                           break;
  172.                 case'2':
  173.                          requireMemo('A',8);
  174.                  requireMemo('B',16);
  175.              requireMemo('C',64);
  176.                  requireMemo('D',124);
  177.                          freeMemo('C');
  178.              printlink();
  179.                          break;
  180.                 case'3':
  181.                          requireMemo('A',8);
  182.                  requireMemo('B',16);
  183.              requireMemo('C',64);
  184.                  requireMemo('D',124);
  185.                          freeMemo('C');
  186.                          requireMemo('E',50);
  187.              printlink();
  188.                          break;
  189.                 case'4':
  190.                          requireMemo('A',8);
  191.                  requireMemo('B',16);
  192.              requireMemo('C',64);
  193.                  requireMemo('D',124);
  194.                          freeMemo('C');
  195.                          requireMemo('E',50);
  196.                          freeMemo('D');
  197.              printlink();
  198.                          break;
  199.                 default:
  200.             cout<<"选择错误!"<<endl;
  201.                         break;
  202.  
  203.                 }
  204.         }
  205. }

回复 "模拟可变分区管理(操作系统)"

这儿你可以回复上面这条便签

captcha