简体   繁体   English

检查进程是否正在运行的问题

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

I am required to create processes that fork into child processes that would execute commands using execv().我需要创建分叉到将使用 execv() 执行命令的子进程的进程。 Parent process will end immediately leaving the child process to run the new image.父进程将立即结束,让子进程运行新映像。 I have stored the child process id and when a command is called, I will check if that process is running or not.我已经存储了子进程 ID,当调用命令时,我将检查该进程是否正在运行。 Here's the code.这是代码。

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;
        }
    }

However, it seems like the processes do not end and are running forever.然而,这些进程似乎并没有结束,而是永远运行着。 Do execv() image not end the process upon completion? execv() 图像在完成后不会结束进程吗? Is there something wrong with the code?代码有问题吗?

You have said that you are checking if the child process is running, but you do not elaborate if and when it should end.您已经说过您正在检查子进程是否正在运行,但您没有详细说明它是否以及何时应该结束。

it would be nice to get more information or to look at the code that the executed process is running, and the processArr struct.获得更多信息或查看执行的进程正在运行的代码和 processArr 结构会很好。

a few things you can do if you are confident that process should end is to try and remove the and errno check in the if statement and see if it changes the result.如果您确信进程应该结束,您可以做的一些事情是尝试删除 if 语句中的 和 errno 检查并查看它是否会更改结果。

this might be due to how you are trying to check errno, here is an explanation from the errno man page:这可能是由于您尝试检查 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 == ...) { ... }
       }

hopefully it will help, and if not I would appreciate additional information.希望它会有所帮助,如果没有,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM