简体   繁体   中英

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(). 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. 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? 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.

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.

this might be due to how you are trying to check errno, here is an explanation from the errno man page:

   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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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