• 大小: 6KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: Java
  • 标签: 操作系统  java代码  

资源简介

java实现银行家算法 1、给出系统可用资源向量(例如:系统可用资源=(5,3,8,2,10))。 2、若干进程最大需求矩阵如下表所示: 3、采用时间片轮转法调度进程。 4、进程执行时提出资源请求(可利用随机数给出或从键盘输入)。 5、判断资源是否可以安全分配,要求进程每提出一个资源请求,都要进行安全

资源截图

代码片段和文件信息

package process;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Bank {

    public static int Max[][] = { {33505} {53812} {21204}  
            {40705 } {32629} };  
    public static int AVAILABLE[] = {538210 };  
    public static int ALLOCATION[][] = { { 0 0 0 00} { 0 0 000 } { 0 0 000 }
            { 0 0 000 } { 0 0 000 } };  
    public static int NEED[][] = { {33505} {53812} {21204}  
        {40705 } {32629} };    
    public static int Request[] = { 0 0 000 };  
    public static int M = 5 N = 5;  
    int FALSE = 0;  
    int TRUE = 1;  
    public void showdata() {  
        int i j;  
        System.out.print(“系统可用的资源数为:/n“);  
        for (j = 0; j < N; j++) {  
            System.out.println(“资源“ + j + “:“ + AVAILABLE[j] + “ “);  
        }  
        System.out.println();  
        System.out.println(“各进程还需要的资源量:“);  
        for (i = 0; i < M; i++) {  
            System.out.println(“进程“ + i + “:“);  
            for (j = 0; j < N; j++) {  
                System.out.print(“资源“ + j + “:“ + NEED[i][j] + “ “);  
            }  
            System.out.println();  
        }  
        System.out.print(“各进程已经得到的资源量: /n“);  
        for (i = 0; i < M; i++) {  
            System.out.print(“进程“);  
            System.out.print(i);  
            for (j = 0; j < N; j++) {  
                System.out.print(“资源“ + j + “:“ + ALLOCATION[i][j] + “ “);  
            }  
            System.out.println();
        }  
    }  
    public void changdata(int k) {  
        int j;  
        for (j = 0; j < N; j++) {  
            AVAILABLE[j] = AVAILABLE[j] - Request[j];  
            ALLOCATION[k][j] = ALLOCATION[k][j] + Request[j];  
            NEED[k][j] = NEED[k][j] - Request[j];  
        }  
    };   
    public void rstordata(int k) {  
        int j;  
        for (j = 0; j < N; j++) {  
            AVAILABLE[j] = AVAILABLE[j] + Request[j];  
            ALLOCATION[k][j] = ALLOCATION[k][j] - Request[j];  
            NEED[k][j] = NEED[k][j] + Request[j];  
        }  
    };  
    public void free(int k) {  
        for (int j = 0; j < N; j++) {  
            AVAILABLE[j] = AVAILABLE[j] + ALLOCATION[k][j];  
            System.out.println(“释放“ + k + “号进程的“ + j + “资源!/n“);  
        }  
    }  
    public int check0(int k) {  
        int j n = 0;  
        for (j = 0; j < N; j++) {  
            if (NEED[k][j] == 0)  
                n++;  
        }  
        if (n == 5)  
            return 1;  
        else  
            return 0;  
    }  
    public int chkerr(int s) {  
        int WORK;  
        int FINISH[] = new int[M] temp[] = new int[M]; 
        int i j k = 0;  
        for (i = 0; i < M; i++)  
            FINISH[i] = FALSE;  
        for (j = 0; j < N; j++) {  
            WORK = AVAILABLE[j];  
            i = s;  
            while

评论

共有 条评论