Home > Backend Development > PHP Problem > Does PHP have a lock?

Does PHP have a lock?

王林
Release: 2023-02-26 07:26:01
Original
4067 people have browsed it

Does PHP have a lock?

File lock

This type of lock is relatively common. For example, after mysql and php-fpm are started, there will be a pid file recording the process ID. This file is the file lock.

This lock can prevent a process from running repeatedly. For example, when using crontab, one task is limited to be executed every minute, but this process may run for more than one minute. If the process lock is not used to resolve the conflict, the two processes will be together. There will be problems with execution.

Another advantage of using PID file lock is that it is convenient for the process to send stop or restart signals to itself. For example, the command to restart php-fpm is:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
Copy after login

Send the USR2 signal to the process recorded in the pid file. The signal belongs to process communication and will be discussed in another chapter.

Mutex locks and read-write locks

Mutex in the sync module:

Mutex is a compound word, mutual exclusion. Use pecl to install the sync module, pecl install sync. SyncMutex in the documentation has only two methods, lock and unlock.

Read-write lock in sync module:

The method of SyncReaderWriter is similar, readlock, readunlock, writelock, writeunlock can appear in pairs.

Event in the sync module:

It feels more like Cond in golang, wait() blocks, and fire() wakes up a process blocked by Event. There is a good article introducing Cond. It can be seen that Cond is a fixed usage of lock, and the same is true for SyncEvent. The examples in the PHP documentation show that the fire() method seems to be used in web applications.

Semaphores in the sync module:

The SyncSemaphore document shows that the difference between it and Mutex is that Semaphore can be used by multiple processes (or threads) at a time Get, and Mutex can only be got by one at a time. So in the constructor of SyncSemaphore, there is a parameter that specifies how many processes the semaphore can be obtained by.

Recommended tutorial: PHP video tutorial

The above is the detailed content of Does PHP have a lock?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template