簡體   English   中英

在C中的waitpid調用

[英]waitpid call in C

我創建了一個簡單的程序,通過演示子進程和父進程來驗證在waitpid()調用中使用statloc參數。 我讀過某個地方,在任何過程中,通過exit()返回的值都通過statloc傳遞給parent。 因此,我試圖在父上下文中打印statloc變量,但它看起來與我通過exit()返回的內容不同。 如果我誤會了,請糾正我。 下面是代碼和結果:(我希望234為statloc值)

#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 

使用WEXITSTATUS(statloc)獲取子進程退出代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM