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
1.0k views
in Technique[技术] by (71.8m points)

c - system() function while SIGCHLD is ignored

Here is an example piece of my code

signal(SIGCHLD, SIG_IGN);

ret = system("ls -al");

if(ret < 0)
{
    perror("system failed");
    printf("return value is %d
", ret);
}

The ls -al command can be executed without any problem.

But the return value of system() is always -1. In this case, I can't get the real return value of the command.

Some reference says that ignore SIGCHLD would affect waitpid() in system(), that's why system always returns -1.

But what puzzles me more is that: isn't SIGCHLD ignored by default? So why ignoring SIGCHLD explicitly would result in such case?

Could someone explain this to me?

Thanks to @cnicutar now I understand the effects of ignoring SIGCHLD.

But now I'm wondering is there any methods to get the return value of child process while SIGCHLD is ignored? Or is this just impossible due to the POSIX specification and OS realizaiton?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

But what puzzles me more is that: isn't SIGCHLD ignored by default?

No, by default it is not ignored. By default there is simply no handler and no action is taken.

So why ignoring SIGCHLD explicitly would result in such case?

Ignoring SIGCHLD has an interesting side-effect: children don't become zombies.

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with errno set to ECHILD.

That is, you don't need to wait any longer for children since they get collected automatically. But system(3) expected a child! So waitpid returns an error and system(3) behaves.


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

...