Pay attention to the 7th line in the two sets of codes. Compared with the deadlock detection algorithm, the conditions are weaker, and only Request [i] <W ork Request[i]<WorkRequest[i]<W o r k takes back the processP i P_iPiResources. The banker’s algorithm determines that N eed [i] <W ork Need[i]<WorkNeed[i]<W o r k only recovered resources.
The purpose of the banker algorithm is to avoid deadlocks, that is, during the entire operation and scheduling process of this group of processes, there can be no deadlocks, so it is necessary to judge whether the process can run completely, that is, to obtain all the resources it needs. .
The deadlock detection algorithm only detects whether there is a deadlock at the moment, if Request [i] <W ork Request[i]<WorkRequest[i]<If W o r k is true, it means that the processP i P_iPiAt this moment, it is not involved in the deadlock, so we optimistically assume that the process P i P_iPiIt will not apply for more resources in the future (in many application scenarios, there are such optimistic assumptions), so the resources it holds are directly recovered during the test. Of course, if this optimistic assumption is wrong and a deadlock occurs in the subsequent execution, it will be detected during the execution of the subsequent deadlock detection algorithm.