LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2006, 10:10 AM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question 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.
 
Old 04-29-2006, 10:45 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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>

Last edited by Hko; 04-29-2006 at 10:55 AM.
 
Old 04-29-2006, 11:49 AM   #3
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Got it working, thanks for your input!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
child process -1 exited with status 1 ashfaq Linux - Software 3 04-09-2004 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration