PHP uses PCNTL and libevent to implement the timer function, first look at the example, Pcntl (PHP thread) explained below.
Copy Code code as follows:
<?php
function newchild ($func _name) {
echo "Enter newchild\n";
$args = Func_get_args ();
unset ($args [0]);
$pid = Pcntl_fork ();
if ($pid = = 0) {
Function_exists ($func _name) and exit (Call_user_func_array ($func _name, $args)) or exit (-1);
else if ($pid = = 1) {
echo "couldn ' t create child process";
} else {
return $pid;
}
}
(PS: ^_^ Good PHP Development Communication group: 256271784, Validation: CSL, if you are interested can join in to discuss)
function On_timer () {
echo "Timer called\n";
}
/**
* @param $func string, function name
* @param $timeouts int, microtimes for time delay
*/
function timer ($func, $timeouts) {
echo "Enter timer\n";
$base = Event_base_new ();
$event = Event_new ();
Event_set ($event, 0, Ev_timeout, $func);
Event_base_set ($event, $base);
Event_add ($event, $timeouts);
Event_base_loop ($base);
}
$pid = newchild ("Timer", "On_timer", 5000000);
if ($pid > 0) {
echo "Master process exit\n";
}
PHP extension PCNTL implementation of "multithreading" (process)
Pcntl and Ticks
Ticks is defined by the Declare (ticks = N) {statement} syntax, declare syntax is currently only acceptable ticks, and the meaning of the ticks = N that he defines is the execution of n lower-level statements in the specified statement block of declare to send To give birth to an event, this event can be registered by Register_tick_function ($function _name).
The signaling mechanism of PCNTL is based on the ticks mechanism. Therefore, when we use the signal-related functions in the Pcntl family function, we need to add the declare (ticks = n) syntax structure to the front.
int pcntl_alarm (int $seconds):
Sends a SIGALRM signal to the process after $seconds seconds, and each call to the Pcntl_alarm method cancels the previously set clock.
void Pcntl_exec (String $path [, array $args [, array $env]]):
Executes a program in the current process space.
$path: Must be a binary executable, or a script file path with valid script header information (#!/usr/local/bin/php).
$args: List of string arguments that will be passed to the program (array form)
$ENVS: Environment variable. Passed to the environment variable to execute the program in the form of an array (key => value).
int pcntl_for k (void):
Creates a child process that is not the same as the parent process (the process number) and the PPID (the parent process number).
Returns the created Subprocess PID when the parent thread executes, returns 0 when the child thread executes, returns 1 in the parent process context when the child process fails, and throws a PHP error.
Understanding the fork here needs to know: Pcntl_fork creates a branch node that is the equivalent of a token, and the child process continues to execute from the tag when the parent process completes, meaning that the code behind the pcntl_fork is executed two times by the parent process and the child process, while two processes are The return values obtained in the row process are different. Therefore, you can detach the parent-child process from executing different code.
int pcntl_getpriority ([int $pid = Getmypid () [, int $process _identifier = prio_process]]):
Gets the priority of the corresponding process for the given $pid, by default the value obtained through Getmypid () is the current process.
$pid: If not specified, the default is the current process.
$process _identifier:prio_pgrp, Prio_user, prio_process one of the three, default prio_process. Where PRIO_PGRP refers to the priority of the process group, prio_user refers to getting the priority of the user process, prio_process refers to obtaining a particular process priority.
Returns the priority of a process, or returns False if an error occurs, the smaller the value, the greater the precedence
BOOL Pcntl_setpriority (int $priority [, int $pid = Getmypid () [, int $process _identifier = prio_process]]:
Sets the priority of the process.
$priority: Priority value, 20 to 20 range, default priority 0. A smaller value indicates a higher priority.
$pid: If not specified, refers to the current process
$process _identifier: $process _identifier of Meaning and pcntl_getpriority.
The setting returns TRUE successfully, and the failure returns FALSE.
BOOL Pcntl_signal_dispatch (void):
A processor that invokes an impending signal that is installed through Pcntl_signal ().
The call successfully returns TRUE, and the failure returns false.
PHP 5.3.3 Join
BOOL Pcntl_signal (int $signo, callback $handler [, bool $restart _syscalls = True]):
Installs a new signal processor $handler for the specified signal $signo.
The last parameter does not understand the meaning.
BOOL Pcntl_sigprocmask (int $how, array $set [, Array & $oldset]):
Add, remove or set the lock signal, the specific behavior depends on the $how parameters
$how: Sig_block is used to increase the signal to the current lock signal, Sig_unblock is used to remove the signal from the current lock signal, and sig_setmask is used to replace the current lock signal with the given list of signals.
$set: List of signals to add, remove, or set.
$oldset: Used to return the old lock signal to the caller.
Successfully returns TRUE, and the failure returns FALSE.
int pcntl_sigtimedwait (array $set [, Array & $siginfo [, int $seconds = 0[, int $nanoseconds = 0]]):
Pcntl_sigtimedwait is actually doing the same thing as Pcntl_sigwaitinfo (), but Pcntl_sigtimedwait has two more enhanced parameters $seconds and $nanoseconds, allowing the scripting There is a limit to the length of stay rather than unlimited waiting.
$set: List of signals to wait on
$siginfo: Information that is used to return the caller to the waiting signal, and the contents of the message see Pcntl_sigwaitinfo
$seconds: Number of seconds to timeout
$nanoseconds: nanosecond Number of timeouts
After success, Pcntl_sigtimedwiat () returns the signal number
int Pcntl_sigwaitinfo (array $set [, Array & $siginfo]):
Suspends execution of the current script until it receives a signal in the $set, if one of the signals is to be reached (for example, locked by Pcntl_sigprocmask) then Pcntl_sigwaitinfo will return immediately
$set: List of waiting signals
$siginfo: The information that is used to return the waiting signal to the caller, which contains the following:
1. All signals have the following three information:
A) Signo: Signal number
b) errno: Error number
c) Code: signal Codes
2. SIGCHLD Signal-specific information
A) Status: Exit Value or Signal
b) Utime: User consumption time
c) Stime: System Consumption time
d) PID: Send process ID
e) UID: The real user ID of the sending process
3. Sigill, SIGFPE, SIGSEGV, Sigbus-owned information
A) Addr: The memory location where the fault is generated
4. Information specific to Sigpoll:
A) Band:band event, meaning unknown
(b) FD: File descriptor
function successfully run return signal number
int pcntl_wait (int & $status [, int *options = 0]):
Suspends the current process until a child process exits or until a signal requires that the current process be terminated or a signal processing function is invoked. If the child process exits when called (commonly known as a zombie process), this function returns immediately and all system resources are freed.
$status is used to hold the state information of the child process, which is generated by the following function: pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, Pcntl_wexitstatus, Pcntl_ Wtermsig, Pcntl_wstopsig.
$options: If your system allows WAIT3 (most BSD class systems), you can provide an optional options parameter, if not provided this parameter, wait will use system call, if the system does not allow WAIT3, provide this parameter will not have any effect , the $options value can be 0 or Wnohang and wuntraced two constants.
The function returns the PID of the exited subprocess, or returns 1 if the error occurs, or if Wnohang is provided as option (WAIT3 is not available) and no valid subprocess returns 0
Zombie Process: Since the parent process does not predict when the subprocess will end after fork, the subprocess leaves a data structure called a zombie to leave the parent process with information, waits for the parent process to initiate a wait operation to bury it, at the end of the child process (logical end) to the parent process This period of time before the corpse is called the zombie process, at the end of the parent process, all of the child processes are held responsible by Init, so if the parent process ends, the zombie process is reclaimed, but if the parent process never ends, the zombie processes will occupy the process number, if the system process number Depleted, it will result in the inability to start a new process, so it is a safe practice to bury the child processes that are generated for you in the parent process.
int pcntl_waitpid (int $pid, int & $status [, int $options = 0]):
Suspends the current process until the child process of the given $pid exits, or the current process receives an exit signal, or receives an IGE signal to invoke a signal processor.
If a given $pid the corresponding subprocess has exited (zombie state) when calling this function, the function returns immediately and all system resources are freed.
$pid: The process number, less than 1 indicates that any child processes in the process group are waiting, and the process group number is the absolute value of the $pid. Equal to-1 indicates that waiting for an arbitrary forbidden City is consistent with the pcntl_wait function behavior. Equal to 0 represents a child process that waits with the calling process in the same group, and greater than 0 represents a specific process.
$status: Used to return child process state by function. This state information is generated by the following functions: Pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, Pcntl_wexitstatus, Pcntl_wtermsig, Pcntl_ Wstopsig.
$options: Same $options meaning as pcntl_wait
int pcntl_wexitstatus (int $status):
Returns a broken subprocess return code that is only useful when the pcntl_wifexited function returns TRUE.
$status parameter is the state information produced by Pcntl_waitpid.
BOOL pcntl_wifexited (int $status):
Checks whether the given State indicates that the child process is exiting normally.
BOOL pcntl_wifsignaled (int $status):
Checks whether a given state indicates that a child process was exited because of a signal being received.
BOOL pcntl_wifstopped (int $status):
Check to see if the $status can indicate that the child process is currently stopped, and this function is valid only if the wuntraced that is acting on the value of the $options parameter is applied to the PCNTL_WAITPID function.
int pcntl_wstopsig (int $status):
The analysis $status returns the number of signals that stop the child process, which is valid only if pcntl_wifsignaled returns TRUE.
int pcntl_wtermsig (int $status):
Returns the signal number that interrupted the process. This function is valid only if the pcntl_wifsignaled returns TRUE.