使用Java模拟分页式存储管理代码

这个代码是在温习操作系统时编写的,主要用于通过文字来模拟操作系统中的分页式存储。
该项目一共有两个类构成:
MemoryBlock 类 和 用于运行的 Run 类(主函数类)

代码如下

MemoryBlock 类:

public class MemoryBlock
{
    static int initsize = 2048;//一块的大小
    private String name;//块的名字
    private int size;//一块用了多少
    private String work;//作业名
    private int block;//块号
    private int page;//页号

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public MemoryBlock(){}

    public MemoryBlock(String name,int size,int block)
    {
        this.name = name;
        this.size = size;
        this.block = block;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public int getBlock() {
        return block;
    }

    public void setBlock(int block) {
        this.block = block;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void show()
    {
        if(this.size != 0)
        {
            System.out.println(work + ",页号=" + page + ",块号=" + block + ",占用空间=" +size);
        }
        else
        {
            System.out.println(name);
        }
    }
}

Run 类(主函数类)

import java.util.*;

public class Run 
{
    public static void main(String[] args) 
    {
        int flag = 0;//标记已经使用了多少块
        int work = 0;//作业编号
        List list = new ArrayList<MemoryBlock>();
        List indexof = new ArrayList();
        for(int i=0;i<32;i++)
        {
            list.add(new MemoryBlock("空闲块号"+i,0,i));
        }
        try
        {
            while(true)
            {

                System.out.println("转载自 http://www.codecrh.com/");
                MemoryBlock mb = new MemoryBlock();
                for(int i=0;i<list.size();i++)
                {
                    mb = (MemoryBlock)list.get(i);
                    mb.show();
                }

                System.out.println("总块号" + (list.size()-1) + "," + "已分配" + flag + ",剩余" + (list.size()-flag) + ",每块大小" + mb.initsize);
                System.out.println("页式内存管理");
                System.out.println("1申请内存     2释放内存");


                try
                {
                    Scanner sc = new Scanner(System.in);

                    int input = sc.nextInt();
                    switch (input) 
                    {
                        case 1:
                            System.out.println("请输入申请内存的大小 (按0返回上一级):");
                            int size = sc.nextInt();//申请内存的大小
                            int count = size / mb.initsize;//计算需要的块数

                            if(size % mb.initsize != 0)//不是整倍数
                            {
                                if(size < mb.initsize)
                                {
                                    for(int i=0;i<list.size();i++)
                                    {
                                        mb = (MemoryBlock)list.get(i);
                                        if(mb.getSize() == 0)
                                        {
                                            mb = (MemoryBlock)list.get(i);
                                            break;
                                        }
                                    }

                                    mb.setWork("作业" + work);
                                    mb.setPage(0);
                                    mb.setSize(size);
                                    work++;
                                    flag++;
                                }
                                else if((list.size()-flag)>(count+1))//剩余空间足够
                                {
                                    //for(int j=flag,i=0;i<count;i++,j++)
                                    //{
                                        int jsq = 0;
                                        for(int s=0;s<list.size();s++)
                                        {
                                            mb = (MemoryBlock)list.get(s);
                                            if(mb.getSize() == 0 && jsq < count)
                                            {
                                                mb = (MemoryBlock)list.get(s);
                                                mb.setWork("作业" + work);
                                                mb.setPage(jsq);
                                                mb.setSize(2048);
                                                jsq += 1;
                                            }
                                        }
                                    //}
                                    for(int i=0;i<list.size();i++)
                                    {
                                        mb = (MemoryBlock)list.get(i);
                                        if(mb.getSize() == 0)
                                        {
                                            mb = (MemoryBlock)list.get(i);
                                            break;
                                        }
                                    }
                                    //mb = (MemoryBlock)list.get((flag + count));
                                    mb.setWork("作业" + work);
                                    mb.setPage(count);
                                    mb.setSize(size % mb.initsize);
                                    work++;
                                    flag = flag + count + 1;
                                    continue;
                                }
                                else
                                {
                                    System.out.println("内存不足!!!");
                                    System.out.println("申请内存大小为" + size + ",但没有空间可分配,请重新输入");
                                    continue;
                                }
                            }
                            else//是整倍数
                            {
                                if((list.size()-flag)>count)//剩余空间足够
                                {
                                    //for(int i=0;i<count;i++)
                                    //{
                                        int jsq = 0;
                                        for(int j = 0;j<list.size();j++)
                                        {
                                            mb = (MemoryBlock)list.get(j);
                                            if(mb.getSize() == 0 && jsq < count)
                                            {
                                                mb = (MemoryBlock)list.get(j);
                                                mb.setWork("作业" + work);
                                                mb.setPage(jsq-1);
                                                mb.setSize(2048);
                                                jsq++;
                                            }
                                        }


                                    //}
                                    work++;
                                    flag = flag  + 1;
                                }
                                else
                                {
                                    System.out.println("内存不足!!!");
                                    System.out.println("申请内存大小为" + size + ",但没有空间可分配,请重新输入");
                                    continue;
                                }
                            }
                            break;
                        case 2:
                            List list2 = new ArrayList();
                            System.out.println("请输入释放内存的作业名 (按x退出,按0返回上一级):");
                            sc.nextLine();
                            String name = sc.nextLine();
                            for(int i=0;i<list.size();i++)
                            {
                                mb = (MemoryBlock)list.get(i);
                                if(name.equals(mb.getWork()))
                                    list2.add(i);
                            }
                            for(int i=0;i<list2.size();i++)
                            {
                                int j =  (int)list2.get(i);
                                mb = (MemoryBlock)list.get(j);
                                mb.setWork("");
                                mb.setSize(0);
                                mb.setPage(0);
                                flag--;
                            }
                            break;
                        default:
                                break;
                    }

                }
                catch(Exception ex)
                {
                    continue;
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("发生了意料之外的错误,程序关闭");
        }

    }
}

如果喜欢这篇文章,可以在注明转发自www.codecrh.com的情况下转发这篇文章。