• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP pcntl_fork出来的子进程用到了信号量退出不成功

php 搞代码 4年前 (2022-01-23) 24次浏览 已收录 0个评论
文章目录[隐藏]

使用的测试代码如下:

<code><?php cli_set_process_title('test-semaphore');$semid = sem_get(ftok(__FILE__, 'x'), 1);function daemonize(){    umask(0);    $pid = pcntl_fork();    if (-1 === $pid) {        throw new Exception('fork fail');    } elseif ($pid > 0) {        // 这里父进程可以退出        exit(0);    }        if (-1 === posix_setsid()) {        throw new Exception("setsid fail");    }    // 从这里开始信号量不删除就无法退出    $pid = pcntl_fork();    if (-1 === $pid) {        throw new Exception("fork fail");    } elseif (0 !== $pid) {        // 子进程退出不成功        exit(0);    }}daemonize();// 孙进程退出不成功</code>

在命令行验证:ps aux | grep test-semaphore 可以看到进程状态是正常的(休眠状态)

<code>xxx    115581  0.0  0.2 388532  7760 ?        Ss   11:33   0:00 test-semaphorexxx    115582  0.0  0.1 388532  7584 ?        S    11:33   0:00 test-semaphore</code>

在代码的任意地方使用sem_remove()或者ipcrm命令都可以让进程退出。

回复内容:

使用的测试代码如下:

<code><?php cli_set_process_title('test-semaphore');$<p style="color:transparent">2本文来源gao!daima.com搞$代!码网</p><span>搞代gaodaima码</span>semid = sem_get(ftok(__FILE__, 'x'), 1);function daemonize(){    umask(0);    $pid = pcntl_fork();    if (-1 === $pid) {        throw new Exception('fork fail');    } elseif ($pid > 0) {        // 这里父进程可以退出        exit(0);    }        if (-1 === posix_setsid()) {        throw new Exception("setsid fail");    }    // 从这里开始信号量不删除就无法退出    $pid = pcntl_fork();    if (-1 === $pid) {        throw new Exception("fork fail");    } elseif (0 !== $pid) {        // 子进程退出不成功        exit(0);    }}daemonize();// 孙进程退出不成功</code>

在命令行验证:ps aux | grep test-semaphore 可以看到进程状态是正常的(休眠状态)

<code>xxx    115581  0.0  0.2 388532  7760 ?        Ss   11:33   0:00 test-semaphorexxx    115582  0.0  0.1 388532  7584 ?        S    11:33   0:00 test-semaphore</code>

在代码的任意地方使用sem_remove()或者ipcrm命令都可以让进程退出。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP pcntl_fork出来的子进程用到了信号量退出不成功
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址