PROBESEVEN - Multi-Processing in PHP at a Glance!

php-logoFew 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
  1. pcntl_fork — Forks the currently running process
  2. pcntl_waitpid — Waits on or returns the status of a forked child
  3. 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";
}
?>

pcntl_fork

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;
	}
}
?>
pcntl_fork_example_2In 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";
}
?>

pcntl_fork_example_3

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:
  1. 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.
  2. This extension is not available on Windows platforms.

 

4 thoughts on “Multi-Processing in PHP at a Glance!”

  1. Excellent work Balasundaram. Very informative and helpful one for all. Continue posting such valuable blogs. Keep it up…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>