Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
990 views
in Technique[技术] by (71.8m points)

shell - C++: system(0) Returns 0

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...