Ç - 检索孩子的退出状态是大于8位 [英] C - Retrieving a child's exit status that is larger than 8 bits
问题描述
注:为简单起见,我不包括太多的错误检查和我的样本code并没有真正起到任何实际目的
Note: For simplicity, I don't include much error checking and my sample code doesn't really serve any practical purpose.
我要什么:
我要那个叉()
一个子进程并已通过调用处理的程序 EXECL()
。我的父母然后检索退出code为这一进程。这是相当微不足道的。
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.
我的尝试:
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(),而我需要从
INT
只检索退出值的低8位>值。具体来说,过程
执行计算,结果可能会大于8位。它甚至可能是负的,在这种情况下,最高位将需要重新present正确的值。
WEXITSTATUS()
only retrieves the lower 8 bits of the exit value while I need all the bits from the int
value. Specifically, process
performs 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.
还有什么我尝试:
另外,当环顾四周,我发现管道()
功能。但是,我不知道如何使用它在这种情况下,因为打完电话后 EXECL()
,我不能从孩子中写入一个文件描述符。
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.
那么,如何我可以去取回孩子的退出状态是大于8位?
So how can I go about retrieving a child's exit status that is larger than 8 bits?
推荐答案
我不认为你正在尝试完成它是可能的,因为在Linux的(其实我认为这是UX系统),一个进程退出code是一个8位号码(最多256):0-255(作为一个惯例,0表示成功,其他任何手段误差)和大量的东西,依靠这个事实(包括你使用的宏)。看看下面的一块code的:
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:
// a.c
int main() {
return 257;
}
如果您编译它( GCC AC
),并运行生成的可执行文件(的a.out 的)检查( ?回声$
),它的退出code(将要被操作系统截断;嗯或者是它的外壳),它会输出1(约算术包装):257%256 = 1。
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.
作为替代正如你所说,你可以使用管道
(<一个href=\"http://stackoverflow.com/questions/22502124/communication-between-child-and-parent-processes-in-c-linux-parent-process-not\">this后是pretty描述)或插槽( AF_UNIX
类型)。
As an alternative as you mentioned, you could use pipe
(this post is pretty descriptive) or sockets (AF_UNIX
type).
这篇关于Ç - 检索孩子的退出状态是大于8位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!