11°

操作系统银行家算法

1.数据结构

  1. 可利用的资源向量Available:一个含有m个元素的数组,其中每一个元素代表一类可利拥的资源数目,其初始值是系统中所配置的该类全部可用资源数目,其数值随该类资源的分配改变而改变。如果Available[j]=K,则表示系统中现有Rj类资源K个。
  2. 最大需求矩阵Max:一个n×m的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。如果Max[i,j]=K,则表示进程i需要Rj类资源的最大数目为K。
  3. 分配矩阵Allocation:一个n×m的矩阵,它定义了系统中每一类资源当前已分配给每一进程的资源数。如果Allocation[i,j]=K,则表示进程i当前已分得Rj类资源的数目为K。
  4. 需求矩阵Need:一个n×m的矩阵,用以表示每一个进程尚需的各类资源数。如果Need[i,j]=K,则表示进程i还需要Rj类资源K个方能完成其任务。

Need[i,j]=Max[i,j]-Allocation[i,j] 

2.银行家算法

在避免死锁的方法中,所施加的限制条件较弱,有可能获得令人满意的系统性能。在该方法中把系统的状态分为安全状态和不安全状态,只要能使系统始终都处于安全状态,便可以避免发生死锁。银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。

设进程cusneed提出请求REQUEST [i],则银行家算法按如下规则进行判断。

(1)如果REQUEST [cusneed] [i]<= NEED[cusneed][i],则转(2);否则,出错。

(2)如果REQUEST [cusneed] [i]<= AVAILABLE[i],则转(3);否则,等待。

(3)系统试探分配资源,修改相关数据:

AVAILABLE[i]-=REQUEST[cusneed][i];

ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];

NEED[cusneed][i]-=REQUEST[cusneed][i];

(4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。

 

3.安全性算法 

(1)设置两个工作向量Work=AVAILABLE;FINISH

(2)从进程集合中找到一个满足下述条件的进程,

FINISH==false;

NEED<=Work;

如找到,执行(3);否则,执行(4)

(3)设进程获得资源,可顺利执行,直至完成,从而释放资源。

Work=Work+ALLOCATION;

Finish=true;

GOTO 2

(4)如所有的进程Finish= true,则表示安全;否则系统不安全。

 

主要代码:

安全性检测:

public void allocation(){
        flag_i.clear();
        for (int i = 0; i < this.processes_Size ; i++) {
            Finish[i] = false;
        }
        for (int i = 0; i < this.Resources_Size ; i++) {
            Work[0][i] = AvailableTmp[0][i];
        }
        int count = 0;
        int flag = 0; //flag_i中的个数
        while (count < this.processes_Size){
            for (int i = 0; i < this.processes_Size ; i++) {
                if(flag_i.contains(i)){
                    continue;
                }else {
                    int k = 0;
                    while (k < this.Resources_Size){
                        if(NeedTmp[i][k] <= Work[count][k] && (this.Finish[count]==false)){
                            k++;
                        }else {
                            k = this.Resources_Size+1;
                        }
                    }
                    if(k == this.Resources_Size){
                        flag_i.add(i);
                        flag++;
                        break;   //每次添加一个
                    }
                }
            }
        </span><span style="color: #008000;">//</span>
        <span style="color: #008000;">//</span><span style="color: #008000;">每走一遍for循环应该找到一个,才能往下执行</span>
        <span style="color: #0000ff;">if</span>((count+1) ==<span style="color: #000000;"> flag){
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
                Work_All[count][i] </span>= Work[count][i] + AllocationTmp[(<span style="color: #0000ff;">int</span><span style="color: #000000;">) flag_i.get(count)][i];
            }
            </span><span style="color: #0000ff;">this</span>.Finish[count] = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
            count</span>++<span style="color: #000000;">;
            </span><span style="color: #008000;">//</span><span style="color: #008000;">赋值</span>
            <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
                Work[count][i] </span>= Work_All[count - 1<span style="color: #000000;">][i];
            }
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            count</span>++<span style="color: #000000;">;
            System.out.println(</span>"第"+count+"次安全性检测失败!"<span style="color: #000000;">);
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span>(Finish[count-1] == <span style="color: #0000ff;">false</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">跳出循环如果有进程数个值,说明每个进程都能满足,将试分配的值真实分配</span>
    <span style="color: #0000ff;">if</span>(flag_i.size() == <span style="color: #0000ff;">this</span><span style="color: #000000;">.processes_Size){
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
                Allocation[i][j] </span>=<span style="color: #000000;">AllocationTmp[i][j];
                Need[i][j] </span>=<span style="color: #000000;"> NeedTmp[i][j];
            }
        }
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
            Available[</span>0][i] = AvailableTmp[0<span style="color: #000000;">][i];
        }
        showResult();
        System.out.println(</span>"*********************************"<span style="color: #000000;">);
        show();
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        </span><span style="color: #0000ff;">if</span>(flag_i.size() == 0<span style="color: #000000;">){
            System.out.println(</span>"不满足任何进程的需求!"<span style="color: #000000;">);
            System.out.println(</span>"无安全序列!"<span style="color: #000000;">);
            System.out.println(</span>"******************************"<span style="color: #000000;">);
            show();
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            System.out.println(</span>"无安全序列!"<span style="color: #000000;">);
            System.out.println(</span>"*****************************"<span style="color: #000000;">);
            show();
        }

    }

}</span></pre> 

 

银行家分配部分:

 private void requset() {
        copy();
        System.out.println("请输入要请求资源的进程号:");
        String name = scanner.next();
        int putInto = -1;    //判断输入的是几号进程
        for (int i = 0; i < this.processes_Size; i++) {
            if (name.equals(processes[i])){
                putInto = i;
            }
        }
        //如果输入的在进程序列内不存在
        if(putInto == -1){
            System.out.println("非法输入!");
            return;
        }
        System.out.println();
        System.out.println("请输入再次申请的资源大小:");
        int[] num = new int[this.Resources_Size];   //输入的request值
        for (int i = 0; i < this.Resources_Size; i++) {
            num[i] = scanner.nextInt();
        }
        int k = 0;
        for (int i = 0; i < this.Resources_Size; i++) {
            if ((num[i] <= Need[putInto][i]) && (num[i] <= Available[0][i])) {
                k++;
            } else {
                if((num[i] > Need[putInto][i])) {
                    System.out.println("申请大于需要的进程!");
                    System.out.println("***************************");
                    show();
                    return;
                }
                if(num[i] > Available[0][i]){
                    System.out.println("资源不足!");
                    System.out.println("***************************");
                    show();
                    return;
                }
        }

    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">跳出说明满足要求尝试分配资源</span>
    <span style="color: #0000ff;">if</span>(k == <span style="color: #0000ff;">this</span><span style="color: #000000;">.Resources_Size){
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            NeedTmp[putInto][j] </span>= NeedTmp[putInto][j] -<span style="color: #000000;"> num[j];
            AllocationTmp[putInto][j]  </span>= AllocationTmp[putInto][j] +<span style="color: #000000;"> num[j];
            AvailableTmp[</span>0][j] = AvailableTmp[0][j] -<span style="color: #000000;"> num[j];
        }

    }
          allocation();
}</span></pre> 

         但是遇到的问题是,当满足银行家算法但是不满足安全性算法时,怎么将银行家预分配的再次还给初始分配前的值,最终采用临时的副本数组,每次安全性算法都是对副本操作,当满足时,才将原数组改变,然后将其输出,当然还有当一个就是一个进程的所需要的资源已经全部得到,此时应该将其删除序列表,并且将其拿到的资源全部还给系统,以便于其他进程的申请。

 

删除进程(已经获取到了全部所需要的资源):

 private void resReturn(){
        int count = 0;
        int num = 0;
        while (count < processes_Size) {
            for (int i = 0; i < this.Resources_Size; i++) {
                if (Need[count][i] == 0){
                    num++;
                }
            }
            if(num == Resources_Size){
                for (int i = 0; i < this.Resources_Size ; i++) {
                    Available[0][i] = Allocation[count][i]+Available[0][i];
                }
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
                </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">if</span>(i &lt;<span style="color: #000000;"> count){
                        Need[i][j] </span>=<span style="color: #000000;"> Need[i][j];
                        Allocation[i][j] </span>=<span style="color: #000000;"> Allocation[i][j];
                        processes[i] </span>=<span style="color: #000000;"> processes[i];
                    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    Need[i][j] </span>= Need[i+1<span style="color: #000000;">][j];
                    Allocation[i][j] </span>= Allocation[i+1<span style="color: #000000;">][j];
                    processes[i] </span>= processes[i+1<span style="color: #000000;">];
                    }
                }
            }
            processes_Size</span>--<span style="color: #000000;">;
        }
        num </span>= 0<span style="color: #000000;">;
        count</span>++<span style="color: #000000;">;
    }

}</span></pre> 

 

测试用例:

 

 

 

 

 

 申请资源结果:

 

 

完整代码如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;

public class Dijkstra { private int[][] Max; //资源最大需求 private int[][] Allocation; //已经分配的资源 private int[][] Need; //需要的资源 private int[][] Available; //剩余的资源 private int[][] Work; //执行资源 private int[][] Work_All; //目前剩余的资源 private boolean[] Finish; //是否安全 private ArrayList flag_i; //存入满足当前需求的资源序号

<span style="color: #0000ff;">private</span><span style="color: #000000;"> Scanner scanner;

</span><span style="color: #0000ff;">private</span> String[] processes;    <span style="color: #008000;">//</span><span style="color: #008000;">进程</span>
<span style="color: #0000ff;">private</span>  resource[] resources;   <span style="color: #008000;">//</span><span style="color: #008000;">资源</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> MAX_Vaue = 20;       <span style="color: #008000;">//</span><span style="color: #008000;">最大的资源以及进程数</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> processes_Size;      <span style="color: #008000;">//</span><span style="color: #008000;">真实进程个数</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> Resources_Size;      <span style="color: #008000;">//</span><span style="color: #008000;">真实资源个数


</span><span style="color: #008000;">//</span><span style="color: #008000;">副本数据</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span>[][] AllocationTmp;  <span style="color: #008000;">//</span><span style="color: #008000;">已经分配的资源</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span>[][] NeedTmp;       <span style="color: #008000;">//</span><span style="color: #008000;">需要的资源</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span>[][] AvailableTmp;   <span style="color: #008000;">//</span><span style="color: #008000;">剩余的资源</span>
<span style="color: #0000ff;">private</span> String[] processesTmp;   <span style="color: #008000;">//</span><span style="color: #008000;">进程</span>

<span style="color: #0000ff;">class</span><span style="color: #000000;"> resource{
    String resourceName;     </span><span style="color: #008000;">//</span><span style="color: #008000;">资源名</span>
    <span style="color: #0000ff;">int</span> resourceSize;       <span style="color: #008000;">//</span><span style="color: #008000;">资源大小</span>
    <span style="color: #0000ff;">public</span> resource(String name,<span style="color: #0000ff;">int</span><span style="color: #000000;"> size){
        resourceName </span>=<span style="color: #000000;"> name;
        resourceSize </span>=<span style="color: #000000;"> size;
    }
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 初始化
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span><span style="color: #000000;"> Dijkstra(){
    </span><span style="color: #0000ff;">this</span>.Max = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Allocation = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Need = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Available = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[1<span style="color: #000000;">][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Work = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Work_All = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.Finish = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;">[MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.processes = <span style="color: #0000ff;">new</span><span style="color: #000000;"> String[MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.resources = <span style="color: #0000ff;">new</span><span style="color: #000000;"> resource[MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.scanner = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scanner(System.in);
    </span><span style="color: #0000ff;">this</span>.flag_i = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ArrayList();

    </span><span style="color: #008000;">//</span><span style="color: #008000;">副本</span>
    <span style="color: #0000ff;">this</span>.AllocationTmp = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.NeedTmp = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.AvailableTmp = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span><span style="color: #000000;">[MAX_Vaue][MAX_Vaue];
    </span><span style="color: #0000ff;">this</span>.processesTmp = <span style="color: #0000ff;">new</span><span style="color: #000000;"> String[MAX_Vaue];
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {

    Dijkstra dijkstra </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Dijkstra();
    dijkstra.control();
}



</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 选择功能
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> control() {
    System.out.println(</span>"欢迎来到本系统"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (;;) {
        System.out.println(</span>"============================="<span style="color: #000000;">);
        System.out.println(</span>"请选择:"<span style="color: #000000;">);
        System.out.println(</span>"1.初始化系统"<span style="color: #000000;">);
        System.out.println(</span>"2.再次请求分配"<span style="color: #000000;">);
        System.out.println(</span>"3.添加资源"<span style="color: #000000;">);
        System.out.println(</span>"4.添加进程"<span style="color: #000000;">);
        System.out.println(</span>"0.退出程序"<span style="color: #000000;">);
        System.out.println(</span>"============================"<span style="color: #000000;">);
        </span><span style="color: #0000ff;">int</span> sc =<span style="color: #000000;"> scanner.nextInt();
        </span><span style="color: #0000ff;">switch</span><span style="color: #000000;"> (sc) {
            </span><span style="color: #0000ff;">case</span> 1<span style="color: #000000;">:
                start();
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 2<span style="color: #000000;">:
                requset();
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 3<span style="color: #000000;">:
                addResource();
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 4<span style="color: #000000;">:
                addProce();
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 0<span style="color: #000000;">:
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">default</span><span style="color: #000000;">:
                System.out.println(</span>"无效输入!"<span style="color: #000000;">);
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;

        }
    }
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 添加进程
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> addProce() {
    System.out.println(</span>"请输入进程名:"<span style="color: #000000;">);
    String name </span>=<span style="color: #000000;"> scanner.next();
    </span><span style="color: #0000ff;">this</span>.processesTmp[processes_Size++] =<span style="color: #000000;"> name;
    System.out.println(</span>"请输入进程已经获取的各资源大小:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> a = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (a &lt;<span style="color: #000000;"> Resources_Size){
        </span><span style="color: #0000ff;">int</span> num =<span style="color: #000000;"> scanner.nextInt();
        AllocationTmp[processes_Size</span>-1][a] =<span style="color: #000000;"> num;
        a</span>++<span style="color: #000000;">;
    }
    System.out.println(</span>"请输入该进程还需要的各资源大小:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> b = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (b &lt;<span style="color: #000000;"> Resources_Size){
        </span><span style="color: #0000ff;">int</span> num =<span style="color: #000000;"> scanner.nextInt();
        NeedTmp[processes_Size</span>-1][b] =<span style="color: #000000;"> num;
        b</span>++<span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; processes_Size ; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; Resources_Size; j++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span>(AllocationTmp[processes_Size-1][j] &gt; Available[0][j] ){ <span style="color: #008000;">//</span><span style="color: #008000;">如果添加的进程的某项已经分的资源大于剩余的资源,直接不可以分配</span>
                System.out.println("无效的进程输入!"<span style="color: #000000;">);
                processes_Size</span>--<span style="color: #000000;">;
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
            }
            </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                </span><span style="color: #008000;">//</span><span style="color: #008000;">否则将该进程写入</span>
                <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> k = 0; k &lt; <span style="color: #0000ff;">this</span>.processes_Size; k++<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> p = 0; p &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; p++<span style="color: #000000;">) {
                        Allocation[k][p] </span>=<span style="color: #000000;">AllocationTmp[k][p];
                        Need[k][p] </span>=<span style="color: #000000;"> NeedTmp[k][p];
                    }
                }
                </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> k = 0; k &lt; <span style="color: #0000ff;">this</span>.Resources_Size; k++<span style="color: #000000;">) {
                    Available[</span>0][k] = AvailableTmp[0<span style="color: #000000;">][k];
                }
                </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> k = 0; k &lt; <span style="color: #0000ff;">this</span>.processes_Size ; k++<span style="color: #000000;">) {
                    processes[k] </span>=<span style="color: #000000;"> processesTmp[k];
                }
            }
        }
    }
    calculate();
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 添加资源
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> addResource() {
    System.out.println(</span>"请输入要添加的个数:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> number =<span style="color: #000000;"> scanner.nextInt();
    </span><span style="color: #0000ff;">while</span> (number &gt; 0<span style="color: #000000;">) {
        System.out.println(</span>"请输入资源名:"<span style="color: #000000;">);
        String resouname </span>=<span style="color: #000000;"> scanner.next();
        System.out.println(</span>"请输入资源大小:"<span style="color: #000000;">);
        </span><span style="color: #0000ff;">int</span> size =<span style="color: #000000;"> scanner.nextInt();
        </span><span style="color: #0000ff;">this</span>.resources[<span style="color: #0000ff;">this</span>.Resources_Size++] = <span style="color: #0000ff;">new</span><span style="color: #000000;"> resource(resouname, size);
        number</span>--<span style="color: #000000;">;
    }
    calculate();
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 初始化
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> start() {
    System.out.println(</span>"请输入进程个数:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> proceNum =<span style="color: #000000;"> scanner.nextInt();
    System.out.println(</span>"请输入进程名:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">while</span> (processes_Size &lt;<span style="color: #000000;"> proceNum){
        String proceName </span>=<span style="color: #000000;"> scanner.next();
        </span><span style="color: #0000ff;">this</span>.processes[processes_Size++] =<span style="color: #000000;"> proceName;
    }
    System.out.println(</span>"请输入资源个数:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> resouNum =<span style="color: #000000;"> scanner.nextInt();
    System.out.println(</span>"请输入资源名以及资源的总大小:"<span style="color: #000000;">);

    </span><span style="color: #0000ff;">while</span> (Resources_Size &lt;<span style="color: #000000;"> resouNum){
        String resouName </span>=<span style="color: #000000;"> scanner.next();
        </span><span style="color: #0000ff;">int</span> size =<span style="color: #000000;"> scanner.nextInt();
        </span><span style="color: #0000ff;">this</span>.resources[Resources_Size++] = <span style="color: #0000ff;">new</span><span style="color: #000000;"> resource(resouName,size);
    }

    System.out.println(</span>"请输入目前的分配情况:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">int</span> sc =<span style="color: #000000;"> scanner.nextInt();
            Allocation[i][j] </span>=<span style="color: #000000;"> sc;
        }
    }
    System.out.println(</span>"请输入目前的需要情况:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">int</span> sc =<span style="color: #000000;"> scanner.nextInt();
            Need[i][j] </span>=<span style="color: #000000;"> sc;
        }
    }
    calculate();
}


</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 计算当前的资源分配表
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> calculate(){
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
            Max[i][j] </span>= Allocation[i][j] +<span style="color: #000000;"> Need[i][j];
        }
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">求每个进程分出去的总和</span>
    <span style="color: #0000ff;">int</span>[] resour_sun = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[<span style="color: #0000ff;">this</span>.Resources_Size]; <span style="color: #008000;">//</span><span style="color: #008000;">每个进程分出去的总和数组</span>
    <span style="color: #0000ff;">int</span> tmp = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (tmp &lt; <span style="color: #0000ff;">this</span><span style="color: #000000;">.Resources_Size) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
            resour_sun[tmp] </span>+=<span style="color: #000000;"> Allocation[i][tmp];
        }
        tmp</span>++<span style="color: #000000;">;
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">求出Avaiable</span>
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
        Available[</span>0][i] = resources[i].resourceSize -<span style="color: #000000;"> resour_sun[i];
    }
    copy();
    allocation();
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 给副本赋值
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> copy(){
    </span><span style="color: #008000;">//</span><span style="color: #008000;">给副本赋值</span>
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
            AllocationTmp[i][j] </span>=<span style="color: #000000;">Allocation[i][j];
            NeedTmp[i][j] </span>=<span style="color: #000000;"> Need[i][j];
        }
    }
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
        AvailableTmp[</span>0][i] = Available[0<span style="color: #000000;">][i];
    }
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
        processesTmp[i] </span>=<span style="color: #000000;"> processes[i];
    }
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 展示初始化结果
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> show() {
    resReturn();
    System.out.println(</span>"当前资源情况:"<span style="color: #000000;">);
    System.out.println(</span>"进程名"+"       Max    "+"      Allocation    "+"     Need    "+"     Available     "<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> tmp = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (tmp &lt; 4<span style="color: #000000;">) {
        System.out.print(</span>"          "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
            System.out.print(resources[i].resourceName </span>+ " "<span style="color: #000000;">);
        }
        tmp</span>++<span style="color: #000000;">;
    }
    System.out.println();
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
        System.out.print(processes[i]</span>+"        "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            System.out.print(Max[i][j]</span>+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"          "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            System.out.print(Allocation[i][j]</span>+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"          "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">){
            System.out.print(Need[i][j]</span>+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"        "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span>(i == 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
                System.out.print(Available[</span>0][j] + " "<span style="color: #000000;">);
            }
        }
        System.out.println();
    }
    System.out.println();

}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 判断是否可以分配(安全性的检测)
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> allocation(){
    flag_i.clear();
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
        Finish[i] </span>= <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; i++<span style="color: #000000;">) {
        Work[</span>0][i] = AvailableTmp[0<span style="color: #000000;">][i];
    }
    </span><span style="color: #0000ff;">int</span> count = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">int</span> flag = 0; <span style="color: #008000;">//</span><span style="color: #008000;">flag_i中的个数</span>
    <span style="color: #0000ff;">while</span> (count &lt; <span style="color: #0000ff;">this</span><span style="color: #000000;">.processes_Size){
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size ; i++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(flag_i.contains(i)){
                </span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">int</span> k = 0<span style="color: #000000;">;
                </span><span style="color: #0000ff;">while</span> (k &lt; <span style="color: #0000ff;">this</span><span style="color: #000000;">.Resources_Size){
                    </span><span style="color: #0000ff;">if</span>(NeedTmp[i][k] &lt;= Work[count][k] &amp;&amp; (<span style="color: #0000ff;">this</span>.Finish[count]==<span style="color: #0000ff;">false</span><span style="color: #000000;">)){
                        k</span>++<span style="color: #000000;">;
                    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                        k </span>= <span style="color: #0000ff;">this</span>.Resources_Size+1<span style="color: #000000;">;
                    }
                }
                </span><span style="color: #0000ff;">if</span>(k == <span style="color: #0000ff;">this</span><span style="color: #000000;">.Resources_Size){
                    flag_i.add(i);
                    flag</span>++<span style="color: #000000;">;
                    </span><span style="color: #0000ff;">break</span>;   <span style="color: #008000;">//</span><span style="color: #008000;">每次添加一个</span>

} } }

        </span><span style="color: #008000;">//</span>
        <span style="color: #008000;">//</span><span style="color: #008000;">每走一遍for循环应该找到一个,才能往下执行</span>
        <span style="color: #0000ff;">if</span>((count+1) ==<span style="color: #000000;"> flag){
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
                Work_All[count][i] </span>= Work[count][i] + AllocationTmp[(<span style="color: #0000ff;">int</span><span style="color: #000000;">) flag_i.get(count)][i];
            }
            </span><span style="color: #0000ff;">this</span>.Finish[count] = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
            count</span>++<span style="color: #000000;">;
            </span><span style="color: #008000;">//</span><span style="color: #008000;">赋值</span>
            <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
                Work[count][i] </span>= Work_All[count - 1<span style="color: #000000;">][i];
            }
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            count</span>++<span style="color: #000000;">;
            System.out.println(</span>"第"+count+"次安全性检测失败!"<span style="color: #000000;">);
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span>(Finish[count-1] == <span style="color: #0000ff;">false</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">跳出循环如果有进程数个值,说明每个进程都能满足,将试分配的值真实分配</span>
    <span style="color: #0000ff;">if</span>(flag_i.size() == <span style="color: #0000ff;">this</span><span style="color: #000000;">.processes_Size){
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; j++<span style="color: #000000;">) {
                Allocation[i][j] </span>=<span style="color: #000000;">AllocationTmp[i][j];
                Need[i][j] </span>=<span style="color: #000000;"> NeedTmp[i][j];
            }
        }
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
            Available[</span>0][i] = AvailableTmp[0<span style="color: #000000;">][i];
        }
        show();
        System.out.println(</span>"*********************************"<span style="color: #000000;">);
        showResult();
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        </span><span style="color: #0000ff;">if</span>(flag_i.size() == 0<span style="color: #000000;">){
            System.out.println(</span>"不满足任何进程的需求!"<span style="color: #000000;">);
            System.out.println(</span>"无安全序列!"<span style="color: #000000;">);
            System.out.println(</span>"******************************"<span style="color: #000000;">);
            show();
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            System.out.println(</span>"无安全序列!"<span style="color: #000000;">);
            System.out.println(</span>"*****************************"<span style="color: #000000;">);
            show();
        }

    }

}



</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 展示分配后的结果
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> showResult() {
    System.out.println(</span>"当前时刻的安全性:"<span style="color: #000000;">);
    System.out.println(</span>"进程名"+"       work    "+"         Allocation    "+"       Need    "+"         Work+All    "+"         Finish    "<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span> tmp = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (tmp &lt; 4<span style="color: #000000;">) {
        System.out.print(</span>"            "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
            System.out.print(resources[i].resourceName </span>+ " "<span style="color: #000000;">);
        }
        tmp</span>++<span style="color: #000000;">;
    }
    System.out.println();
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
        System.out.print(processes[(</span><span style="color: #0000ff;">int</span>)flag_i.get(i)]+"         "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            System.out.print(Work[i][j]</span>+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"             "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            System.out.print(Allocation[(</span><span style="color: #0000ff;">int</span>)flag_i.get(i)][j]+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"              "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">){
            System.out.print(Need[(</span><span style="color: #0000ff;">int</span>)flag_i.get(i)][j]+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"              "<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">){
            System.out.print(Work_All[i][j]</span>+" "<span style="color: #000000;">);
        }
        System.out.print(</span>"              "<span style="color: #000000;">);
        System.out.print(</span>" "+Finish[i]+" "<span style="color: #000000;">);
        System.out.println();
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">迭代遍历安全序列</span>

System.out.println(); Iterator<Integer> iterator = flag_i.iterator(); System.out.println("存在安全序列为:"); while (iterator.hasNext()) { int num = iterator.next(); if(processes[num] !=null) { System.out.print(processes[num] + " "); } } System.out.println(); }

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 申请资源
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> requset() {
    copy();
    System.out.println(</span>"请输入要请求资源的进程号:"<span style="color: #000000;">);
    String name </span>=<span style="color: #000000;"> scanner.next();
    </span><span style="color: #0000ff;">int</span> putInto = -1;    <span style="color: #008000;">//</span><span style="color: #008000;">判断输入的是几号进程</span>
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (name.equals(processes[i])){
            putInto </span>=<span style="color: #000000;"> i;
        }
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">如果输入的在进程序列内不存在</span>
    <span style="color: #0000ff;">if</span>(putInto == -1<span style="color: #000000;">){
        System.out.println(</span>"非法输入!"<span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
    }
    System.out.println();
    System.out.println(</span>"请输入再次申请的资源大小:"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">int</span>[] num = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[<span style="color: #0000ff;">this</span>.Resources_Size];   <span style="color: #008000;">//</span><span style="color: #008000;">输入的request值</span>
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
        num[i] </span>=<span style="color: #000000;"> scanner.nextInt();
    }
    </span><span style="color: #0000ff;">int</span> k = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span> ((num[i] &lt;= Need[putInto][i]) &amp;&amp; (num[i] &lt;= Available[0<span style="color: #000000;">][i])) {
            k</span>++<span style="color: #000000;">;
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            </span><span style="color: #0000ff;">if</span>((num[i] &gt;<span style="color: #000000;"> Need[putInto][i])) {
                System.out.println(</span>"申请大于需要的进程!"<span style="color: #000000;">);
                System.out.println(</span>"***************************"<span style="color: #000000;">);
                show();
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
            }
            </span><span style="color: #0000ff;">if</span>(num[i] &gt; Available[0<span style="color: #000000;">][i]){
                System.out.println(</span>"资源不足!"<span style="color: #000000;">);
                System.out.println(</span>"***************************"<span style="color: #000000;">);
                show();
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
            }

        }

    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">跳出说明满足要求尝试分配资源</span>
    <span style="color: #0000ff;">if</span>(k == <span style="color: #0000ff;">this</span><span style="color: #000000;">.Resources_Size){
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
            NeedTmp[putInto][j] </span>= NeedTmp[putInto][j] -<span style="color: #000000;"> num[j];
            AllocationTmp[putInto][j]  </span>= AllocationTmp[putInto][j] +<span style="color: #000000;"> num[j];
            AvailableTmp[</span>0][j] = AvailableTmp[0][j] -<span style="color: #000000;"> num[j];
        }

    }
          allocation();
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 删除进程(如果某个资源申请后的need 为0)
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> resReturn(){
    </span><span style="color: #0000ff;">int</span> count = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">int</span> num = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">while</span> (count &lt;<span style="color: #000000;"> processes_Size) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size; i++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (Need[count][i] == 0<span style="color: #000000;">){
                num</span>++<span style="color: #000000;">;
            }
        }
        </span><span style="color: #0000ff;">if</span>(num ==<span style="color: #000000;"> Resources_Size){
            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.Resources_Size ; i++<span style="color: #000000;">) {
                Available[</span>0][i] = Allocation[count][i]+Available[0<span style="color: #000000;">][i];
            }

            </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.processes_Size; i++<span style="color: #000000;">) {
                </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; <span style="color: #0000ff;">this</span>.Resources_Size; j++<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">if</span>(i &lt;<span style="color: #000000;"> count){
                        Need[i][j] </span>=<span style="color: #000000;"> Need[i][j];
                        Allocation[i][j] </span>=<span style="color: #000000;"> Allocation[i][j];
                        processes[i] </span>=<span style="color: #000000;"> processes[i];
                    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    Need[i][j] </span>= Need[i+1<span style="color: #000000;">][j];
                    Allocation[i][j] </span>= Allocation[i+1<span style="color: #000000;">][j];
                    processes[i] </span>= processes[i+1<span style="color: #000000;">];
                    }
                }
            }
            processes_Size</span>--<span style="color: #000000;">;
        }
        num </span>= 0<span style="color: #000000;">;
        count</span>++<span style="color: #000000;">;
    }

}

}

     

本文转载自博客园,原文链接:https://www.cnblogs.com/128-cdy/p/12188340.html

全部评论: 0

    我有话说: