Deutsch   English   Français   Italiano  
<20241112131933.158@kylheku.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Tue, 12 Nov 2024 21:24:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20241112131933.158@kylheku.com>
References: <vgl8h1$385vs$1@dont-email.me>
Injection-Date: Tue, 12 Nov 2024 22:24:53 +0100 (CET)
Injection-Info: dont-email.me; posting-host="ae3b77ee73e9050342ac300cdc53a8dc";
	logging-data="1888656"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18fH91me+NNnAQbLIbN0NdYtz7362IDPWU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:bFRz3iPbLlr/yqa2ORzVxmrO2VM=
Bytes: 2294

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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca