📌  相关文章
📜  教资会网络 | UGC NET CS 2015 年六月 – II |问题 50(1)

📅  最后修改于: 2023-12-03 14:54:49.610000             🧑  作者: Mango

UGC NET CS 2015 June - II Question 50

Introduction

The UGC NET CS 2015 June - II Exam is conducted by the National Testing Agency (NTA) for candidates aspiring to be Assistant Professors or Junior Research Fellows in Computer Science. The exam consists of three papers: Paper I, Paper II, and Paper III. This article focuses on Question 50 from Paper II.

Question 50

Write a program to implement the banker's algorithm. Assume the following data:

Number of processes: 5
Number of resources: 3

Allocation Matrix:
    A B C
P0  0 1 0
P1  2 0 0
P2  3 0 2
P3  2 1 1
P4  0 0 2

Max Matrix:
    A B C
P0  7 5 3
P1  3 2 2
P2  9 0 2
P3  2 2 2
P4  4 3 3

Available Resources:
A B C
3 3 2
Solution

Here is the implementation of the banker's algorithm in Python:

def bankers_algorithm(n_p, n_r, allocation, max_req, available):
    # Step 1: Initialize variables
    need = [[0] * n_r for i in range(n_p)]
    safe_seq = []

    # Calculate the need matrix
    for i in range(n_p):
        for j in range(n_r):
            need[i][j] = max_req[i][j] - allocation[i][j]

    # Step 2: Check for safe state
    work = list(available)
    finish = [0] * n_p
    found = True
    while found:
        found = False
        for i in range(n_p):
            if finish[i] == 0:
                if all(j >= 0 for j in need[i]):
                    if all(j <= work[idx] for idx, j in enumerate(allocation[i])):
                        work = [work[idx] + allocation[i][idx] for idx in range(n_r)]
                        finish[i] = 1
                        safe_seq.append(i)
                        found = True

    # Step 3: Return results
    if all(i == 1 for i in finish):
        return safe_seq
    else:
        return False
Explanation

The bankers_algorithm function takes the following parameters:

  • n_p: The number of processes.
  • n_r: The number of resources.
  • allocation: A 2D list representing the allocation matrix.
  • max_req: A 2D list representing the maximum requirement matrix.
  • available: A list representing the available resources.

The function first initializes the need and safe_seq variables. It then calculates the need matrix by subtracting the allocation matrix from the maximum requirement matrix.

Next, the function checks if the initial state is safe by implementing the banker's algorithm. It does so by first initializing the work and finish variables. The work variable represents the current available resources. The finish variable keeps track of whether a process has finished executing.

The function then enters a loop where it checks if a process can execute or not. If it can execute, it updates the work variable, marks the process as finished, and adds it to the safe_seq list. The loop runs until all processes are executed or there is no further progress.

Finally, the function returns the safe sequence if all processes are executed, or False if the system is in an unsafe state.

Conclusion

The banker's algorithm is an important algorithm used in operating systems to allocate resources in a safe and efficient manner. It is used to avoid deadlock and ensure that all processes are executed in a safe and secure manner. This implementation in Python can be used by programmers to test and implement the algorithm in their own programs.