note 103307 added to function.pcntl-fork

From: Date: Wed, 06 Apr 2011 15:27:03 +0000
Subject: note 103307 added to function.pcntl-fork
Groups: php.notes 
Request: Send a blank email to php-notes+get-178564@lists.php.net to get a copy of this message
Answer for somebody 12-Aug-2010 10:46: > - sometimes one process gets the result of the OTHER processes' > query! (because both send their queries down the same socket, > and it's pure luck who gets the reply) This problem may be worked around with semaphores. Then you won't have concurrent reads/writes on the socket. Assuming you'd like to write a child process that does nothing but keeping a connection alive you could write the following code: <?php // this code is based on @somebody's example $sem_key = mt_rand(); $conn = mysql_connect('server', 'username', 'password'); $f=pcntl_fork(); if ($f){ $sem = sem_get($sem_key); // parent while(true){ sleep(0.1); if (!sem_acquire($sem)) die("parent: semaphore cannot be acquired"); $r=mysql_query("select $f;"); if(!$r) {posix_kill($f, SIGKILL);die($f.": ".mysql_error()."\n");} list($x)=mysql_fetch_array($r); if (!sem_release($sem)) die("parent: semaphore cannot be released"); echo "."; if($x!=$f){posix_kill($f, SIGKILL);die ($f.": fail: $x!=$f\n ");} } } else{ $sem = sem_get($sem_key); // child while(true){ sleep(0.5); if (!sem_acquire($sem)) die("parent: semaphore cannot be acquired"); if (!mysql_ping($conn)) { die("Lost connection"); } if (!sem_release($sem)) die("parent: semaphore cannot be released"); echo "-"; } } ?> If you remove the sem_acquire/sem_release lines from the above code, then you'll really soon meet the problem "somebody" mentioned. Of course the sleep values in the above example are set especially to reproduce the "concurrent read" problem if the semaphore is not there. For a real keep-connection-alive example change the sleep values in the parent to 40, and in the child to 15. ---- Server IP: 69.147.83.197 Probable Submitter: 80.98.101.3 ---- Manual Page -- http://www.php.net/manual/en/function.pcntl-fork.php Edit -- https://master.php.net/note/edit/103307 Del: integrated -- https://master.php.net/note/delete/103307/integrated Del: useless -- https://master.php.net/note/delete/103307/useless Del: bad code -- https://master.php.net/note/delete/103307/bad+code Del: spam -- https://master.php.net/note/delete/103307/spam Del: non-english -- https://master.php.net/note/delete/103307/non-english Del: in docs -- https://master.php.net/note/delete/103307/in+docs Del: other reasons-- https://master.php.net/note/delete/103307 Reject -- https://master.php.net/note/reject/103307 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178564) next »