**已关闭。**此问题需要debugging details。目前不接受回答。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
4年前关闭。
Improve this question的
在RHEL 7.4中运行以下C代码:
errno = 0;
status = system("ls >> /tmp/test.txt");
sprintf(msg, "Value of errno: %d\n", errno);
sprintf(msg, "Status: %d ", status);
os_log_msg(msg);
字符串
我得到的返回码为-1和errno = 10(没有子进程)。/tmp/test. txt文件实际上是创建的,所以它可以工作,但是程序看到一个非零的返回码并退出。
问题是,这个命令在HP-UX 11.11中返回0,但我们迁移到RHEL 7.4,现在我们得到-1。
1条答案
按热度按时间xcitsw881#
如果初始创建子进程,
system
只能返回值-1(通过fork
)或收集其退出状态(通过wait
)失败。这两种情况都不会发生,因为传递给system
的命令有问题,因为该命令是在子进程中解释的。该命令的问题将显示为system
返回值s
不等于0或-1,并且WIFEXITED(s) && WEXITSTATUS(s) != 0
或WIFSIGNALED(s)
为真。(宏WIFEXITED
、WIFSIGNALED
和WEXITSTATUS
在sys/wait.h
中定义。)(请参阅the POSIX specification forsystem
以了解为什么会发生这种情况。)fork
故障通常只会在系统范围的资源耗尽和/或严重的强制资源配额时才会发生。字符串
当我运行它。
型
如果您有一个窃取等待状态的SIGCHLD处理程序,则
system
内部可能会发生wait
故障。型
(SIGCHLD处理程序还有其他几种方式可以干扰
system
;这只是我能想到的最短的演示程序。)型
你说你传递给
system
的任何命令都能正确执行,但是system
仍然返回-1,这让我认为你的问题是由于wait
和SIGCHLD
处理程序之间的交互不好。errno
中的(ECHILD
)与此假设一致,因为wait
被记录为产生该错误代码,而fork
不是。我必须强调,这是一个假设的基础上提供的信息。为了确保这是正确的,我们需要看到一个 * 完整的 * 测试程序,我们可以编译和运行自己,并观察到完全相同的失败条件,你是。请阅读并按照说明在https://stackoverflow.com/help/mcve。