操作系统实验——主存空间的分配和回收 c#模拟

    技术2024-01-10  97

    一、实习内容

    主存储器空间的分配和回收。

    二、实习目的

    通过本实习帮助理解在不同的存储管理方式下应怎样进行存储空间的分配和回收。

    三、实习题目

    在分页管理方式下采用位示图来表示主存分配情况,实现主存分配和回收 [提示]: (1) 假定系统的主存被分成大小相等的64个块,用0/1对应空闲/占用。 (2) 当要装入一个作业时,根据作业对主存的需求量,先查空闲块数是否能满足作业要求,若能满足,则查位示图,修改位示图和空闲块数。位置与块号的对应关系为: 块号=j*8+i,其中i表示位,j表示字节。 根据分配的块号建立页表。页表包括两项:页号和块号。 (3) 回收时,修改位示图和空闲块数。 要求能接受来自键盘的空间申请及释放请求,能显示位示图和空闲块数的变化,能显示进程的页表。

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CaoZuoXiTong2 { class Program { static int memory = 64;//内存使用情况 static int[] record = new int[64];//位示图数组 static int[] work = new int[100];//记录作业占用多少内存的数组 static int[,] work_ = new int[100,100];//记录作业具体占用的块号 static int index = 0;//作业下标 static void Main(string[] args) { for (int x=0; x < 100; x++)//初始化 { for(int y = 0; y < 100; y++) { work_[x, y] = 100; } } Console.WriteLine("假设每个页大小为1K,每个块大小也为1K,共分为64块\n"); int judge = 0; while (judge != 3) { Console.WriteLine("输入1申请空间,输入2释放空间,输入3退出"); judge = Convert.ToInt32(Console.ReadLine()); switch (judge) { case 1: request(); break; case 2: release(); break; case 3: judge = 3; break; } } } static void request() { string table = "-----------------\n作业"+index+"的页表:\n页号 块号\n"; Console.WriteLine("你在为作业" + index + "申请内存"); Console.WriteLine("输入申请的内存大小:"); int a = Convert.ToInt32(Console.ReadLine());//申请内存大小 if (a > memory) Console.WriteLine("超出内存,申请失败"); else { work[index] = a; memory -= a; int m = 0; for (int i = 0; i < 64; i++) { if (a != 0 && record[i] == 0) { record[i] = 1; table = table +m+ " " + i+"\n"; work_[index, a] = i; a--; m++; } } index++; Console.WriteLine("---------------------------\n申请完成\n" + table); show(); } } static void release() { Console.WriteLine("请输入想释放的作业号:"); int a = Convert.ToInt32(Console.ReadLine());//释放的作业号 for(int i = 0; i < 100; i++) { if (work_[a, i] != 100) record[work_[a, i]] = 0; } memory = memory + work[a]; Console.WriteLine("---------------------------\n释放完成\n" ); show(); } static void show() { Console.WriteLine("----------------------\n现空闲块数:" + memory+"\n--------------------\n位示图:"); int a = 1; for(int i = 0; i < 8; i++) { Console.WriteLine(record[i * a] + " " + record[i * a + 1] + " " + record[i * a + 2] + " " + record[i * a + 3] + " " + record[i * a + 4] + " " + record[i * a + 5] + " " + record[i * a + 6] + " " + record[i * a + 7] + " \n"); a = 8; } } } }

    才疏学浅,如有错误请多指正

    转载请注明出处

    Processed: 0.011, SQL: 9