Home>Article>Backend Development> Briefly understand the semaphore of PHP process communication

Briefly understand the semaphore of PHP process communication

WBOY
WBOY forward
2022-09-02 16:57:22 2029browse

(Recommended tutorial:PHP video tutorial)

Common process communication methods

Some theoretical basis

  • Critical resources: Resources that are only allowed to be accessed by one process at a time.
  • Critical section: The code that accesses critical resources in each process is called the critical section

The so-called critical section (also called critical section) is the code that accesses and operates shared data part.

Processes are mutually exclusive: Two or more processes cannot enter the critical area on the same set of shared variables at the same time, that is, one process is accessing critical resources, and the other process must wait if it wants to access.

Process synchronization: It mainly studies how to determine the execution order between several processes and avoid data competition issues, that is, how to make multiple processes run well together

The so-called synchronization, That is, concurrent processes/threads may need to wait for each other and exchange messages at some key points. This mutually restricted waiting and exchange of information is called process/thread synchronization.

To give an example of synchronization in life, you are hungry and want to eat. You ask your mother to cook early. After hearing this, your mother starts cooking, but before your mother finishes cooking, you must block and wait. , after mom finishes cooking, she will naturally notify you, and then you can start eating.

Note that synchronization and mutual exclusion are two different concepts:

Synchronization is like: "Operation A should be executed before operation B", "Operation C must be executed before operation A and operation B "It can only be executed after everything is completed" etc.;

Mutual exclusion is like: "Operation A and operation B cannot be executed at the same time";

system V semaphore

Semaphore Purpose: Mainly used for multi-process or multi-thread access control to public resource objects. Used to solve the problem of multi-process (multi-thread synchronization), similar to a lock, acquire the lock before accessing (wait if not acquired), and release the lock after accessing.

Multiple processes/multithreads are generally executed concurrently. If access to public resources is not synchronized, it is easy to cause data damage

The semaphore is actually an integer counter, mainly used for Implement mutual exclusion and synchronization between processes instead of caching data for inter-process communication.

The semaphore represents the number of resources. There are two atomic operations to control the semaphore:

One is the P operation. This operation will subtract -1 from the semaphore. After subtraction, if If the semaphore = 0 after subtraction, it indicates that there are still resources available and the process can continue to execute normally.

The other is the V operation. This operation will add 1 to the semaphore. After the addition, if the semaphore 0, it indicates that there is currently no blocked process;

P operation is used before entering the shared resource, and V operation is used after leaving the shared resource. These two operations are Must appear in pairs.

For example, the semaphores of 2 resources are equivalent to 2 train tracks. The PV operation process is as follows:

A train enters the track , equivalent to the P operation of the semaphore, resource -1, so there is only one track left

Then another train occupies another track, which is P operation, resource -1

At this point the traffic light turns red because no track is available and the third train must wait

The first train leaves the track, which is equivalent to V operation. At this time, the track resource is 1 and the traffic light turns green

The third train finds that the traffic light turns green, So entering the train track, the track resources are exhausted to 0, so the traffic light turns to red

In this train track system, the track is a public resource. Each train is like a thread, and the traffic light plays the role of a semaphore. The semaphore can realize the mutual exclusion operation of the lock and can also realize the process/thread synchronization

Semaphore type

1) Binary semaphore (also called binary semaphore)

At this time, the initial value of the semaphore can only be 0 and 1. (Binary semaphores can implement mutex lock operations)

2) General/counting semaphores

At this time, the initial value of the semaphore can be any non-negative number. Obviously, it contains a binary semaphore. The train track example given above can be implemented using a counting semaphore. Generally, the difference between a counting semaphore and a lock is that it allows multiple threads/processes (the number of threads is defined by the initial value of the counting semaphore) to operate public resources at the same time

Generally only when developing multiple processes, you may encounter scenarios that require the use of semaphores. There are almost no scenarios where semaphores are used in phper. Even if there are multiple processes operating on public resources, flock file locks are mostly used. Mutually exclusive operation

php simulates multi-process operation of public resources


      

Assistant in writing a shell script

#!/bin/bash for a in {1..1000} do (php demo1.php) b=`cat num.txt` if [ $b != 2000 ] then echo -e "错误$b" fi done

Logically speaking, the variable$xis written last The value of the input file should be 2000, but unfortunately, this is not the case. Let’s execute the above script:

After running it 1000 times, we found that the variable $x appeared. The value result is 1000 8 times. Although the probability of error is relatively small, it is intolerable in the computer.

Why does this happen? We know that in order to achieve the illusion of multiple programs running at the same time in a single-core CPU system, the operating system usually uses time slice scheduling. When one process uses up its time slice, it switches to the next process. Run, and in our high-level language not every line of code is atomic. For example,x = (int)file_get_contents($file)This line of code is not possible for us The segmentation is atomic, but after the compiler compiles it into assembly code [machine instructions], it may be implemented by multiple instructions. This will cause problems if only half of the instructions are executed until the time slice allocated by the process is used up or interrupted by other processes. , may cause data damage, resulting in errors in the final calculation result

Use php to encapsulate the system v semaphore set function


      

After adding the semaphore, it must be guaranteed to be 100% 2000. Absolutely no other values will appear.

(Recommended tutorial:PHP video tutorial)

The above is the detailed content of Briefly understand the semaphore of PHP process communication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete