试图模拟WEXITSTATUS功能,这需要获取退出状态返回值的第8位至第15位。
void My_Exit_Status_Check(unsigned int status){
pid_t pid;
unsigned mask, mask2;
unsigned result[7], result2[8];
unsigned i;
unsigned ans1 = 0, ans2=0;
mask = ((1 << 7) - 1) << 0;
ans1 = status & mask;
mask2 = ((1 << 8) - 1) << 8;
ans2 = (status >> 0) & mask2;
if(binary_to_decimal(ans1)==0){
printf("The exit status for child %d using my status check function is: Normal termination, exit status is: %d should equal %d\n", getpid(), binary_to_decimal(ans2), WEXITSTATUS(status));
}
else{
printf("The exit status for child %d using my status check function is: Abnormal termination, signal is: %d\n", getpid(), binary_to_decimal(ans1));
}
}
只需用0xFF00
掩盖它,然后向右移动8。假设您从右边和从0开始计数。