⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ram.java

📁 一个简单的操作系统模拟程序 java+swing 可进行一般的文件操作 进程建立及删除等
💻 JAVA
字号:
/*
 * RAM.java ver 0.1.0
 *
 * Created on 2006年12月12日, 下午3:11
 *
 * The simulation ram for the system
 *
 *History : 2006年12月12日,ver 0.1.0 class create ,first release
 *
 */

package ossimulation;

import java.util.ArrayList;
import java.util.Iterator;
/**
 *
 * @author Decell
 */
public class RAM {
    private byte[][] ramInstance;
    private ArrayList freeSpaceList;
    
    /** Creates a new instance of RAM */
    public RAM() {
        //initialization code for the ram
        ramInstance = new byte[128][4];
        for(int i = 0;i <= 127;i++){
            for(int j = 0;j <= 3;j++){
                ramInstance[i][j] = 0;//initialize the ram,'0' indicate that the byte idnot uesd;
            }
        }
        
        //initialization code for the free space list
        int[] tag = new int[2];//the tag indiacte the free space in the ram
        tag[0] = 0;//tag[0] indicate  the free space offset
        tag[1] = 127;//tag[1] indicate the the amount of the free space  
        freeSpaceList = new ArrayList();
        freeSpaceList.add(tag);//add the initial tag into the list
        
    }
    
    /**Request specifical free space from the ram,-1 indicate that the allocation is faile*/
    public int requestSpace(int argAmount){
        int amount = argAmount;
        int offset = -1;//-1 indiacte that there is no free space in the ram
        int[] tag;
        Iterator freeSpaceListIterator = freeSpaceList.iterator();//get the iterator of the list
        
        for(;freeSpaceListIterator.hasNext();){
            tag = (int[])freeSpaceListIterator.next();//get the free space tag
            //find the suitable free space
            if(tag[1] >= amount){
                offset = tag[0];//get the free space's offset'
                //allocate the free space
                for(int i = 0;i < amount;i++){
                    for(int j = 0;j <= 3;j++){
                        ramInstance[offset + i][j] = 1;
                    }
                }
                break;
            }
        }
        
        this.maintainFreeSpaceList();
        return offset;
    }
    
    /**Free specifial space in the ram*/
    public void freeSpace(int argOffset,int argAmount){
        int offset = argOffset;
        int amount = argAmount;
        //free the space
        for(int i = 0;i < amount;i++){
            for(int j = 0;j <= 3;j++){
                ramInstance[offset + i][j] = 0;
            }
        }
    
        this.maintainFreeSpaceList();
    }
    
    //maintain the free space list
    private void maintainFreeSpaceList(){
        int offset;
        int amount;
        
        freeSpaceList.clear();//clear the list first
        offset = 0;
        amount = 0;
        for(int index = 0;index <= 127;index++){
            if(ramInstance[index][0] == 0){
                offset = index;
                for(;index <= 127 && ramInstance[index][0] == 0;index++){
                    amount++;
                }
                int[] tag = new int[2];
                tag[0] = offset;
                tag[1] = amount;
                freeSpaceList.add(tag);
                amount = 0;
            }
        }
        
    }
    
    /**calculate the usage of the menory*/
    public int calculateRAM(){
        int usage = 0;
        int result = 0;
        float temp;

        for(int i = 0;i <= 127;i++){
            if(ramInstance[i][0] == 1){
                usage++;
            }
        }
        temp = (float)usage / 128 * 100;
        result = (int)temp;
        
        return result;
    }

    //The main method is for class testing
    public static void main(String[] args){
        RAM ram = new RAM();
        int offset1 = ram.requestSpace(10);
        int offset2 = ram.requestSpace(4);
        int offset3 = ram.requestSpace(18);
        int offset4 = ram.requestSpace(1);
        ram.freeSpace(offset2,4);
        offset2 = ram.requestSpace(5);
        ram.freeSpace(offset3,18);
        offset3 = ram.requestSpace(4);
        ram.freeSpace(offset1,10);
        offset1 = ram.requestSpace(60);
        ram.freeSpace(offset4,1);
        offset4 = ram.requestSpace(10);
        
        System.out.print(offset1 + " ");
        System.out.print(offset2 + " ");
        System.out.print(offset3 + " ");
        System.out.print(offset4 + " ");
        
        //show the usage of the ram
        System.out.print(ram.calculateRAM() + " ");
    }




}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -