Few days back had a great experience that got me rejoicing. My development IQ created a spark while working keenly on a client project. Let me introduce a useful concept for the developers to pitch through all the creations. This is all about the multi-processing in PHP.
A Quick Note:
A glance through the concepts of Multi-programming which would help to understand better the process explained in this article.
Multi-Programming
Engaging a multi-programming system, you would find one or more programs loaded in main memory which is similar to running Excel, Paint, Firefox etc.
Multi-Processing
Multi-processing refers to executing multiple processes at a same time. In fact, multiprocessing refers to the hardware not software. A computer using more than one CPU (Physical processor) at a time is called multi-processing.
Multi-Tasking
In multi-tasking, a single CPU is involved, but it switches from one task/program/job/process to another most swiftly that it gives the appearance of executing all of the programs at the same time.
Multi-Threading
Multi-threading is an execution model that allows a single process to have multiple code segments (one process is divided into sub-tasks.) run concurrently within the “context” of that process.
The need for PCNTL functions:
Any modern operation system performs Multi-tasking, which means we can run several programs at the same time. In php, we are using a single process concept and assigning all our work to one process. It will be executed one by one and ultimately takes more time for completion. If we split our one process task into a multi-process task using operating system feature ‘multi-tasking’, we can complete the process in great speed. Using ‘PCNTL’ function, we can do multi-process tasks concept in php.
Multiprocessing in PHP
- pcntl_fork — Forks the currently running process
- pcntl_waitpid — Waits on or returns the status of a forked child
- pcntl_wexitstatus — Returns the return code of a terminated child
The pcntl_fork() function creates a child process that differs from the parent process only in its PID. Both the parent and child process will be exactly the same up until the moment of the fork. Any variables up to that point will be exactly the same in both processes. After forking, changing a variable’s value in one process doesn’t affect the other process though.
When we call pcntl_fork(), it will return one of the three values. They are:
- -1 – is return on failure.
- 0 – is returned in the child’s thread of execution.
- PID – is returned in the parent’s thread of execution.
Example 1:
<?php $pid = pcntl_fork(); switch($pid) { case -1: print "Could not fork!\n"; exit; case 0: print "In child!\n"; break; default: print "In parent!\n"; } ?>The above script just print out a message in the both parent and child processes.
Example 2:
<?php for ($i = 1; $i <= 5; ++$i) { $pid = pcntl_fork(); if (!$pid) { sleep(1); print "In child $i\n"; exit; } } ?>In this script, we started with one process and forked it as five process. In foreach each time, we created one new child process and it did not affect the parent process. Because we exit the child process after the print the message.
Example 3:
<?php for ($i = 1; $i <= 5; ++$i) { $pid = pcntl_fork(); if (!$pid) { sleep(1); print "In child $i\n"; exit($i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } ?>In this sample, We terminate the child process using exit($i) function and then return the status of a forked child from pcntl_waitpid(). Using pcntl_wexitstatus() function, we can get the return value of child process. pcntl_wexitstatus() function returns the return code as an integer.
Attention Note:
- Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment.
- This extension is not available on Windows platforms.
October 29, 2015 at 2:35 pm
Excellent work Balasundaram. Very informative and helpful one for all. Continue posting such valuable blogs. Keep it up…
October 29, 2015 at 3:10 pm
Great intro to multi-process in PHP.
October 29, 2015 at 3:15 pm
Hi Bala, It’s very interesting & useful post for PHP developers!.
October 31, 2015 at 6:18 pm
Very informative thank you..