Sharing variables between child processes in PHP? Sharing variables between child processes in PHP? multithreading multithreading

Sharing variables between child processes in PHP?


forked children will gain their own dedicated copy of their memory space as soon as they write anywhere to it - this is "copy-on-write". While shmop does provide access to a common memory location, the actual PHP variables and whatnot defined in the script are NOT shared between the children.

Doing $x = 7; in one child will not make the $x in the other children also become 7. Each child will have its own dedicated $x that is completely independent of everyone else's copy.


As long as father and children know the key/keys of the shared memory segment is ok to do a shmop_open before pcnlt_fork. But remember that pcnlt_fork returns 0 in the child's process and -1 on failure to create the child (check your code near the comment /confusion/). The father will have in $pid the PID of the child process just created.

Check it here:

http://php.net/manual/es/function.pcntl-fork.php


Use this class:http://pastebin.com/0wnxh4gY

http://framework.zend.com/manual/1.7/en/zendx.console.process.unix.overview.html

It utilizes shm functions to share variables across many processes with the setVariable method...obviously you shoud use it running PHP in some kind of cgi mode most likely php-fpm