簡體   English   中英

檢查進程是否正在運行的問題

[英]Problem with checking if process is running or not

我需要創建分叉到將使用 execv() 執行命令的子進程的進程。 父進程將立即結束,讓子進程運行新映像。 我已經存儲了子進程 ID,當調用命令時,我將檢查該進程是否正在運行。 這是代碼。

for(size_t i = 0; i < currProcess; i++) {
        if (kill(processArr.pid, 0) != 0 && errno == ESRCH) {
            processArr[i].run = false;
        } else {
            processArr[i].run = true;
        }
    }

然而,這些進程似乎並沒有結束,而是永遠運行着。 execv() 圖像在完成后不會結束進程嗎? 代碼有問題嗎?

您已經說過您正在檢查子進程是否正在運行,但您沒有詳細說明它是否以及何時應該結束。

獲得更多信息或查看執行的進程正在運行的代碼和 processArr 結構會很好。

如果您確信進程應該結束,您可以做的一些事情是嘗試刪除 if 語句中的 和 errno 檢查並查看它是否會更改結果。

這可能是由於您嘗試檢查 errno 的方式,這里是 errno 手冊頁的解釋:

   A common mistake is to do

       if (somecall() == -1) {
           printf("somecall() failed\n");
           if (errno == ...) { ... }
       }

   where errno no longer needs to have the value it had upon return from
   somecall() (i.e., it may have been changed by the printf(3)).  If the
   value of errno should be preserved across a library call, it must be
   saved:

       if (somecall() == -1) {
           int errsv = errno;
           printf("somecall() failed\n");
           if (errsv == ...) { ... }
       }

希望它會有所幫助,如果沒有,我將不勝感激。

暫無
暫無

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

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