【发布时间】:2020-02-19 12:59:00
【问题描述】:
我正在学习过程,在下面的代码sn-p中:
/*
* fork10 - Synchronizing with multiple children (wait)
* Reaps children in arbitrary order
* WIFEXITED and WEXITSTATUS to get info about terminated children
*/
void fork10()
{
pid_t pid[N];
int i, child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0) {
exit(100+i); /* Child */
}
for (i = 0; i < N; i++) { /* Parent */
pid_t wpid = wait(&child_status);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminate abnormally\n", wpid);
}
}
当我跟踪 WIFEXITED 函数定义时,这就是我得到的:
#define WIFEXITED(x) (_WSTATUS(x) == 0)
#define _WSTATUS(x) (_W_INT(x) & 0177)
#define _W_INT(w) (*(int *)&(w)) /* convert union wait to int */
我有两个问题:
- 为什么要用掩码
0177而不是0b1111111,我见过很多次了,用八进制数比其他格式有什么好处?我个人觉得二进制更直观。 -
(*(int *)&(w))是什么意思?
谢谢!
【问题讨论】:
-
恕我直言
0177是写0x7F的一种混淆方式。 -
是的,八进制只是纯粹的混淆。
127可读性更强,0x7F可读性最高。 -
0b1111111不清楚。是6个吗? 7?要知道0b1111111的含义,必须花时间数一数。 (我发现0x7F在这里最清楚:除了最重要的位之外的所有)
标签: c process operating-system