In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the PHP multi-process programming related functions, the article is very detailed, has a certain reference value, interested friends must read it!
PHP has a set of process control functions (compile time requires-enable-pcntl and posix extensions) to enable php to create child processes, execute programs using exec functions, process signals, and so on, just like c.
Using the real multi-process running mode of PHP, it is suitable for data collection, mail group sending, data source updating, tcp server and so on.
PHP has a set of process control functions (compiler time requires-enable-pcntl and posix extensions), which enables php to create child processes, use exec functions to execute programs, process signals, and so on, just like c in the * nix system. PCNTL uses ticks as the signal processing mechanism (signal handle callback mechanism) to minimize the load when handling asynchronous events. What is ticks? Tick is an event that occurs every time the interpreter executes N low-level statements in a code snippet, which needs to be specified through declare.
Commonly used PCNTL functions
1. Pcntl_alarm (int $seconds)
Set a counter that sends SIGALRM signals after $secondsseconds
2. Pcntl_signal (int $signo, callback $handler [, bool $restart_syscalls])
Set a callback function for $signo to handle the signal. Here is an example of sending a SIGALRM signal every 5 seconds, getting it by the signal_handler function, and then printing a "Caught SIGALRM":
3. Pcntl_exec (string $path [, array $args [, array $envs]])
Executes the specified program in the current process space, similar to the exec family function in c. The so-called current space, that is, the code loaded into the specified program overwrites the space of the current process, and the process ends after the program is executed.
4. Pcntl_fork (void)
Create a child process for the current process, and run the parent process first, returning the PID of the child process, which must be greater than zero. In the code of the parent process, you can use pcntl_wait (& $status) to pause the parent process until its child process has a return value. Note: the blocking of the parent process also blocks the child process. However, the end of the parent process does not affect the operation of the child process.
When the parent process is finished, it then runs the child process, which starts with the statement that executed pcntl_fork () (including this function), but at this time it returns zero (which means it is a child process). It is best to have an exit statement in the code block of the child process, which ends immediately after the child process is executed. Otherwise, it will start executing parts of the script all over again.
Pay attention to two points:
It is better for a child process to have an exit; statement to prevent unnecessary errors
It is best not to have other statements between pcntl_fork, such as:
5. Pcntl_wait (int & $status [, int $options])
Blocks the current process until a child of the current process exits or receives a signal to end the current process. Use $status to return the status code of the child process, and you can specify a second parameter to indicate whether to call in blocking state:
If called in blocking mode, the function returns the pid of the child process. If there is no child process, the return value is-1.
Called in a non-blocking manner, the function can also return 0 when a child process is running but does not end.
6. Pcntl_waitpid (int $pid, int & $status [, int $options])
The function is the same as pcntl_wait, except that waitpid is a child process waiting for the specified pid. When pid is-1, pcntl_waitpid is the same as pcntl_wait. The state information of the child process is stored in $status in the pcntl_wait and pcntl_waitpid functions, which can be used in functions such as pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, and pcntl_waitpid.
For example:
The child process sleep 2 seconds after outputting the words child process, etc., while the parent process blocks until the child process exits before continuing to run.
7. Pcntl_getpriority ([int $pid [int $process_identifier]])
Get the priority of the process, that is, the nice value, which defaults to 0. In the linux of my test environment (CentOS release 5.2 (Final)), the priority is-20 to 19, which is the highest priority and the lowest 19. (from-20 to 20 in the manual).
8. Pcntl_setpriority (int $priority [, int $pid [, int $process_identifier]])
Set the priority of the process.
9. Posix_kill
Can send signals to the process
10. Pcntl_singal
The callback function used to set the signal
How does the child process know the exit of the parent process when the parent process exits
When the parent process exits, the child process can generally know the message that the parent process has exited through the following two relatively simple methods:
When the parent process exits, an INIT process will adopt the child process. The process number of this INIT process is 1, so the child process can get the pid of the current parent process by using getppid (). If 1 is returned, the parent process has become an INIT process, and the original process has been launched.
Use the kill function to send a null signal (kill (pid, 0)) to the original parent process. Use this method to check the existence of a process without actually sending a signal. So, if this function returns-1, the parent process has exited.
In addition to the above two methods, there are some more complex methods in implementation, such as establishing pipes or socket for constant monitoring, and so on.
An example of PHP multi-process data collection
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.