How can I find a safe sequence while using the Banker's algori开发者_如何学Gothm for avoiding deadlock? If I use safety algorithm, it doesn't give a sequence as output. Then how would I get a sequence that is safe?
With the banker's algorithm you know the total number of resources available, and the maximum number of resources that each process may request. Being conservative, you're a banker after all, you assume that any process will request its maximum resources before finishing.
When resources are requested, you look first at whether there are enough resources left so that at least one process can receive its maximum. If there would not be enough resources left then you cannot grant the request as you would enter an unsafe state.
Now assume that all the processes that you determined could acquire their maximum and finish have done so and released the resources that they were holding onto. This will allow more processes to acquire their maximum request and finish, freeing more resources.
Repeat until either all the processes have finished (in which case the request is OK to allocate), or there are some processes that cannot acquire their maximum even after all the processes that can finish have released their resources.
In practice (and on exam problems worked by hand) it usually does not take more than a couple of passes though the list of processes to see if you are in a safe or unsafe state.
In the safety algorithm, define the finished vector as an array of integers initialized to zero. Instead of setting the flag in the finished vector to true, set it to an incremented order number. At the end the finished vector will contain the sequence order. Unfinished processes will have their finished order equal to zero.
精彩评论