LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-19-2011, 05:08 PM   #1
bean street
LQ Newbie
 
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
waiting vs. axing child processes?


Hi everybody;

We are running a family of remote data collection devices (2.6.21 on ARM) that store data onto a USB memory stick. Because the write times are so long onto the stick, we have opted to have a child process handle the file copy so that the parent is unfettered in its quest to continue collecting geomagnetic data. And this has us at an odd tradeoff decision, (which I am sure the gurus here can answer).

We are using the traditional fork/execl and waitpid from the C API. New files are written to a ramdisk and need to be copied to memory stick every 12 minutes. File copy to memory stick takes ~2 minutes.

Choice 1 : child finishes file copy and exits, entering the defunct state. The parent then waits on the defunct child just before it forks another child. This means that the child is sitting in the defunct state for about 10 minutes before it is waited on.

Choice 2 : child finishes file copy and sleeps. The parent then axes the sleeping child just before it forks another child.

Other choices?

Anyone know which approach might give us the most stable system?

Thanks a lot!!
 
Old 12-19-2011, 09:23 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
The first choice -- letting the child stay in defunct state -- is perfectly okay. The kernel releases the resources used by the child, only keeping the process ID and exit status, so there is really no downsides for this.

You can call waitpid(childpid,&status,WNOHANG) every now and then to see if the child has exited yet. It will return childpid if the child has exited, 0 if it is still running, and -1 (and errno set) if an error has occurred. In your case, I don't think it is necessary -- as I said, it is perfectly okay to let the child stay defunct until just before the next fork.
 
Old 12-19-2011, 10:09 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
You are assuming that the normal timing of a 2 minute write every 12 minutes will always hold true. Suppose you have some kind of error condition where the write has not completed in 12 minutes. In that case your solution 1 will wait for the write to finish. If the write actually finishes, but late, then solution 1 will preserve your file integrity. If the write never finishes then solution 1 will hang your parent process.

Under the same error condition solution 2 will lose the data still waiting to be written and possibly might corrupt the file. But the parent process will not hang.

I suggest that you use solution 1 but test the wait condition instead of issuing an unconditional wait. If the test shows that the child process is not finished then treat it as an error condition.

---------------------
Steve Stites

Edit: After carefully rereading Nominal Animal's reply I think that his answer is the same as mine, just with different wording.

Last edited by jailbait; 12-19-2011 at 10:22 PM.
 
Old 12-20-2011, 03:17 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Yes, it is definitely a good idea to use waitpid(childpid,&status,WNOHANG) to reap the child process without waiting for it to exit. If sufficient time has passed to indicate the child has hung, kill the child via kill(childpid,SIGKILL); and reap it using waitpid(childpid,&status,0) .

If the application is critical, you can install a dummy SIGALRM signal handler (empty body), and set a timeout (using alarm(seconds)) before the waitpid() call; the alarm signal will interrupt the waitpid() call, which will then return (pid_t)-1 with errno set to EINTR. If the waitpid() call is successful, alarm(0) will defuse the timeout. This is extremely robust and reliable.

As to the other choices, you could always install a SIGCHLD handler, and use waitpid(childpid,&status,WNOHANG) in the signal handler -- waitpid() is async-signal-safe, thus okay to use in a signal handler -- to reap the child whenever it exits. It is a bit tricky to implement correctly, because the status must be accessed atomically (as other parts of the code might be midway reading the status just when the signal handler is triggered), and the child may exit at any time after the parent fork()s. In particular, you cannot assume that the main program can save the child PID (to be tried by the signal handler) before the SIGCHLD signal handler may be run; the signal handler must rely on the si_pid field in the siginfo_t structure, or you risk missing a signal. Considering the complexity, I would only use the signal approach in an asynchronous and/or multithreaded program.
 
  


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
Can a parent process proceed its execution without waiting a child to finish? pmil Linux - General 3 08-10-2010 03:23 AM
su processes child of pid 1? acid_kewpie Linux - Software 5 07-29-2009 01:22 PM
pls give suggestion for waiting the parent upto completing total execution in child nagendrar Programming 1 07-08-2009 02:16 AM
Minimised processes just disappear instead of waiting at the foot of the screen rrsc16954 Mandriva 5 05-22-2009 01:59 AM
waiting for a child process to finish execution in C cynthia_thomas Programming 2 05-26-2006 03:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:53 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