繁体   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