system() function while SIGCHLD is ignored

眉间皱痕 提交于 2019-12-11 09:54:45

问题


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\n", 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?


回答1:


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.



来源:https://stackoverflow.com/questions/25039369/system-function-while-sigchld-is-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!