簡體   English   中英

在C程序中返回wait()的值

[英]Return value of wait() in C program

我正在嘗試打印wait()函數的當前值。 我從輸出中想到的是,當子進程正在運行時,子上下文中的wait()的當前值為-1,一旦完成並返回,則在父上下文中,wait()的值等於其子進程的pid。 根據我的理解,這暗示是正確的嗎?

#include<stdio.h>
#include<stdlib.h>

int main ()

{
 int statloc;
int stat;
 printf("\nthis process id is %d", getpid());

 int pid;
pid =fork();
stat=wait(&statloc);

printf("\n Value of stat is %d",stat);
getchar();
}

輸出:

       this process id is 10740
 Value of stat is -1k                 // k is entered as input due to getchar
        this process id is 10740
 Value of stat is 10741j               // j is entered as input due to getchar

我認為您的理解是錯誤的。

使用wait是等待直到子進程中的任何一個退出。 如果子進程退出,它將返回已退出子進程的進程ID,並將該進程的退出狀態存儲在傳遞給wait函數的參數中。

我們可以使用WEXITSTATUS()宏獲取退出狀態。

在該宏中,我們必須將傳遞給參數的參數傳遞給wait系統調用。 如果通過,它將返回流程的實際退出狀態。

要了解有關等待的更多信息,請閱讀以下鏈接。

http://man7.org/linux/man-pages/man2/wait.2.html

暫無
暫無

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

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