Sign in to follow this  

Waiting until the parent exits

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a utility to update the files for my project from a web server, I call it the "updater" (clever, huh?). It downloads what it needs into a temp dir (since that may include a new version of itself). Then I have another little utility that moves all of those files into the main dir. My plan was to do a fork in the updater, have the child do a waitpid on the parent so it knows the parent is gone and it's safe to move all the files, and then do an execv to run the little utility that does the actual move. That way the move won't be trying to overwrite a running program. However from reading man pages, I discovered that waitpid can only wait on the process's child processes. It won't wait on any process. Makes sense I guess, but now I'm not sure what to do. How can I wait until the parent has exited before continuing? Is there some way I can get process info using the pid, so that way I can tell if the parent has exited? I have one more question: since I know the parent will be exiting before the child, do I need to do anything like giving the child a new parent? If I just let the parent exit, will the system do that automatically? One more thing, I'm writing this in linux, although I need it to run in windows as well (although I know I'll need to do some #ifdef'ing). Thanks for your help.

Share this post


Link to post
Share on other sites
Ok, I have an idea, although I haven't tried it since I just got up and need to go to work. I read in another man page that when the parent terminates, the child's parent is changed to a system process. I could check the parent's pid (using getppid) as save that to a variable. Then I could go into a loop where I sleep for half a second, and then check the parent's pid. When it no longer equals the local copy that I have, I know the parent has terminated and the system changed the child's parent value. What do you think?

Share this post


Link to post
Share on other sites
Hi,

I'm right know learning fork stuff for curse I take. A fork is the same program as the parent (except that it "knows" it's forked). So it will use the same file the parent, should it not?
So your problem would remain. Sorry but I don't have an answer to your problem so far.

Share this post


Link to post
Share on other sites
This is my understanding based on reading man pages and courses I took several years ago. It's still the same executable, although it doesn't inherit file descriptors. After I do an execv in the child, that replaces the current program with the new one, which (I hope) allows me to overwrite the old (ie parent's) executable. Is that true? Does that make sense? I haven't gotten far enough to try that yet, since I have this waiting on the parent issue.

Share this post


Link to post
Share on other sites

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this