2016-04-05 9 views
0

私は、子プロセスと親プロセスのデモンストレーションによってwaitpid()コールでstatlocパラメータの使用を検証する簡単なプログラムを作成しました。私はどこかのプロセスでexit()を通した戻り値がstatloc経由で親に送られているところを読んでいます。したがって、私は親コンテキストでstatloc変数を出力しようとしていますが、exit()を通して返されているものとは異なって表示されています。私が誤解した場合は、私を修正してください。以下のコードと結果である:(Iはstatloc値として234を期待しています)waitpid call in C

#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)。あなたが得られる値は、* flags *ビット付きの集合と、プロセスからの実際の戻り値を加えたものです。 –

+0

なぜ 'C 'について質問しているのですが、これは' C++ 'でタグ付けされていますか? –

答えて