The basis of distributed systems is the concurrency and collaboration between multiple processes, which inevitably involves mutually exclusive access to shared resources by multiple processes.
In a single system, mutually exclusive access to shared resources between processes can be easily achieved through the signal volume, mutual exclusion lock;
In distributed systems may be slightly more complex, some of the general idea of mutual exclusion algorithms and the single system is similar to the mutex (such as tokens), but there are many other mutually exclusive methods in the distributed system;
Distributed mutex algorithms can be divided into two different types:
1) Token-based resolution, mutual exclusion is achieved by passing a special message between processes, which we call a token, only the process that obtains the token grants access to the shared resource, and the token is passed to the next process after the access is completed, and if a process obtains a token and does not need to access the shared resource, Then it simply passes the token to the next process.
2) license-based solutions, a process that accesses shared resources requires first obtaining permission from other processes.
Let's learn about the centralized algorithm: As the name implies, we can imagine that there is a collaborator in the system unified management of access to shared resources, only to obtain the permission of the collaborators to access the shared resources, access after completion of the need to know the collaborators in a timely manner.
1, in this system, 3 acts as a collaborator to unify the management of access to shared resources. At some point, process 1 initiates a request for access to the shared resource to collaborator 3, at which point the collaborator detects that no other process is accessing the shared resource and gives the process 1 reply OK to allow access.
Figure 1
2, while process 1 accesses the shared resource, Process 2 also initiates an access request to collaborator 3, then the collaborator 3 will put the request of process 1 into the request queue because it does not receive the release message at the end of the 2 access. There are two ways to deny 2 access: One is to not return any messages to 2, At this point 2 has not received the reply from the collaborators by default is denied access, then process 2 is suspended, and the second is to process 2 explicitly return denied access to the message, then process 2 will constantly poll whether to meet the access criteria.
Figure 2
3, Process 1, after accessing the shared resource, gives the collaborator an end-of-access release message, at which time the collaborator fetches the first process from the queue and replies to the Allow message, where the process acquires permissions to access the shared resource.
From its implementation principle we can see that the algorithm is still relatively fair, because the order of permission requests and receive their order is consistent, also very simple, each access request only requires 3 message delivery is request, permission, release;
But its shortcomings are also obvious, when the system is very large, the collaborators will become the bottleneck;
In the whole system, the collaborators have to maintain a certain frequency of detection mechanism between the other processes in order to quickly deal with a single point of failure of the collaborator, if a process first detects that the collaborator has failed, then immediately follow the pre-set leader election algorithm to initiate a new collaborator election process, Finally, a new collaborator is selected to continue to manage access to the system's shared resources.
Centralized algorithm---Distributed system mutual exclusion algorithm