解决system调用返回ECHILD的问题
2015-08-03 11:14
225 查看
system调用返回ECHILD
再来看看system返回ECHILD错误的原因,根据system内部实现fork-exec-waitpid,查到应该是waitpid设置了errno,查看waitpid的man page,有下面的描述信息:
If the calling process has SA_NOCLDWAIT set or has SIGCHLD set to SIG_IGN, and the process has no unwaited-for children that were transformed into zombie processes, the calling thread shall block until all of the children of the process containing the calling thread terminate, and wait() and waitpid() shall fail and set errno to [ECHILD].
我的理解是,如果调用进程设置了SIGCHLD为SIG_IGN,那么使用waitpid的时候会阻塞,等待子进程执行完,并且设置errno为ECHILD。
由此我们解决问题的思路已经明朗了,因为我们的代码在最开始将SIGCHLD设置为SIG_IGN,所以现在我们在调用system的时候,需要将信号还原,设置为默认值SIG_DEF,参考了http://my.oschina.net/renhc/blog/54582 这篇博客后,修改方式如下:
再来看看system返回ECHILD错误的原因,根据system内部实现fork-exec-waitpid,查到应该是waitpid设置了errno,查看waitpid的man page,有下面的描述信息:
If the calling process has SA_NOCLDWAIT set or has SIGCHLD set to SIG_IGN, and the process has no unwaited-for children that were transformed into zombie processes, the calling thread shall block until all of the children of the process containing the calling thread terminate, and wait() and waitpid() shall fail and set errno to [ECHILD].
我的理解是,如果调用进程设置了SIGCHLD为SIG_IGN,那么使用waitpid的时候会阻塞,等待子进程执行完,并且设置errno为ECHILD。
由此我们解决问题的思路已经明朗了,因为我们的代码在最开始将SIGCHLD设置为SIG_IGN,所以现在我们在调用system的时候,需要将信号还原,设置为默认值SIG_DEF,参考了http://my.oschina.net/renhc/blog/54582 这篇博客后,修改方式如下:
typedef void (*sighandler_t)(int); int pox_system(const char *cmd_line) { int ret = 0; sighandler_t old_handler; old_handler = signal(SIGCHLD, SIG_DFL); ret = system(cmd_line); signal(SIGCHLD, old_handler); return ret; }
相关文章推荐
- 数据库导入导出命令
- 算法:给定多个数字,把多个数字任意组合,选出组合后最大的数
- 括号
- 锦上
- 黑马程序员——JAVA基础---反射
- uva 10763 Foreign Exchange(排序+二分查找)
- Eclipse注解模板设置详解
- Algorithms—160.Intersection of Two Linked Lists
- OSG更新遍历(updateTraversal)流程
- apt-get常用命令
- HDU 1285 确定比赛名次 (拓扑排序)
- HDU5335 Walk Out(dfs+递推)
- java;jsp;tomcat;mysql;hibernate;j2ee 编码中文乱码全面解决方案
- [LeetCode] Shortest Palindrome I
- Shell 学习20 - Shell 跳出循环
- Redis使用总结之与Memcached异同
- linux串口的设定
- 【学习笔记】HTML 5 拖放
- 手机APP脚本录制(LoadRunner 12)
- 基于HBase的入库方案效率对比验证(二)