LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting a child process status (C) (https://www.linuxquestions.org/questions/programming-9/getting-a-child-process-status-c-440007/)

smoothdogg00 04-29-2006 10:10 AM

Getting a child process status (C)
 
I am creating a simple shell, in which I can run a process in the background using the '&' option. I am starting a process and calling waitpid(pid, status, WNOHANG), which allows it to continuously check to see if the process is complete. Now, the problem I am facing is printing a message when a child process completes.

Is there any way to use waitpid() to report back when a process completes? Or is there a function to get the list of running processes and somehow store those to continuously check to see if a process has finished?

Or, does anyone have any other suggestions as to how I should go about this?

Thank you.

Hko 04-29-2006 10:45 AM

Quote:

Originally Posted by smoothdogg00
I am starting a process and calling waitpid(pid, status, WNOHANG), which allows it to continuously check to see if the process is complete. Now, the problem I am facing is printing a message when a child process completes.

Is there any way to use waitpid() to report back when a process completes?

I don't understand what the problem is. What you're asking for is exactly what waitpid() does: after waitpid(pid, status, WNOHANG), the variable status can tell you if the process (with pid) stopped, dumped core, or exited.

Please read "man waitpid" for more information on how to check if it exited.

<edit>
I noted that you call waitpid() passing the variable status without taking it address with &. This is not necessarily a problem if status is a pointer to an int, e.g. something like:
Code:

int st;
int *status;
status = &st;
waitpid(pid, status, WNOHANG);

If you have something like the above, it's OK. But make sure you check stat instead of status to see if the process exited.
</edit>

smoothdogg00 04-29-2006 11:49 AM

Got it working, thanks for your input!


All times are GMT -5. The time now is 05:06 PM.