C语言 C - 检索大于 8 位的孩子的退出状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33288356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - Retrieving a child's exit status that is larger than 8 bits
提问by aanrv
Note: For simplicity, I don't include much error checking and my sample code doesn't really serve any practical purpose.
注意:为简单起见,我没有包含太多错误检查,我的示例代码并没有真正用于任何实际目的。
What I want:
我想要的是:
I want a program that fork()s a child process and has it invoke a process using execl(). My parent then retrieves the exit code for that process. This is fairly trivial.
我想要一个程序,它fork()是一个子进程并让它使用execl(). 我的父母然后检索该进程的退出代码。这是相当微不足道的。
What I tried:
我试过的:
int main(int argc, char** argv) {
int ch = fork();
if(ch == -1) {
perror(NULL);
}else if(ch == 0) {
// In child, invoke process
execl("/path/process", "process", 0);
}else {
// In parent, retrieve child exit code
int status = 0;
wait(&status);
// print exit status
if(WIFEXITED(status)) printf("%d\n", WEXITSTATUS(status));
}
}
My issue:
我的问题:
WEXITSTATUS()only retrieves the lower 8 bits of the exit value while I need all the bits from the intvalue. Specifically, processperforms a calculation and the result may be larger than 8 bits. It may even be negative, in which case, the highest bit will be needed to represent the correct value.
WEXITSTATUS()只检索退出值的低 8 位,而我需要该int值的所有位。具体的,process进行一次计算,结果可能大于8位。它甚至可能是负数,在这种情况下,需要最高位来表示正确的值。
What else I tried:
我还尝试了什么:
Also, while looking around, I found the pipe()function. However, I'm not sure how to use it in this situation since, after calling execl(), I can't write to a file descriptor from within the child.
另外,在环顾四周时,我发现了该pipe()功能。但是,我不确定在这种情况下如何使用它,因为在调用 之后execl(),我无法从子级内部写入文件描述符。
So how can I go about retrieving a child's exit status that is larger than 8 bits?
那么我怎样才能检索大于 8 位的孩子的退出状态呢?
回答by CristiFati
I don't think that what you are trying to accomplish it's possible, because in Linux (actually i think it's UX specific), a process exit code is an 8bit number (max 256): 0-255 (and as a convention, 0 means success, anything else means error) and lots of stuff rely on this fact (including the macros you used). Take the following piece of code:
我不认为你想要完成的事情是可能的,因为在 Linux 中(实际上我认为它是特定于用户体验的),进程退出代码是一个 8 位数字(最大 256):0-255(作为惯例,0意味着成功,其他任何事情都意味着错误)并且很多东西都依赖于这个事实(包括您使用的宏)。取下面的一段代码:
// a.c
int main() {
return 257;
}
If you compile it (gcc a.c), and run the resulting executable (a.out) checking (echo $?) its exit code (that will be truncated by the OS; hmm or is it the shell?) it will output 1 (wrap around arithmetic): 257 % 256 = 1.
如果您编译它 ( gcc a.c),并运行生成的可执行文件 ( a.out) 检查 ( echo $?) 它的退出代码(这将被操作系统截断;嗯还是外壳?)它将输出 1 (环绕算术): 257% 256 = 1。
As an alternative as you mentioned, you could use pipe(this postis pretty descriptive) or sockets (AF_UNIXtype).
作为您提到的替代方案,您可以使用pipe(这篇文章非常具有描述性)或套接字(AF_UNIX类型)。
回答by user3629249
this code is from: How to send a simple string between two programs using pipes?
这段代码来自:如何使用管道在两个程序之间发送一个简单的字符串?
writer.c
作家.c
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", sizeof("Hi"));
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
reader.c
阅读器
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
close(fd);
return 0;
}
the code, probably the parent/reader, should delete the fifo node, perhaps by calling rm.
Otherwise the fifo entry is left in existence, even across a re-boot, just like any other file.
代码,可能是父/阅读器,应该删除 fifo 节点,也许是通过调用rm. 否则,即使重新启动,fifo 条目也会存在,就像任何其他文件一样。

