2016-04-05 47 views
0

我已經創建了一個簡單的程序來驗證在waitpid()調用中使用statloc參數是否通過演示子進程和父進程。我已經讀過某處通過exit()在任何進程中通過statloc返回到父節點的返回值。因此,我試圖在父上下文中打印statloc變量,但它與我通過exit()返回的內容不同。如果我誤解了,請糾正我。下面的代碼和結果:(我期待234 statloc值)C中的waitpid調用

#include<stdio.h> 
#include<stdlib.h> 
#include<sys/types.h> 
#include<sys/wait.h> 

int main() 

{ 
pid_t pid; 
char ch; 
int statloc; 



if((pid=fork())!=0) 
{ 

/* parents code */ 
printf("\n pid of child process is %d ", pid); 

printf("\nI am in parent "); 

waitpid(-1,&statloc,0); 

printf("\n I have yet not exited from parent "); 
printf("\n value of statloc is %d ",statloc) ; // expecting 234 as statloc's value 

exit(0); 
} 

if(pid==0) 
{ 

printf("\n hello, in child's code \n"); 

printf("\n Exiting child's code \n "); 
exit(234); 

}  

} 

輸出

avotclbh:/home/akhils/prep#./a.out 

hello, in child's code 

Exiting child's code 
pid of child process is 12464 
I am in parent 
I have yet not exited from parent 
value of statloc is 59904 
+1

閱讀例如[這個'waitpid'參考](http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html)。它會告訴你,你得到的價值是一組*標誌* bitwised在一起,再加上過程的實際返回值。 –

+0

如果你問'C',爲什麼用'C++'標記? –

回答