2016-04-05 32 views
0

Çocuk ve ebeveyn işlemlerini göstererek waitpid() çağrısında statloc parametresinin kullanımını doğrulamak için basit bir program oluşturdum. Herhangi bir süreçte output() yoluyla dönüş değeri, statloc aracılığıyla ebeveyne giden bir yer okudum. Bu nedenle, üst değişken bağlamında statloc değişkeni yazdırmaya çalışıyorum ancak çıkış() ile geri döndüğümden farklı görünmektedir. Yanlış anlaşılırsam lütfen beni düzeltin. Aşağıda kod ve sonucudur: (ı statloc değeri olarak 234 bekliyorum)waitpid çağrısı 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); 

}  

} 

Çıktı

:

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

Örneği oku. [bu 'waitpid' referansı] (http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html). Size, elde ettiğiniz değerin, * bitler halinde oluşturulmuş * bayrakların bir araya getirildiği ve işlemden gerçek geri dönüş değeri olduğunu söyleyecektir. –

+0

"C" hakkında soru soruyorsanız, bu neden C++ ile etiketlenir? –

cevap

İlgili konular