In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Software completion method
Some marks are set and reflected in the entry zone to indicate whether there is a process in the critical zone. If there is already a process in the critical zone, it stops waiting after reincarnation reflection in the entry zone, and the zone correction mark is added after the process separates the critical zone.
1) algorithm 1: single mark method.
The algorithm sets a common integer variable turn, which is used to guide the process number that is allowed to enter the critical area, that is, if turn=0, the P0 process is allowed to enter the critical area. The algorithm ensures that only one process is allowed to enter the critical area at a time. But the two processes must enter the critical zone, if one process no longer enters the critical zone, then the other process will also enter the critical zone (violation of "leisure concession"), so it is easy to form the inadequacy of capital application.
/ / P0 process while (turntables 0); critical section; turn=1; remainder section; / / P1 process while (turntables 1); / / entry area critical section; / / critical area turn= 0; / / join area remainder section; / / residual area 2) algorithm 2: double-label introspection first.
The basic idea of the algorithm is to check whether the critical capital is being visited before each process visits the critical zone capital, and if it is being visited, the process has to wait; otherwise, the process enters its own critical area. For this reason, a data flag [I] is set, for example, the value of the I element is FALSE, which shows that the Pi process does not enter the critical region, and the value is TRUE, which indicates that the Pi process enters the critical region.
/ / Pi process while (Pi [j]); / / ① flag [I] = TRUE; / / ③ critical section; flag [I] = FALSE; remainder section; / / Pj process while [I]; / ② entry zone flag [j] = TRUE; / / ④ entry zone critical section; / / critical zone flag [j] = FALSE; / / join zone remainder section; / / residual area
Advantages: do not need to enter the melon generation, can continue to use; defects: Pi and Pj can enter the critical area at the same time. When you list ①②③④ performance in sequence, you will enter the critical area at the same time (in violation of "busy waiting"). That is, there is a period of time after reflecting on each other's flag and before switching my flag, and the consequences are all reflected on the process. The achievement here is that the introspection and correction operation cannot be stopped at once.
3) algorithm 3: introspection after double marking.
The second algorithm is to first detect the other process morphological markers, and then put the own mark, because in the detection and placement can pull out the detection operation of the arrival of another process, will form two processes after discrimination detection, and enter the critical area at the same time. For this reason, the algorithm first sets myself to be marked as TRUE, and then detects the other party's morphological marker. If the other party is marked as TURE, the process waits; otherwise, it enters the critical area.
/ / Pi process flag [I] = TRUE; while (Pj [j]); critical section; flag [I] = FLASE; remainder section; / / Pj process flag [j] = TRUE; / / entry zone while (entry [I]); / / entry zone critical section; / / critical zone flag [j] = FLASE; / / join zone remainder section; / residual zone
When the two processes almost want to enter the critical area at the same time, they set their own tag value flag to TRUE, and at the same time check each other's shape (performing the while sentence), and find that the other side also wants to enter the critical area, so they give way to each other, resulting in no one can enter the critical area, thus leading to "hunger" scene.
4) algorithm 4: Peterson's Algorithm.
In order to prevent the two processes from waiting for a certain time to enter the critical area, the variable turn is set to guide the process number that is not allowed to enter the critical area. Each process first sets its own mark and then sets the turn tag, which is not allowed for the other process. At this time, another process morphological marker and no entry marker are detected at the same time, so as to ensure that when two processes request to enter the critical zone at the same time, only one process is allowed to enter the critical area.
/ / Pi process flag [I] = TURE; turn=j; while (FLASE; remainder section; [j] & & turn==j); critical section; flag [I] = FLASE; remainder section; / / Pj process flag [j] = TRUE;turn=i; / / entry zone while (entry [I] & & turn==i); / / entry zone critical section; / / critical zone flag [j] = FLASE; / / join zone remainder section; / residual area
The basic idea of this algorithm is the combination of algorithm one and algorithm three. Flag is used to deal with mutually exclusive visits to critical capital, while turn is used to deal with "hunger" scenes.
Hardware completion method
A detailed understanding of the hardware completion in this section is very helpful to the previous study of the amount of signals. The computer provides special hardware instructions to stop detecting and modifying the contents of one word, or to stop communicating with the contents of two words, and so on. The primary method or meta-method to complete the results of the critical stage through hardware support.
1) infix barrier method
When a process is using the processor to perform its critical area code, the most complex way to prevent other processes from entering its critical area to visit is to stop all infixes, or barrier affixes, closed affixes. Since CPU only causes process switching when the infix occurs, such a barrier infix can guarantee that the critical section code will be executed smoothly in the later operation process, thus ensuring the exact completion of mutual exclusion, and then performing the open infix. Its typical form is:
...
Guanzhong affix
Critical area
Open infix
...
This approach limits the ability of the disposal machine to perform the procedure, so the effectiveness of performance will be significantly reduced. For the kernel, it is convenient for it to perform several instructions to update variables or lists, but it is unwise to give the right to close the suffix to the user. If a process shuts off the suffix and no longer opens the infix, then the bits and pieces can be terminated.
2) hardware instruction method
TestAndSet instruction: this instruction is an atomic operation, that is, it is not allowed to be suffixed when performing the code. Its function is to set the specified tag to true after reading it. The function of the instruction is described as follows:
Boolean TestAndSet (boolean * lock) {boolean old; old = * lock; * lock=true; return old;}
It can be considered that each critical capital sets a shared Boolean variable lock, which represents two forms of capital: true performance is being occupied, and the initial value is false. Before the process visits the critical capital, apply TestAndSet introspection and correction to mark lock; if there is a process in the critical area, then reflect repeatedly until the process is added. The algorithm for applying this instruction to complete the process mutual exclusion is described as follows:
While TestAndSet (& 1 ock); / / critical section code snippet of the process; other code of the lock=false; / / procedure
Swap instruction: the function of this instruction is to communicate two bytes of content. Its function is described as follows.
Swap (boolean * a, boolean * b) {boolean temp; Temp=*a; * a = * b; * b = temp;}
Note: the above description of TestAndSet and Swap instructions is only functional, not software completion. In fact, they are done directly by hardware logic and will not be suffixed.
A shared Boolean variable lock should be set for each critical capital, and the initial value should be false;. In each process, a partial Boolean variable key should be set to exchange information with lock. Before entering the critical area, we first use Swap instructions to communicate the contents of lock and key, and then reflect on the form of key; when there is a process in the critical area, repeatedly communicate and reflect on the process until the process is added. The algorithm for using Swap instructions to complete the process mutual exclusion is described as follows:
Key=true; while (keyword checking false) Swap (& lock, & key); / / critical section code snippet of the procedure; other code of the lock=false; / / procedure
The advantages of the hardware approach: it can be applied to an arbitrary number of processes, whether it is a single processor or multiple processors; it is complex and easy to verify its accuracy. There are multiple critical regions in the supporting process, and only one Boolean variable needs to be set up for each critical region.
The defect of the hardware method: when the process waits to enter the critical area, it consumes the disposal machine time and cannot complete the concession waiting. Randomly select one from the waiting process to enter the critical area, some processes can not be selected continuously, thus leading to the scene of "hunger".
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.