PHP共享块内存和派生 [英] PHP Shared block memory and fork

查看:64
本文介绍了PHP共享块内存和派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建使用共享块内存的计数器,只需看一下代码即可:

Im trying to create counter that using shared block memory, just look 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;
  }
}

但是我得到奇怪的结果

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 for read and write strings in file in multithreading.

推荐答案

您根本不会结束子进程,它们永远不会结束.您也没有检查该过程是否正确分叉,无法控制完成的处理过程和处理顺序.分叉一个进程并不是其他语言提供的真正的多线程,所有发生的事情是正在复制当前进程并共享变量-您的$ i不会以3结尾,也不能保证哪个进程首先完成或最后.

You're not ending child processes at all, they'll never finish. You're also not checking whether the process forked correctly or not, there's no control over what's finished processing and in what order. Forking a process isn't really multithreading that other languages provide, all that happens is that the current process is being copied and variables are shared - your $i won't end at 3, nor is there a guarantee which process is finishing first or last.

尝试:

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's 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
        }
    }
}

这篇关于PHP共享块内存和派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆