Sujet : Re: Why does getppid() still return old parent pid after setsid()?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.unix.programmerDate : 12. Nov 2024, 22:24:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20241112131933.158@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-11-08,
Muttley@DastartdlyHQ.org <
Muttley@DastartdlyHQ.org> wrote:
I've tried this code on both MacOS and Linux yet the child process getppid()
still returns its original parent process instead of "1" for init which is
what I'd expect. Isn't setsid() supposed to completely detach the child or
have I misunderstood?
No; it's because you put a 1 second delay into the parent to keep it
alive, whereas the child calls getppid() without any such timed
scheduling delay.
Because 1 second is an eternity, you've practically guaranteed that the
child will get the original parent pid. The only thing that would make
it air tight would be if the parent waited for the child's termination
instead of sleeping.
setsid has to do with the TTY session, and manipulates the group ID.
If you read descriptions of it carefully, you will see that it has
nothing to do with detaching from a parent process.
To detach from a parent process, do a double fork to create a
grandchild. Have the child quit. Then the grandchild will be orphaned
and reparented to some process (typically PID 1). The timing of
that is "racy"; it has to do with when the intermediate child
terminates, until which time, the grandchild has that process as its
parent PID.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca