popen使用不当引起产生僵尸进程

函数签名

#include <stdio.h>

FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

DESCRIPTION
       The  popen() function opens a process by creating a pipe, forking, and invoking the shell.  Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
       
RETURN VALUE
       The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory.

       The pclose() function returns -1 if wait4(2) returns an error, or some other error is detected.  In the event of an error, these
       functions set errnro to indicate the cause of the error.
ERRORS
       The  popen()  function  does not set errno if memory allocation fails.  If the underlying fork(2) or pipe(2) fails, errno is set appropriately.  If the type argument is invalid, and this condition is detected, errno is set to EINVAL.

       If pclose() cannot obtain the child status, errno is set to ECHILD.

popen函数会通过fork产生子进程,然后从子进程中调用/bin/sh -c执行参数command指令,子进程退出的时候会发送SIGCHLD信号,如果父进程没对此信号处理,就会导致僵尸进程,因此建议有用到popen的都加个signal(SIGCHLD, SIG_IGN);

SIG_IGN表示忽略对信号SIGCHLD的处理,或者调用wait()

调用wait()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用 wait() 时子进程已经结束,则 wait() 会立即返回子进程结束状态值。子进程的结束状态值会由参数 status 返回,而子进程的进程识别码也会一快返回

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值