C - 检索大于 8 位的 child 的退出状态
C - Retrieving a child's exit status that is larger than 8 bits
注意:为简单起见,我没有包含太多错误检查,而且我的示例代码实际上没有任何实际用途。
我想要的:
我想要一个 fork()
是一个 child 进程并让它调用一个使用 execl()
的进程的程序。然后我的 parent 检索该进程的退出代码。这是相当微不足道的。
我试过的:
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));
}
}
我的问题:
WEXITSTATUS()
仅检索退出值的低 8 位,而我需要 int
值中的所有位。具体来说,process
进行一次计算,结果可能大于8位。它甚至可能是负数,在这种情况下,将需要最高位来表示正确的值。
我还尝试了什么:
此外,在四处寻找时,我发现了 pipe()
函数。但是,我不确定在这种情况下如何使用它,因为在调用 execl()
之后,我无法从 child.
中写入文件描述符
那么我怎样才能检索大于 8 位的 child 的退出状态?
我不认为你试图完成的是可能的,因为在 Linux 中(实际上我认为它是特定于用户体验的),进程退出代码是一个 8 位数字(最大 256):0 -255(按照惯例,0 表示成功,其他任何表示错误)很多东西都依赖于这个事实(包括您使用的宏)。取下面一段代码:
// a.c
int main() {
return 257;
}
如果编译它 (gcc a.c
),并且 运行 生成的可执行文件 (a.out) 检查 (echo $?
) 它的退出代码(将由 OS 确定为 t运行;嗯,还是 shell?)它将输出 1(环绕算法):257 % 256 = 1 .
作为您提到的替代方案,您可以使用 pipe
(this post 非常具有描述性)或套接字(AF_UNIX
类型)。
此代码来自:How to send a simple string between two programs using pipes?
writer.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;
}
代码,可能是 parent/reader,应该删除 fifo 节点,也许通过调用 rm
。
否则,fifo 条目仍然存在,即使重新启动,就像任何其他文件一样。
注意:为简单起见,我没有包含太多错误检查,而且我的示例代码实际上没有任何实际用途。
我想要的:
我想要一个 fork()
是一个 child 进程并让它调用一个使用 execl()
的进程的程序。然后我的 parent 检索该进程的退出代码。这是相当微不足道的。
我试过的:
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));
}
}
我的问题:
WEXITSTATUS()
仅检索退出值的低 8 位,而我需要 int
值中的所有位。具体来说,process
进行一次计算,结果可能大于8位。它甚至可能是负数,在这种情况下,将需要最高位来表示正确的值。
我还尝试了什么:
此外,在四处寻找时,我发现了 pipe()
函数。但是,我不确定在这种情况下如何使用它,因为在调用 execl()
之后,我无法从 child.
那么我怎样才能检索大于 8 位的 child 的退出状态?
我不认为你试图完成的是可能的,因为在 Linux 中(实际上我认为它是特定于用户体验的),进程退出代码是一个 8 位数字(最大 256):0 -255(按照惯例,0 表示成功,其他任何表示错误)很多东西都依赖于这个事实(包括您使用的宏)。取下面一段代码:
// a.c
int main() {
return 257;
}
如果编译它 (gcc a.c
),并且 运行 生成的可执行文件 (a.out) 检查 (echo $?
) 它的退出代码(将由 OS 确定为 t运行;嗯,还是 shell?)它将输出 1(环绕算法):257 % 256 = 1 .
作为您提到的替代方案,您可以使用 pipe
(this post 非常具有描述性)或套接字(AF_UNIX
类型)。
此代码来自:How to send a simple string between two programs using pipes?
writer.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;
}
代码,可能是 parent/reader,应该删除 fifo 节点,也许通过调用 rm
。
否则,fifo 条目仍然存在,即使重新启动,就像任何其他文件一样。