• 操作系统--实验四


    实验四主存空间的分配和回收

    1.    目的和要求

    1.1.           实验目的

    用高级语言完成一个主存空间的分配和回收程序,以加深对动态分区分配方式及其算法的理解。

    1.2.           实验要求

    采用连续分配方式之动态分区分配存储管理,使用首次适应算法、循环首次适应算法、最佳适应算法和最坏适应算法4种算法完成设计。

    (1)**设计一个作业申请队列以及作业完成后的释放顺序,实现主存的分配和回收。采用分区说明表进行。

    (2)或在程序运行过程,由用户指定申请与释放。

    (3)设计一个空闲区说明表,以保存某时刻主存空间占用情况。

    把空闲区说明表的变化情况以及各作业的申请、释放情况显示。

    2.    实验内容

    根据指定的实验课题,完成设计、编码和调试工作,完成实验报告

    3.    实验环境

    可以选用Visual C++作为开发环境。也可以选用Windows下的VB,CB或其他可视化环境,利用各种控件较为方便。自主选择实验环境。

    4.    代码:

      1 #include<stdio.h>
      2 #include<conio.h>
      3 #include<string.h>
      4 #include<stdlib.h> /*含ma l l o c ( ) 的头文件*/ 
      5 #define MAX 24
      6 #define MEMORY 512
      7 #define OSSIZE 100
      8 struct partition{
      9     
     10     char pn[10];
     11     int begin;
     12     int size;
     13     int end;   ////////
     14     char status;  //////////
     15     struct partition *next;
     16 };
     17 typedef struct partition PART;
     18 int MemorySize(PART *mem,PART *head);
     19 void init(PART *mem,PART *head);
     20 void input(PART *mem,PART *head);
     21 void output(PART *mem,PART *head);
     22 void recycle(PART *mem,PART *head);
     23 void ComMemory(PART *mem,PART *head);
     24 void main(){
     25     
     26     PART *mem,*head;
     27     int sel;
     28     head=mem=(struct partition*)malloc(sizeof(struct partition));/*新节点*/
     29     init(mem,head);
     30     while(1){
     31         printf("
    
    1.输出信息
    2.输入信息
    3.回收作业
    4.退出");
     32         printf("
    
    请选择:");
     33         scanf("%d",&sel);
     34         switch(sel){
     35         case 1:
     36             output(mem,head);
     37             break;
     38         case 2:
     39             //创建新作业
     40             input(mem,head);    
     41             break;
     42         case 3:
     43             recycle(mem,head);
     44             break;
     45         case 4:
     46             exit(0);
     47             break;
     48         default:
     49             printf(" 请正确选择!");    
     50         }                        
     51     }
     52 }
     53 //初始化
     54 void init(PART *mem,PART *head){
     55     PART *q=(struct partition*)malloc(sizeof(struct partition));
     56     printf("初始化:设内存总容量%dk
    ",MEMORY);
     57     printf("系统从低地址部分开始使用,占用%dk
    ",OSSIZE);
     58     head=mem;//链表的头 
     59     mem->begin=0;    
     60     mem->size=OSSIZE;
     61     mem->end=mem->size-mem->begin;
     62     strcpy(mem->pn,"SYSTEM");
     63     mem->status='u';
     64     //mem->next=NULL;
     65 
     66     q->begin=OSSIZE;    
     67     q->size=MEMORY-OSSIZE;
     68     q->end=MEMORY;
     69     strcpy(q->pn,"-----");
     70     q->status='f';
     71     q->next=NULL;
     72     mem->next=q;
     73     mem=q;
     74 }
     75 //回收作业
     76 void recycle(PART *mem,PART *head)
     77 {
     78     char tempName[10];
     79     output(mem,head);
     80     printf("
    
    输入需要回收的作业名");
     81     scanf("%s",&tempName);
     82     mem=head;
     83             while(mem!=NULL)
     84             {
     85                 
     86                 if(strcmp(mem->pn, tempName)==0){
     87                     break;
     88                 }
     89                 else{
     90                     mem=mem->next;
     91                 }
     92             }
     93             if(mem==NULL){
     94             printf("
    回收失败!作业名不存在!
    ");
     95             }else{
     96                     strcpy(mem->pn,"-----");
     97                     mem->status='f';
     98                     ComMemory(mem,head);
     99                     printf("
    回收成功!
    ");
    100             }
    101         while(mem!=NULL)
    102             mem=mem->next;
    103             
    104 }
    105 //计算分配后剩余的内存
    106 int MemorySize(PART *mem,PART *head)
    107 {
    108     int memSize=0;
    109     mem=head;
    110     while(mem!=NULL)
    111     {
    112         if(mem->status=='u')
    113         {
    114             memSize+=mem->size;
    115         }
    116         mem=mem->next;
    117     }
    118     memSize=MEMORY-memSize;
    119     return memSize;
    120 }
    121 //合并空内存块
    122 void ComMemory(PART *mem,PART *head){
    123     mem=head;
    124     while(mem!=NULL)
    125     {
    126         if(mem->next!=NULL){
    127 
    128             if(mem->status==(mem->next)->status){
    129                 mem->end+=mem->next->end;
    130                 mem->size+=mem->next->size;
    131                 mem->next=mem->next->next;
    132             }
    133         }
    134         mem=mem->next;
    135     }
    136 }
    137 
    138 
    139 void input(PART *mem,PART *head){
    140     //创建新作业
    141     PART *p;
    142     PART *q=(struct partition*)malloc(sizeof(struct partition));
    143     int num;
    144 /*    printf("
    
    需要插入的作业个数:");
    145     scanf("%d",&num);
    146     for(int i=0;i<num;i++){*/
    147         
    148         p=(struct partition*)malloc(sizeof(struct partition));/*新节点*/
    149         
    150         while(1){
    151             printf("
    输入作业名:");
    152             scanf("%s",&p->pn);
    153             q=head;
    154             while(q!=NULL)
    155             {
    156                 
    157                 if(strcmp(q->pn, p->pn)==0){
    158                     printf("
    作业名已存在!
    请重新输入 :");
    159                     scanf("%s",&p->pn);
    160                     q=head;
    161                 }
    162                 else{
    163                     q=q->next;
    164                 }
    165             }
    166             if(q==NULL)
    167                 break;            
    168         }
    169         
    170         
    171         while(1){
    172             printf("输入作业需要的内存大小:");
    173             scanf("%d",&p->size);
    174             q=head;
    175             while(q!=NULL)
    176             {
    177                 if(q->status=='f'&&(q->size >= p->size)){
    178                     p->begin=mem->end;
    179                     p->end=mem->end+p->size;
    180                     p->status='u';
    181                     p->next=NULL;    
    182                     mem=p;
    183                     break;
    184                 }
    185                 else{
    186                     q=q->next;
    187                 }
    188             }
    189             if(q!=NULL){
    190                 break;    
    191             }else{
    192                     printf("
    内存大小不足!
    ");
    193                 printf("
    重新");
    194             }
    195         }    
    196         
    197         
    198 /*    }*/
    199     
    200     q=(struct partition*)malloc(sizeof(struct partition));/*新节点*/
    201     strcpy(p->pn,"------");
    202     /*q->size=MemorySize(mem,head);*/
    203     q->size=mem->size-p->size;
    204     q->begin=mem->end;
    205     q->end=q->size+q->begin;
    206     q->status='f';
    207     q->next=NULL;
    208     
    209     p->next=q;
    210     mem->next=p;
    211     mem=p;
    212 }
    213 
    214 
    215 void output(PART *mem,PART *head){
    216     //空闲区打印
    217     printf("
    
    空闲区
    ");
    218     printf("进程名	开始	大小	状态
    ");
    219     mem=head;
    220     while(mem!=NULL)
    221     {
    222         
    223         
    224         if(mem->status=='f')
    225         {
    226             printf("%s	",mem->pn);
    227             printf("%d	",mem->begin);
    228             printf("%d	",mem->size);
    229             printf("%c	",mem->status);
    230             printf("
    ");
    231         }
    232         mem=mem->next;
    233     }
    234     
    235     //已经分配区打印
    236     printf("
    已分配
    ");
    237     printf("进程名	开始	大小	状态
    ");
    238     mem=head;
    239     while(mem!=NULL)
    240     {
    241         
    242         
    243         if(mem->status=='u')
    244         {
    245             printf("%s	",mem->pn);
    246             printf("%d	",mem->begin);
    247             printf("%d	",mem->size);
    248             printf("%c	",mem->status);
    249             printf("
    ");
    250         }mem=mem->next;
    251     }
    252     
    253     //打印全部
    254     printf("
    全部
    ");
    255     printf("进程名	开始	大小	状态
    ");
    256     mem=head;
    257     while(mem!=NULL)
    258     {            
    259         printf("%s	",mem->pn);
    260         printf("%d	",mem->begin);
    261         printf("%d	",mem->size);
    262         printf("%c	",mem->status);
    263         printf("
    ");
    264         mem=mem->next;        
    265     }
    266     
    267     
    268 }

    5.    运行结果:

  • 相关阅读:
    div 水平居中 内容居左
    net core 踩坑记录
    正向代理和反向代理
    NOIP2013 | 货车运输
    【转载】字符串Hash & 【题解】好文章
    cqyz oj | 帮助Jimmy | DAG图
    cqyz oj | 猜序列
    转载 | 原码, 反码, 补码 详解
    cqyz oj | 有线电视网
    cqyz oj | 罕见的秩序 | 拓扑排序
  • 原文地址:https://www.cnblogs.com/wucanlong/p/5594276.html
Copyright © 2020-2023  润新知