[SOLVED] Re: continuing after fork() (Re: Problems with NSTimer and NSRunLoop/CFRunLoop)
[SOLVED] Re: continuing after fork() (Re: Problems with NSTimer and NSRunLoop/CFRunLoop)
- Subject: [SOLVED] Re: continuing after fork() (Re: Problems with NSTimer and NSRunLoop/CFRunLoop)
- From: Mont Rothstein <email@hidden>
- Date: Fri, 14 Jan 2005 00:47:04 -0800
For anyone that comes across this problem execvc() did indeed fix the problem (thanks Chris).
Don't forget that if you modify the command line arguments (to flag that the process is a child and shouldn't re-fork) you will want to call
[NSUserDefaults resetStandardUserDefaults]; so that it re-parses the arguments.
-Mont
On Jan 13, 2005, at 11:21 AM, Chris Kane wrote:
On Jan 12, 2005, at 10:56 PM, Mont Rothstein wrote:
Thank you Chris for the detailed and informative reply, this problem has been haunting me for some time.
If you don't mind I'd like to clarify my interpretation of your response to make sure I have a handle on this.
First, since my last post I have determined that certain frameworks I was including cause my child to fail. Without those frameworks the child runs fine.
If I understand you correctly then the fact that without those frameworks the child works is luck, because in both cases the app links against Apple frameworks. I was headed down a path of attempting to remove the dependency on the problem frameworks (my previous removal was only a test and was at the expense of functionality). Given your description I believe I should abandon that course and instead use execve() because even the working version is at risk of failing.
That would be best. Though it is kind of a pain if you have to refactor the code, though most things I've seen do the fork() in main(), so they haven't gone too far in to execution.
Also, I am unclear from the man page on execve(). Will my parent process still receive the exit from the child? And it sounds like I need a second main() with (argc, argv, envp) that will be called by execve().
Yes. fork + execve is the typical operation for creating new processes, so that all works fine.
You don't need a second main(), you just augment your existing main() to look at the argument list or environment list (argv list is probably easier to work with) for a special argument/environment variable that means "this is the child process", and continue down a different code that that does not include reforking (or, set a boolean variable that the forking code later looks and and skips the fork and does the child thing instead).
Just declare main() like this if it isn't already: int main(int argc, const char *argv[])
and iterate over argv using strcmp() to compare each argument to your special one.
Creating the enhanced argument list to pass to execve() I'll let you work out. Just pass 'environ' for the environment argument.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden