Peterson's algorithm: a visual analysis

Some basics before learning algorithms

1. What does PeterSon algorithm do?

  • The PeterSon algorithm is a software-based solution to the critical section problem .
  • In addition, the solutions to all critical section problems must meet three requirements: mutual exclusion, advancement, and limited waiting .
  • The PeterSon algorithm is suitable for two processes to execute alternately in the critical area and the remaining area.

2. Implementation of PeterSon algorithm (you can ignore it first)

First of all, the Peterson algorithm needs to be in two processes P 1 P_1P1And P 2 P_2P2Two data items are shared between :

int turn; // 令牌,值为1或2,表示哪个进程可以进入临界区
boolean flag[2];//登记簿,数组,flag[i]==true表示进程Pi想要进入临界区

In PeterSon's algorithm, P i P_iPiStructure

do{
    
    
	flag[i] = TRUE;
	turn = j;
	while(flag[j] && turn == j);
	//临界区
	flag[i] = FALSE;
	//剩余区
}while(TRUE);

In PeterSon's algorithm, P j P_jPjStructure

do{
    
    
	flag[j] = TRUE;
	turn = i;
	while(flag[i] && turn == i);
	//临界区
	flag[i] = FALSE;
	//剩余区
}while(TRUE);

Prove that the algorithm meets the requirements of mutual exclusion, advancement, and limited waiting

1. First of all, we consider the case of using only tokens without registering

Insert picture description here
In an ideal situation , suppose that the process P i P_iPiFirst get the token, just process P i P_iPiAlso enter the critical section, run turn = j after the execution, give the token to P j P_jPj. So it goes back and forth.

However , this algorithm does not conform to the forward principle of the above three principles . Consider a situation where the process P i P_iPi(Left) Obtained a token, namely P i P_iPiCan enter the critical zone. But it does not want to enter (does not execute code), and keeps holding the token and does not give the process P j P_jPj, This will cause when there is no process execution in the critical section, P j P_jPjWant to enter the critical zone but not allowed.

This kind of problem can be compared to tenants A and B, there is only one public toilet (critical area). There is only one key (token) for two people. When A has the key, he can open the toilet door, and give the key to B after processing. However, sometimes A gets the key but does not want to go to the toilet. This is because B wants to go but does not have the key.

2. Let us consider the case of only the register

Insert picture description here
Ideally , if the process P i P_iPiIf you want to apply for entry into the critical zone, make your register flag[i] = true. This is the query whether flag[j] = true, if the process P j P_jPjIf the register is true, it means that the process P j P_jPjIt is still executing in the critical section. Process P i P_iPiWait until flag[j] = false, process P i P_iPiEnter the critical area and change your register to false after executing the code. So it goes back and forth.

However , if the process P i P_iPiAnd process P j P_jPjAt the same time apply for entering the critical region, then there will be a situation where flag[i] = true and flag[j] = true, that is, the two processes "humbly" each other and fall into an infinite loop. There is no process execution in the critical region, but there is The situation where the process wants to enter the critical zone does not conform to the principle of progress .

It was the same two tenants. After the two found that the keys were useless, they decided to put two signs (registration book) on the toilet door. If the signs are facing outwards (indicating that the corresponding person is inside), if the signs are facing outwards, it means there is no one. . In most of the time, this program can be executed normally. If A wants to go to the toilet, he will first put his sign facing up, and then look at the brand of B. If B is inside, wait. After B comes out, he will turn over his sign. A sees the sign of B facing out. Then go to the toilet.

Until one day, when A and B wanted to go to the toilet at the same time, they both turned their signs to face outwards. The two looked at each other and were humility to each other, and both wanted the other to go to the toilet first. In this cycle, the two of them could not go to the toilet.

3. The scheme of simultaneous existence of token and register (ie Peterson algorithm)

Insert picture description here

Proof of mutual exclusion

Take the process P i P_iPiFor example, if the process P i P_iPiIf you want to enter the critical section, only when flag[j] = false or turn = i, the process P i P_iPiWas allowed to enter. When the process P i P_iPiAfter entering the critical section, flag[i] = true, the process P j P_jPjUnable to enter the critical section.

In another case, the process P i P_iPiAnd P j P_jPjAt the same time, if you want to enter the critical region, both flag[i] and flag[j] are true, but turn can only be i or j, which is a certain value. In other words, one of the two while must be in an infinite loop, the other is not executed, and the process that the while does not execute can enter the critical section.

Therefore, in any case, at most only one process executes in the critical section to meet the mutual exclusion requirements .

Proof of Progress

In the case of two processes, it is proved that the two processes will not fall into an infinite loop and wait for each other. And there is only one way to fall into an infinite loop, that is, two while in an infinite loop at the same time. But this is impossible. The variable of turn cannot be i and j at the same time, that is, there is always a process that can enter the critical section to meet the requirements of progress .

Proof of limited waiting

That is to say, if the process P i P_iPiApply to enter the critical section, then the process P j P_jPjYou can’t come out of the critical zone and then go in and out, go in, go out and go in again.
Proof: Assume that the process P i P_iPiWhile in loop, process P j P_jPjExecute in the critical section, when the process P j P_jPjAfter coming out, flag[j] = false. At the same time, the process P i P_iPiEnter the critical zone at the speed of light without giving the process P j P_jPjThe opportunity to go in again meets the limited waiting requirements .

Tenants A and B decided to use the key and the brand at the same time. Different from using only the brand, they would give each other the key each time before going to the toilet. And only when the key is not in his hand and his sign is facing outwards can he go to the toilet.

Consider the first situation. If A wants to go to the toilet, and he does not have a key, and B is not in the toilet (B's brand is facing outward), he can go to the toilet. If A wants to go to the toilet and the key is in his hand, then he will give the key to B. As long as B is not in the toilet, A can also enter, which solves the problem of having only the key.

Consider the second situation. A and B both want to go to the toilet at the same time, and they both turned their signs over. When they were about to give in to each other, they found that the key was either in A’s hand or B’s hand, so there was no key. The party can enter the toilet.

Guess you like

Origin blog.csdn.net/qq_41882686/article/details/112614568