Re: How to create process independent of current process AND that takes command line arguments?
Re: How to create process independent of current process AND that takes command line arguments?
- Subject: Re: How to create process independent of current process AND that takes command line arguments?
- From: Jonathan Prescott <email@hidden>
- Date: Sat, 5 Apr 2008 22:59:45 -0400
fork() is the basic process by which a Unix system (IRIX, Solaris,
Darwin/MacOSX, AIX, etc.), and Linux systems spawn new processes (not
threads). exec() and its variants are the way to cause a new
executable image to be started in either the old or new process. The
only concern the OP has to deal with is zombie processes (child
processes who may exit before the parent does, and the parent does
not execute a wait() call to get the child's exit status). If you
are truly creating independent children, you would execute a call to
signal(SIGCLD,SIG_IGN) which tells the operating system the parent is
not interested in the exit status of its children. Typical sequence
of calls would be:
int pid;
... < Potential intervening code > ...
signal(SIGCLD,SIG_IGN);
... < Potential intervening code > ...
pid = fork();
if (pid < 0) {
/* fork call failed */
} else if (pid == 0) {
/* child process - do the child's thing */
execv(/* args */); /* if you want to start a new
executable */
} else {
/* this is the parent return from a successful fork. pid
contains the process ID of the child */
}
By setting the action of signal SIGCLD to IGNORE (SIG_IGN), the
parent is expressing no interest in the child's behavior, until the
signal action is reset to something else, or the parent exits.
If the parent exits before the child does, the child's parent is set
by the operating system to PID 1, which, for other Unix and Linux
systems is the init process, but, for Darwin and MacOSX, is launchd.
Every process in a Unix/Linux system has a parent, except for PID 1
(init/launchd).
Note that if the parent process stays around, it will remain the
parent of the child until the child exits, or the parent exits.
However, if you execute the signal() call above, and an execv() call
to load up a new executable, any causal link between the two
processes is broken, unless you set up a pipe, shared memory, shared
mem queues, etc., so, effectively, you have two independent
processes. If you re-enable the signal() call in the parent, you
will re-establish a causal link, and, you will have to execute a wait
() call in the parent and wait for the child to exit in order not to
create a zombie process.
The variants of the exec() call (exec(), execv(), etc.) all provide a
means to load and start an executable image, and pass it command line
parameters. This is how the shells (tcsh, bash, sh, csh, etc.)
execute programs, for example.
Jonathan
On Apr 5, 2008, at 1:40 AM, Scott Ribe wrote:
If
you fork and execv a process, then when the parent terminates, the
created process is automatically made a child of launchd.
Normally when a parent process terminates, all its children are
automatically terminated. The original question was how to launch a
process
from an app such that the process is not a child of the app, but
rather a
child of a higher-up process, whether a child of launchd or something
further down the chain like WindowServer was not really part of the
discussion. A corollary of course would be how to launch a child
such that
what you describe happens--termination of parent results in child
becoming
child of an ancestor further up the chain.
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden