PHP Semaphore Basic Usage example detailed, PHP signal example detailed _php tutorial

Source: Internet
Author: User
Tags php error

PHP Semaphore Basic Usage example detailed, PHP signal example detailed


The basic usage of PHP semaphore is described in this paper. Share to everyone for your reference, as follows:

Some theoretical foundations:

Semaphore: Also known as semaphore, Semaphore is used to solve the process (thread synchronization problem), similar to a lock, access to the lock before accessing (not get to wait), after access to release the lock.
Critical Resource : A resource that only one process is allowed to access at a time.
Critical Section: the code that accesses critical resources in each process is called a critical section
Process Mutex : two or more processes cannot enter a critical region about the same set of shared variables at the same time, that is, one process is accessing the critical resource, and another process must wait to access it.
process synchronization mainly studies how to determine the order of execution between several processes and avoid the problem of data competition, that is, how to enable multiple processes to work well together

Example: (from Baidu Encyclopedia)

Take the operation of a car park as an example. For simplicity, suppose that there are only three parking spaces in the car park and three spaces are empty at the beginning. At the same time, if five vehicles were to come, the janitor would allow three of them to enter directly, then put down the car block, the rest of the car must wait at the entrance, and then the car will have to wait at the entrance. At this time, there is a car left the parking lot, the janitor learned, open the car block, put into the outside of a car, if left two, then can put two, so reciprocating.

In this parking system, parking spaces are public resources, and each car is like a thread, and the gatekeeper acts as the semaphore.

$key =ftok (__file__, ' t ');/** * Gets a semaphore resource int $key [, int $max _acquire = 1 [, int $perm = 0666 [, int $auto _release = 1]]]< c0/> $max _acquire: Up to how many processes can get the signal at the same time $perm: permissions default 0666 $auto _release: Whether to automatically release semaphores */$sem _id=sem_get ($key); #获取信号sem_acquire ( $seg _id);//do something here is an atomic operation//release semaphore sem_release ($seg _id);//Remove the secondary signal from the system Sem_remove ($sem _id);//problems that may arise $fp = Sem_ Get (Fileinode __dir__), Sem_acquire ($FP), $fp 2 = Sem_get (Fileinode (__dir__), 1)), Sem_acquire ($fp 2);

Implementation of a read-write semaphore in PHP:

Class Rw_semaphore {Const READ_ACCESS = 0;    Const WRITE_ACCESS = 1;  /** * @access Private * @var Resource-mutex semaphore */private $mutex;  /** * @access Private * @var resource-read/write semaphore */private $resource;  /** * @access Private * @var int */Private $writers = 0;  /** * @access Private * @var int */Private $readers = 0; /** * Default Constructor * * Initialize the read/write semaphore * * Public Function __construct () {$mutex _k    EY = Ftok ('/home/cyrus/development/php/sysvipc/rw_semaphore.php ', ' m ');        $resource _key = Ftok ('/home/cyrus/development/php/sysvipc/rw_semaphore.php ', ' R ');    $this->mutex = Sem_get ($mutex _key, 1);      $this->resource = Sem_get ($resource _key, 1); }/** * destructor * Remove the read/write semaphore */Public Function __destruct () {Sem_remove ($this-&G    T;mutex);  Sem_remove ($this->resource);  }/** * Request acess to the resource * * @param int $mode * @return void */Private Function request_access ($access _type = self::read_access) {if ($access _type = = Self::w      rite_access) {sem_acquire ($this->mutex);      /* Update the writers counter */$this->writers++;            Sem_release ($this->mutex);    Sem_acquire ($this->resource);            } else {Sem_acquire ($this->mutex);                if ($this->writers > 0 | | $this->readers = = 0) {sem_release ($this->mutex);                Sem_acquire ($this->resource);              Sem_acquire ($this->mutex);      }/* Update the readers counter */$this->readers++;    Sem_release ($this->mutex);      }} Private Function Request_release ($access _type = self::read_access) {if ($access _type = = self::write_access) {      Sem_acquire ($this->mutex);      /* Update the writers counter */$this->writers--;      Sem_release ($this->mutex);    Sem_release ($this->resource);     } else { Sem_acquire ($this->mutex);      /* Update the readers counter */$this->readers--;      if ($this->readers = = 0) sem_release ($this->resource);    Sem_release ($this->mutex); }}/** * Request read access to the resource * * @return void */Public Function read_access () {$this->re Quest_access (self::read_access); }/** * Release read access to the resource * * @return void */Public Function read_release () {$this->requ Est_release (self::read_access); }/** * Request write access to the resource * @return void */Public Function write_access () {$this->req Uest_access (self::write_access); }/** * Release write access to the resource * @return void */Public Function write_release () {$this->re Quest_release (self::write_access); }}

Shared memory + signal for atomic operation

The above source PHP manual sem_get function Comment

More about PHP related content readers can view the topic: "PHP Basic Grammar Introductory Tutorial", "PHP error and Exception handling method summary", "PHP Programming algorithm Summary" and "PHP object-oriented Programming introduction Tutorial"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Get increment sequence ID by system semaphore Lock method under PHP
    • PHP Extender Implementation Daemon
    • PHP Daemon Instance
    • PHP Open Multi-process method
    • PHP Process Synchronization Code Instance
    • PHP Advanced Programming Example: Writing Daemons

http://www.bkjia.com/PHPjc/1099075.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099075.html techarticle PHP semaphore Basic usage examples, examples of PHP signal example of the basic usage of PHP semaphore. Share to everyone for your reference, as follows: Some theoretical basis: letter ...

  • 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.