PHP shared block memory and fork

I am trying to create a counter using shared block memory, just look at the code:

$i=0; $counter = new counter('g'); while($i<3){ $pid = pcntl_fork(); echo $counter->get()."\t".$i."\t".$pid."\n"; $i++; } class counter { protected static $projID = array(); protected $t_key; protected $length; function __construct($projID){ !in_array( $projID, self::$projID) or die('Using duplicate project identifer "'.$projID.'" for creating counter'); self::$projID[] = $projID; $this->t_key = ftok(__FILE__, $projID); $this->shmid = shmop_open($t_key, 'c', 0755, 64); $this->length = shmop_write($this->shmid, 0, 0); shmop_close($this->shmid); } function get(){ $sem = sem_get($this->t_key, 1); sem_acquire($sem); $shmid = shmop_open($this->t_key, 'c', 0755, 64); $inc = shmop_read($shmid, 0, $this->length); $this->length = shmop_write($shmid, $inc+1, 0); shmop_close($shmid); sem_release($sem); return $inc; } } 

But il will get a strange result

 7 0 2567 8 1 2568 9 0 0 1 1 0 2 2 2569 40 1 2570 4 2 2572 3 2 0 51 2 2571 52 1 0 63 2 0 5 2 0 64 2 2573 65 2 0 

I want to create this class to read and write lines in a file in multithreading.

0
source share
2 answers

You do not finish child processes at all, they will never end. You also do not check whether the process is correct or not, there is no control over what finished processing and in what order. The process viking is not really multi-threaded, which other languages ​​provide, all that happens is that the current process is copied and the variables are separated - your $ i does not end at 3, and there is no guarantee that the process ends first or last .

Try:

 while($i < 3) { $pid = pcntl_fork(); if($pid == -1) { // some sort of message that the process wasn't forked exit(1); } else { if($pid) { pcntl_wait($status); // refer to PHP manual to check what this function does } else { // enter your code here, for whatever you want to be done in parallel // bear in mind that some processes can finish sooner, some can finish later // good use is when you have tasks dependent on network latency and you want // them executed asynchronously (such as uploading multiple files to an ftp or // synchronizing of something that being done over network // after you're done, kill the process so it doesn't become a zombie posix_kill(getmypid(), 9); // not the most elegant solution, and can fail } } } 
+1
source

You are not dealing with the PID after your pcntl_fork call. Your forks unfold because the cycle continues to execute and evolve.

If you are not trying to create a localized fork bomb, you probably do not want your forks to develop.

I did some work locally to try to figure out if this could solve the problem, but it is not. It seems that the shared memory segment is not being written correctly, as if one of the numbers on both sides of the line was repeated, which corrupts all this and makes things start.

Full speculation.

You might want to consider another way to do parallel processing using PHP. Using Gearman as a multiprocessor work queue is my favorite solution.

0
source

All Articles