Basic signal of PHP process communication, php process Communication

Source: Internet
Author: User

Basic signal of PHP process communication, php process Communication

Use signal communication. You can use kill-l to view the signal type of the current system.
The detailed meaning of each signal, please refer to my article: http://www.bkjia.com/article/106040.htm
When using the signal, you can use php -- version to view the current PHP version. You have decided which method to use for inter-process signal communication.

[root@roverliang ipc]# php --versionPHP 5.6.24 (cli) (built: Aug 15 2016 19:14:02)Copyright (c) 1997-2016 The PHP GroupZend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

The pcntl_signal_dispatch function requires the PHP version (PHP 5> = 5.3.0, PHP 7)

If the PHP version is earlier than, some large companies may be earlier than this version. In this case, declare (ticks = 1) is used, which means that each low-level command is executed,
The system checks whether the signal is displayed. Detailed introduction to view http://www.bkjia.com/article/48340.htm

The explanation on the official website is as follows: Tick (clock cycle) is an event that occurs every time the interpreter executes N low-level statements that can be timed in the declare code segment. The value of N is specified by ticks = N in the directive section of declare.

So what is a low-level statement: the following code:

  for ($i = 0; $i < 3; $i++) {    echo $i.PHP_EOL;  }

The for loop contains three low-level commands. $ I is output each time. It will check whether a registered event has occurred. As you can imagine, the efficiency is relatively low. Therefore, if you detect that your PHP is greater than or equal to 5.3. Pcntl_singal_dispath is used for signal delivery.

The main process registers some signal processing functions at startup.

/*** @ Param $ signal */function signalHandal ($ signal) {switch ($ signal) {case SIGINT: // do something break; case SIGHUP: // do something break; default: // do something break ;}}

Then bind the signal processor with the signal processing function:

// Install different signal processors pcntl_signal (SIGINT, 'signalhandal'); pcntl_signal (SIGHUP, 'signalhandal'); pcntl_signal (SIGUSR1, 'signalhanda') based on different signals ');

If the sub-process monitors the signal, call the pre-installed signal processing function.

// Assign a signal. Pcntl_signal_dispatch ($ signal );

Let's sort out the following ideas:
1. define functions for event processing when a signal occurs
2. Bind the signal to the signal processing function, which is called the signal installation.
3. When the signal is monitored or distributed, the installed signal is called.

To understand the above signal concept, let's look at a demo:

<? Php $ parentpid = posix_getpid (); echo "parent progress pid: {$ parentpid} \ n"; // define a signal processing function sighandler ($ signal) {if ($ signal = SIGINT) {$ pid = getmypid (); exit ("{$ pid} process, Killed! ". PHP_EOL) ;}}// php version <5.3. Each time a low-level command is executed, check whether the signal is displayed. High efficiency loss. // Declare (ticks = 1); $ child_list = []; // register a signal processor. When this signal is sent, call the defined function pcntl_signal (SIGINT, 'sighandler'); for ($ I = 0; $ I <3; $ I ++) {$ pid = pcntl_fork (); if ($ pid = 0) {// sub-process while (true) {// call the installed signal processor, to detect whether a new signal is waiting for dispatching pcntl_signal_dispatch (); echo "I am child :". getmypid (). "and I am running! ". PHP_EOL; sleep (rand (1, 3) ;}} elseif ($ pid> 0) {$ child_list [] = $ pid;} else {die ('fork fail! '. PHP_EOL) ;}} sleep (5); foreach ($ child_list as $ key =>$ pid) {posix_kill ($ pid, SIGINT);} sleep (2 ); echo "{$ parentpid} parent is end ". PHP_EOL;

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.