PHP process Communication basics-semaphores and shared memory communication, php process

Source: Internet
Author: User

PHP process Communication basics-semaphores and shared memory communication, php process

It is not determined who executes the tasks first. This depends on the Process Scheduling Algorithm of the kernel, which is complicated. As a result, multiple processes may simultaneously access the shared memory at the same time, resulting in unexpected errors. The semaphore name is puzzling, but it is easy to understand according to its original English meaning.

Semaphore English [sem %f %:( r)] vt. Signal, flag;

Similar to the role of a commander.

Next we will look at the use of the next pseudo-code semaphore.

1. Create a unique semaphore identifier

$ftok = ftok(__FILE__, 'a');

2. Create a semaphore resource ID

$sem_resouce_id = sem_get($ftok);

3. Receive semaphores

sem_acqure($sem_resource_id);

4. Release semaphores

sem_release($sem_resource_id);

5. Destroy semaphores

sem_remove($sem_resource_id);

An inelegant example makes it easy for us to understand the usage of this semaphore in our daily life. After understanding it, we can apply it to our programming field.
A company only has one bathroom. When someone goes to the bathroom, they need to get a lock (semaphore), indicating that the bathroom is in use. The Code is as follows:

sem_acqure($sem_resource_id);

After completing the restroom, the employee needs to open the lock and release the lock (semaphore), indicating that the lock can be used by others now. The Code is as follows:

sem_release($sem_resource_id);

With a simple lock, we can know whether the current restroom (shared memory) can be used. This example is not elegant but illustrates the problem. This blog is also a tasteful blog. It is really not easy .... The following is the sample code:

<? Php // create a shared memory area $ shm_key = ftok (_ FILE __, 'A'); $ shm_id = shm_attach ($ shm_key, 1024,075 5 ); // var_dump ($ shm_id); die (); resource (4) of type (sysvshm) const 1__key = 1; $ child_list = []; // Add the semaphore $ sem_id = ftok (_ FILE __, 'B'); $ signal = sem_get ($ sem_id); // $ signal resource (5) of type (sysvsem) for ($ I = 0; $ I <3; $ I ++) {$ pid = pcntl_fork (); if ($ pid =-1) {exit ("Fork fail! ". PHP_EOL);} elseif ($ pid = 0) {// obtain the semaphores sem_acquire ($ signal); if (shm_has_var ($ shm_id, 1__key )) {$ count = shm_get_var ($ shm_id, 1__key); $ count ++; // simulate business processing $ sec = rand (1, 3); sleep ($ sec ); shm_put_var ($ shm_id, 1__key, $ count);} else {$ count = 0; $ sec = rand (1, 3); sleep ($ sec); shm_put_var ($ shm_id, performance_key, $ count);} echo "child process :". getmypid (). "is writing! Now count is: $ count ". PHP_EOL; // release the semaphores sem_release ($ signal); exit ("child process ". getmypid (). "end ". PHP_EOL);} else {$ child_list [] = $ pid;} while (count ($ child_list)> 0) {foreach ($ child_list as $ key => $ pid) {$ status = pcntl_waitpid ($ pid, $ status); if ($ status> 0 | $ status =-1) {unset ($ child_list [$ key]) ;}} sleep (1) ;}$ count = shm_get_var ($ shm_id, 1__key); echo "$ count ". PHP_EOL; // destroy semaphores sem_remove ($ signal); shm_remove ($ shm_id); shm_detach ($ shm_id );

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.