The principle of locking and unlocking for PHP

Source: Internet
Author: User
Tags php language
PHP does not have perfect threading support, and even deploying to a threading model-based HTTPD server can have some problems, but even PHP under a multi-process model will inevitably have multiple processes with common access to the same resource. For example, the entire program shared data cache, or because of resource constraints must be queued for specific processes, and for each user to generate a unique identity of a situation. The PHP language itself does not provide process mutexes and locking mechanisms, which makes programming in these situations difficult, and the options currently available are the following:

  • Use MySQL's locking mechanism to achieve mutual exclusion. The disadvantage is that it increases the connection burden on the database server and makes the program dependent on the database service to work properly.

  • Use the file lock mechanism. That is, using the flock function to implement locking and mutual exclusion mechanisms through the file, to simulate the common programming model of the locking primitive working mode. This approach, which previously served as a storage engine for plain text files, became a necessary element in preserving data integrity, and is now quite common in situations where text files are used as cache media. PmWiki should also use this mechanism to remind multiple people to edit a page at the same time. However, the file lock mechanism will call the file Lock feature on the host operating system, so it is necessary to check whether the server operating system provides a perfect and reliable file lock mechanism for the PHP environment.

  • Use the shared memory space count. PHP can use the Shmop_open function to open up a piece of memory space, sharing data between service processes, in order to ensure mutually exclusive secure access to shared data, you can use the Sem_get, Sem_acquire and sem_release This set of functions to implement a shared count locking mechanism. This approach in the background is actually called the system's IPC service to achieve.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.