C ++:system(0)返回0 [英] C++: system(0) Returns 0
问题描述
当我调用 system(0)时,它返回0,表示shell不可用
。当使用 system (调用gcc编译hello world程序)的命令时,它工作正常,但返回-1(我认为它与 system >返回0)。
编译器: gcc version 4.6.1(Ubuntu / Linaro 4.6.1-9ubuntu3)这是什么原因导致它不可用 )
>Errno说没有子进程。
我也编译了另一个测试程序除了调用 system(0),它会返回非零值。是否会受到Code :: Blocks的影响?
EDIT
SOLVED 在系统启动后,系统(0) p>
我在早期实现这段代码时使用了 :
信号(SIGCHLD,SIG_IGN);
我用它来处理僵尸。我发现这会导致错误。
如果你忽略了 SIGCHLD
信号使用如下代码:
信号(SIGCHLD,SIG_IGN);
这将导致 system
返回 -1
当所有子项已结束时,将 errno
设置为 ECHILD
。
请参阅 http: //pubs.opengroup.org/onlinepubs/009695399/functions/wait.html 。具体来说:
如果调用进程设置了SA_NOCLDWAIT或者SIGCHLD设置为SIG_IGN,并且进程没有未被等待的子进程到zombie进程,调用线程将阻塞,直到包含调用线程的进程的所有子进程终止,并且wait()和waitpid()将失败并将errno设置为[ECHILD]。
When I call system(0) it returns 0, which means shell is not available.
When a command using system (calls gcc to compile a hello world program), it works just fine but returns -1 (I assume it's the same cause as for system(0) returning 0). What causes it to be "not available" and why does it work anyway?
Compiler: gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
OS: Ubuntu 11.10
EDIT
Errno says No Child Processes.
I have also compiled another test program that does not do anything but calls system(0) and it returns nonzero value. Can it be affected by Code::Blocks?
EDIT
So far I have figured out that system(0) only fails after I start my first pthread.
SOLVED
I used fork in my early implementation of one of this piece of code:
signal(SIGCHLD,SIG_IGN);
I used it to "handle" zombies. I just found out that this caused the error.
This could happen if you are ignoring the SIGCHLD
signal using code like :
signal(SIGCHLD, SIG_IGN);
This would cause system
to return -1
when all children have ended, setting errno
to ECHILD
.
Refer to http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html. Specifically :
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].
这篇关于C ++:system(0)返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!