Autor: ibid.ag Data: Para: Edward Bartolo CC: dng Assunto: Re: [DNG] Doing away with multi-threading in my project (netman)
On Sat, Sep 05, 2015 at 08:52:39PM +0100, Edward Bartolo wrote: > Quote: <<An 'orphaned process' is one whose parent has exited after creating a
> process (that's done via fork and not via execve) and it's not adopted
> by the grand parent but by init.>>
>
> So what is the difference between an "orphaned process", i.e. a
> process whose parent has exited after giving birth to a child process
> and one which replaces the original process effectively giving its
> parent a death sentence?
>
> Sorry, but it looks there is only a very subtle difference between the
> two. The only difference I see, is that, since in the case of execl,
> the parent effectively is replaced, i.e. dies, it is also awarded its
> parent PID. This is not the case when a parent process creates a child
> process and exits immediately, as the child will have a different PID.
A program that runs via execlp()--or any of the exec*() functions--
*is* the process that called execlp().
It does not kill the *process*, though it effectively ends the
*program*.
As a result, it has the same parent as the previous program.
(This is why you can run "exec killall forkbomb" to kill a forkbomb,
at the expense of one shell.)
To create a *new* process, you use fork(), vfork(), posix_spawn(),
popen(), or system().
In C, system() is the recommended, portable way to run programs; it
forks, execs "sh -c COMMAND", waits, and returns the exit status.
popen() is the recommended approach if you want to use a stream.
It creates a pipe, forks, and runs "sh -c COMMAND" in the child.
The parent then returns a FILE pointer to the stream.
Once you have done the desired IO on the stream, you can call
pclose(), which will take care of closing the pipe, waiting for the
child, and finally return the child status.
If you want to avoid zombies simply, just use popen() and pclose().