PHP provides an extension pcntl that can take advantage of the operating system's fork calls to implement multiple processes. The code executed after the fork call will be in parallel.
Note: Pcntl only supports Linux platforms and is only available in CLI mode.
$pid = pcntl_fork();if($pid > 0){//父进程代码exit(0);} elseif($pid == 0) {//子进程代码exit(0);}
PHP officially does not provide multi-threaded extensions, PECL has a pthreads extension provides multi-threaded features, the address is http://pecl.php.net/package/pthreads, this extension is only available in the thread-safe version. Multi-process and multithreading are actually the same function. Difference is
- Threads are within the same process and can share memory variables for inter-thread communication
- Threads are more lightweight than processes, and a large number of processes consume more system resources than threads
Multithreading also has some problems:
- Thread read-write variable has synchronization problem, need to lock
- The lock's grain pass the Convention has a performance issue that could cause only 1 threads to be running, while other threads are waiting for the lock. So it's not parallel.
- Using multiple locks at the same time, logic is complex, and once a lock is not released correctly, thread deadlock can occur
- A fatal error on a thread can cause the entire process to crash
The multi-process approach is more stable, and data sharing can also be achieved using interprocess communication (IPC).
- Shared memory, which is the same as the read and write variables between threads, need to lock, there will be synchronization, deadlock problem.
- Message Queuing, can use multiple sub-processes to grab the queue mode, performance is very good
- Pipe,unixsock,tcp,udp. You can use Read/write to pass data, TCP/UDP use sockets to communicate, sub-processes can be distributed and run
With fork, you can implement one of the simplest concurrent TCP servers. The main process accept connection, there is a new connection arrives fork a child process. The recv/send loops in the subprocess and processes the data. This mode is very useful in the case of very few requests, like an FTP server. In the past, there are many Linux programs that are this pattern, simple and efficient, dozens of lines of code can be implemented. Of course, this model in hundreds of concurrent cases is good, a lot of concurrency in the case of a bit too much consumption.
if (($Sock=Socket_create (Af_inet,Sock_stream,0))<0) {Echo"Failed to create socket:".Socket_strerror ($Sock." \ n ";Exit ();}if (($Ret=Socket_bind ($Sock$Address$Port))<0) {Echo"Failed to bind socket:".Socket_strerror ($Ret." \ n ";Exit ();}if (($Ret=Socket_listen ($Sock0))<0) {Echo"Failed to listen to socket:".Socket_strerror ($Ret." \ n ";Exit ();}while (True) {$Conn= @Socket_accept ($sock);Child processIfPcntl_fork ()==0) {$Recv=Socket_read ($conn, 8192); $send_data = Span class= "string" > "server:" . $RECV; socket_write ($conn, $send_data); socket_close ($conn); span class= "keyword" >exit (0);} else{socket_close ($conn)}
PHP Parallel multi-process/multithreaded