在退出()状态满师使用C中WEXITSTATUS宏,256什么好处? [英] Any benefit in using WEXITSTATUS macro in C over division by 256 on exit() status?

查看:391
本文介绍了在退出()状态满师使用C中WEXITSTATUS宏,256什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个练习大学,我不得不退出与返回一个值,该值竟是东西计数。这可能是255以上(其中出口()不能处理),但老师建议使用测试数据,其中计数可能永远不会高于该值。

这一切后,我需要处理这个计数值,退出状态,我通过使用waitpid函数得到了在主进程这个值()。令我惊讶的是,如果子进程返回1,在主过程中的真实价值为256,2 512等等...

我需要打印此值,所以我简单地除以256,它和它做。但是,如果我用WEXITSTATUS()宏,我也得这个值我想要的方式...

我看着C源$ C ​​$ c和这是我发现了什么:

 的#define __WEXITSTATUS(状态)(((状态)及为0xFF00)GT;> 8)

我明白这里发生了什么,例如,512二进制为10 0000 0000,移位8点会给00 0000 0010,即十进制数2的权利。我不要在此宏观理解的是,&安培;运营商和为0xFF00似乎随机数的事实(它可能不是,在那里它来自关系吗?)。这是什么做的正是,为什么会出现&安培;为0xFF00在宏?那岂不是有工作了吗?

和本主题中真正的问题,是同样的事情执行这个宏在我的code作为除以256?


解决方案

  

和本主题中真正的问题,
  是一回事执行这个宏
  在我的code作为除以256?


这可能会始终在工作的情况下,其中的子进程正常终止​​(即,通过调用exit(),不是由分段错误,断言失败,等等)。

()由 waitpid函数存储状态连接codeS两个原因,子进程被终止和退出code。和退出code存储在下一字节(由<掩盖$;原因是存储在最低显著字节(0xFF的按状态和放大器获得) C $ C>状态和放大器;为0xFF00 WEXITSTATUS())。当进程正常终止​​,原因是0,因此 WEXITSTATUS 只是相当于8移(或除以256)。但是,如果这个过程是由一个信号(如SIGSEGV)被杀,没有出口code,你必须使用 WTERMSIG 从提取信号数原因字节。

I was doing an exercise for university where I had to return a value with exit, that value was actually a count of something. This could be above 255 (which exit() can't handle) but the teacher suggested to use test data where the count could never go above that value.

After all this, I needed to handle this count value, the exit status, I got this value in the main process by using waitpid(). To my surprise, if the child process returned 1, the "real" value in the main process was 256, 2 was 512 and so on...

I needed to print this value so I simply divided it by 256 and it was done. However, if I use the WEXITSTATUS() macro, I'll also get this value the way I want it...

I looked at C source code and this is what I found out:

#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)

I understand what's happening here, for instance, 512 in binary is 10 0000 0000, shifting 8 o the right will give 00 0000 0010, which is 2 in decimal. What I don't understand in this macro is that & operator and the fact that 0xff00 seems a random number (it probably isn't, where does it come from though?). What does this do exactly, why is there "& 0xff00" in the macro? Wouldn't it work with out it?

And the real question in this topic, is the same thing to call this macro in my code as dividing by 256?

解决方案

And the real question in this topic, is the same thing to call this macro in my code as dividing by 256?

It will probably always work in the cases where the child process terminates normally (i.e., by calling exit(), not by a segmentation fault, assertion failure, etc.).

The status stored by waitpid() encodes both the reason that the child process was terminated and the exit code. The reason is stored in the least-significant byte (obtained by status & 0xff), and the exit code is stored in the next byte (masked by status & 0xff00 and extracted by WEXITSTATUS()). When the process terminates normally, the reason is 0 and so WEXITSTATUS is just equivalent to shifting by 8 (or dividing by 256). However, if the process is killed by a signal (such as SIGSEGV), there is no exit code, and you have to use WTERMSIG to extract the signal number from the reason byte.

这篇关于在退出()状态满师使用C中WEXITSTATUS宏,256什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆