PCNTL (Process Control – http://www.php.net/manual/en/book.pcntl.php) is an extension of PHP. It implements the Unix style of process creation, program execution, signal handling and process termination. It 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. Note that this extension is not available on Windows platforms.

2. Installing and using PCNTL

Installing PCNTL

If Process Control support in your PHP is not enabled by default, you have to compile the CGI or CLI version of PHP with –enable-pcntl configuration option when compiling PHP to enable Process Control support.

Using PCNTL

With PCNTL extension, we use pcntl_fork() to create parallel processes.

When pcntl_fork() is called, it will return one of three values. If the return value is -1, the fork failed and there is no child process. This may be as a result of a lack of memory, or because the system limit on the number of user processes has been reached. If the return value is any number higher than 0, the current script is the parent that called pcntl_fork() and the return value is the process ID (PID) of the child that was forked. Finally, if the return value is 0, the current script is the child that was forked. So base on return value of pcntl_fork(), we can define what to do in each process (child or parent).

Example of create child process

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?php

$pid=pcntl_fork();

switch($pid){

case-1:

echo"Could not fork!\n";

exit;

case0:

echo"In child!\n";

break;

default:

echo"In parent!\n";

break;

}

?>

When successfully fork, child process will be the copy of parent process. An important notice that both of them carry on from the pcntl_fork() and the child gets a copy of all the variables that were set in the parent, even down to the resources. Copy of a resource does not make it a unique resource – child process and parent process will both point to the same thing, and this might be problematic – more on that later.

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?php

$pid=pcntl_fork();

switch($pid){

case-1:

echo"Could not fork!\n";

exit;

case0:

echo"In child!\n";

exit();

break;

default:

echo"In parent!\n";

break;

}

echo"process continue ...";

?>

If you do not have exit() statement in case 0, child process will not only print “In child” but also print “process continue …” because child process will carry on all line code from pcntl_fork() statement position.

When a child process exits, it will send a SIGCHLD signal to parent process and we can use that signal in parent process to handle what child process had finished.

In parent process, define a function to handle signal from child processes

1

2

3

4

5

6

7

8

9

functionsigHandler($signo){

switch($signo){

caseSIGCHLD:

// do something when a child exit

break;

default:

break;

}

}

And add handler function with pcntl_signal() statement

pcntl_signal(SIGCHLD, ‘sigHandler’);

3. Conclusion

In my opinion, PCNTL is an suitable PHP extension to create parallel processes. It comes with easy to use functions and we can find many guide of other people to implement your own code.

This is my first blog, it is information that I and Ngo Tuan Long searched for PCNTL of PHP. We are very glad to receive your feedback and comments.