首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >死锁检测算法有代码吗?

死锁检测算法有代码吗?
EN

Stack Overflow用户
提问于 2020-09-17 20:34:25
回答 1查看 767关注 0票数 1

我想找到一个在任何编程语言中实现死锁检测算法的代码。

算法(系统步骤):

将所有zeros.

  • Initialize的分配矩阵中有一行的
  1. 标记为一个临时向量W,以等于可用向量。
  2. 找到一个索引I,使进程i当前未标记,而i(q的行)小于或等于W。即,齐克≤Wk,1≤k≤m。如果没有找到这样的行,则终止算法。如果找到这样的行,则终止
  3. ,标记进程i并将相应的分配矩阵行添加到W。即,设Wk = Wk + Aik,为1≤k≤m。返回到步骤3.

用户输入: 1-索赔矩阵2-分配矩阵3-可用矩阵

系统输出: 1-打印C-A矩阵2-打印W向量,为每个步骤3分配和C-A矩阵-最后打印标记的进程(没有死锁)和未标记的进程(已死锁)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-21 12:55:00

试图寻找密码。我在Java中找到了部分内容,并做了一些编辑。希望能帮上忙。

代码语言:javascript
复制
package com.company;
import java.io.*;
import java.util.*;
    
    class Main {
        static int safe[] = new int[20]; //Array for safe
        static int unsafe[] = new int[20]; // Array for unsafe
    
        static boolean safe(int available[], int allocated[][], int need[][], int n1, int m1) {
            int n = n1; //number of processes
            int m = m1; //number of resources
            int nd[][] = new int[n][m];
            int work[] = new int[m];
            int alloc[][] = new int[n][m];
    
    
            for (int i = 0; i < m; i++) { //Work var
                work[i] = available[i];
            }
    
            /** Print Available Matrix */
            System.out.println("*** Available Matrix /n");
            for( int i = 0; i < work.length; i++ ) {
                System.out.println( work[i] + " " );
            }
    
    
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    alloc[i][j] = allocated[i][j];
                }
            }
    
            /** Print Allocated Matrix */
            System.out.println("*** Allocated Matrix /n");
            for( int i = 0; i < alloc.length; i++ ) {
                for( int j = 0; j < alloc[i].length; j++ ) {
                    System.out.print( alloc[i][j] + " " );
                }
                System.out.println();
            }
    
    
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    nd[i][j] = need[i][j];
                }
            }
    
            /** Print Need Matrix */
            System.out.println("*** Need Matrix ");
            for( int i = 0; i < nd.length; i++ ) {
                for( int j = 0; j < nd[i].length; j++ ) {
                    System.out.print( nd[i][j] + " " );
                }
                System.out.println();
            }
    
            /* Hold the finished processes */
            boolean finish[] = new boolean[n];
    
    
            for (int i = 0; i < n; i++) {
                finish[i] = false;
            }
    
    
            int check = 0;
            int check1 = 0;
    
    
            do {
                for (int i = 0; i < n; i++) {
                    boolean flag = true; /* Temp Flag */
                    if ( finish[i] == false ) {
                        for (int j = 0; j < m; j++) {
                            if (work[j] < nd[i][j]) {
                                flag = false;
                            }
                        }
    
                        /* If available resources are greater than needed, then assign allocated resources
                         for processing */
                        if ( flag ) {
                            for (int j = 0; j < m; j++) {
                                work[j] += alloc[i][j];
                            }
                            safe[check] = i; /* Put the process number (Pi) in the safe Matrix */
                            check++;
                            finish[i] = true;
                        } else {
                            unsafe[check] = i; /* Put the process number (Pi) in the safe Matrix */
                        }
                    }
                }
    
                check1++; /* Start on the next process */
            } while ( check < n && check1 < n);
    
            if (check > n) {
                return false;
            } else {
                return true;
            }
        }
        /** Main Function */
        public static void main(String args[]) throws IOException {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader obj = new BufferedReader(isr);
    
            int n, m;
            System.out.println("Enter no. of processes: "); /* Enter number of processes. */
            n = Integer.parseInt(obj.readLine());
            System.out.println("Enter no. of resources: "); /* Enter number of Resources. */
            m = Integer.parseInt(obj.readLine());
    
            int availableArr[] = new int[m]; // Matrix of available instances
            for (int i = 0; i < m; i++) {
                System.out.println("Enter no. of available instances resources: " + i);
    
                availableArr[i] = Integer.parseInt(obj.readLine());
            }
    
            System.out.println("Enter allocation of resources: ");
            // Allocation Matrix. //
            int allocArr[][] = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    /* Enter allocation of instances of resources. */
                    System.out.println("Enter allocation instances of resources: " + j + " for process p" + i);
                    /* Enter allocation of instances of resources. */
                    allocArr[i][j] = Integer.parseInt(obj.readLine());
                }
            }
    
            System.out.println("enter maximum of resources: ");
            int maxArr[][] = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    System.out.println("enter max instances of resources:" + j + "for process p" + i);
    
                    maxArr[i][j] = Integer.parseInt(obj.readLine());
                }
            }
    
            /** Print Max Matrix */
            System.out.println("*** Max Matrix \n");
            for( int i = 0; i < maxArr.length; i++ ) {
                for( int j = 0; j < maxArr[i].length; j++ ) {
                    System.out.print( maxArr[i][j] + " " );
                }
                System.out.println();
            }
    
            /* Calculate Need Matrix */
            int needArr[][] = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    /* Need= Max - Allocation */
                    needArr[i][j] = maxArr[i][j] - allocArr[i][j];
                }
            }
    
            if ( safe( availableArr, allocArr, needArr, n, m )) {
                System.out.println("System is at a safe state");
                System.out.print("System's Safe sequence:");
                for (int i = 0; i < n; i++) {
                    System.out.print( "P" + safe[i] + " " );
                }
                System.out.println();}
                else{
                    System.out.println("The system is at unsafe state");
                System.out.println("System's UnSafe sequence");
                for (int i = 0; i < n; i++) {
                    System.out.print( "P" + unsafe[i] + " " );
                }
            }
            System.out.println("System's UnSafe sequence"); //to print the unsafe sequence if the it's a safe state (lead to deadlock)
            for (int i = 0; i < n; i++) {
                System.out.print( "P" + unsafe[i] + " " );
            }
    
    
        }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63945845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档