Jump to content

How do I share variables and objects between children created using pcntl_fork()


dlublink

Recommended Posts

I want to share objects between the children processes and the parent. So I wrote this simple code to illustrate what I want to do :

 


$obj = new Cool() ;
$obj->ping();

$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // we are the parent
     pcntl_wait($status); //Protect against Zombie children
     $obj->ping();
} else {
     $obj->ping();
     // we are the child
}

Class Cool {
     private $pong = 0;
     public function __construct()
     {
     }
     public function ping()
     {
          echo $this->pong++ ."\n";
     }
}

 

( This code was taken from http://ca.php.net/manual/en/function.pcntl-fork.php with a small modification to illustrate my need ).

 

Here is my phpinfo()

 

phpinfo()

PHP Version => 5.2.4-2ubuntu5.5

 

System => Linux dev 2.6.24-24-server #1 SMP Wed Apr 15 16:36:01 UTC 2009 i686

Build Date => Feb 11 2009 20:07:10

Server API => Command Line Interface

Virtual Directory Support => disabled

Configuration File (php.ini) Path => /etc/php5/cli

Loaded Configuration File => /etc/php5/cli/php.ini

Scan this dir for additional .ini files => /etc/php5/cli/conf.d

additional .ini files parsed => /etc/php5/cli/conf.d/curl.ini,

/etc/php5/cli/conf.d/gd.ini,

/etc/php5/cli/conf.d/memcache.ini,

/etc/php5/cli/conf.d/mysql.ini,

/etc/php5/cli/conf.d/mysqli.ini,

/etc/php5/cli/conf.d/pdo.ini,

/etc/php5/cli/conf.d/pdo_mysql.ini,

/etc/php5/cli/conf.d/xdebug.ini,

/etc/php5/cli/conf.d/xsl.ini

 

PHP API => 20041225

PHP Extension => 20060613

Zend Extension => 220060519

Debug Build => no

Thread Safety => disabled

Zend Memory Manager => enabled

IPv6 Support => enabled

Registered PHP Streams => zip, php, file, data, http, ftp, compress.bzip2, compress.zlib, https, ftps 

Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

Registered Stream Filters => string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, convert.iconv.*, bzip2.*, zlib.*

 

This server is protected with the Suhosin Patch 0.9.6.2

 

Copyright © 2006 Hardened-PHP Project

 

This program makes use of the Zend Scripting Language Engine:

 

Zend Engine v2.2.0, Copyright © 1998-2007 Zend Technologies

    with Xdebug v2.0.2, Copyright © 2002-2007, by Derick Rethans

 

here is the output

0

1

1

 

As you can see, this PHPinfo is taken from my development server, not production.

 

So, how do I get the pings to count 012 instead of 011 ?

 

Thanks,

 

David

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.