Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"I DO want your money, because god wants your money!" -- The Reverend Jimmy, from _Repo_Man_


devel / comp.unix.programmer / a pipe exercise

SubjectAuthor
* a pipe exerciseMeredith Montgomery
+* Re: a pipe exerciseScott Lurndal
|`- Re: a pipe exerciseLew Pitcher
`- Re: a pipe exerciseLew Pitcher

1
a pipe exercise

<868rts8d1w.fsf@levado.to>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17023&group=comp.unix.programmer#17023

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!MFZx1yx7Oe4QZxzA8GQPrg.user.46.165.242.91.POSTED!not-for-mail
From: mmontgomery@levado.to (Meredith Montgomery)
Newsgroups: comp.unix.programmer
Subject: a pipe exercise
Date: Wed, 02 Mar 2022 14:44:11 -0300
Organization: Aioe.org NNTP Server
Message-ID: <868rts8d1w.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="15341"; posting-host="MFZx1yx7Oe4QZxzA8GQPrg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:Da3Yfpk2LgxB6CAqigtph8cpnKE=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Wed, 2 Mar 2022 17:44 UTC

I took Lew Pitcher's program and gave myself the exercise of just
switching parent and child's role. Briefly, Lew Pitcher's program forks
and the child writes data to the parent, which executes less to page the
output. What you find below is my own writing of that program (with the
roles switched) --- so all errors are mine, of course.

I think this program is wrong because the parent doesn't wait for the
child, so the child finishes ``quickly'' and must be left in a zombie
state until less exits. When less exits, it does so without calling
wait(), so the child ends up being wait()ed by the init program. Things
might end up well, but it's a totally flawed design.

That's my understanding. I appreciate if you correct any
misunderstandings here. Thank you.

(*) Full program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{ int pfd[2]; int r;

r = pipe(pfd); if (r < 0) { perror("pipe"); exit(1); }
r = fork(); if (r < 0) { perror("fork"); exit(2); }

if (r == 0) {
close(pfd[0]);
r = dup2(pfd[1], STDOUT_FILENO); if (r < 0) { perror("dup2 child"); exit(4); }
close(pfd[1]);
for (int c = 1; c <= 100; ++c)
printf("Line %d\n", c);
fclose(stdout);
exit(0);
}

close(pfd[1]);
r = dup2(pfd[0], STDIN_FILENO); if (r < 0) { perror("dup2 parent"); exit(3); }
close(pfd[0]);
execl("/usr/bin/less","less",NULL);
fprintf(stderr, "%s: execl() failed - %s\n", argv[0], strerror(errno));

// who's gonna wait() for the child?
exit(0);
}

Re: a pipe exercise

<yIOTJ.47116$m1S7.16528@fx36.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17025&group=comp.unix.programmer#17025

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx36.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: a pipe exercise
Newsgroups: comp.unix.programmer
References: <868rts8d1w.fsf@levado.to>
Lines: 13
Message-ID: <yIOTJ.47116$m1S7.16528@fx36.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 02 Mar 2022 18:29:50 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 02 Mar 2022 18:29:50 GMT
X-Received-Bytes: 1375
 by: Scott Lurndal - Wed, 2 Mar 2022 18:29 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>I took Lew Pitcher's program and gave myself the exercise of just
>switching parent and child's role. Briefly, Lew Pitcher's program forks
>and the child writes data to the parent, which executes less to page the
>output. What you find below is my own writing of that program (with the
>roles switched) --- so all errors are mine, of course.
>

Keep in mind that there is no guarantee as to whether the child
process runs before the parent, or that the parent runs before the
child, or that they don't run _at the same time_ (e.g. on a multicore
processor). In this case, the child could have run to completion before
the parent returns from the fork() system call.

Re: a pipe exercise

<svok52$jba$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17026&group=comp.unix.programmer#17026

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: a pipe exercise
Date: Wed, 2 Mar 2022 20:30:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <svok52$jba$1@dont-email.me>
References: <868rts8d1w.fsf@levado.to> <yIOTJ.47116$m1S7.16528@fx36.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 2 Mar 2022 20:30:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8613ade514610477fe71534f6577e731";
logging-data="19818"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uNwuAFs26x2GRyCzwkA8dHbAc2vn3sg8="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:kCYRGXzrFk0nKsu9Vr2tw6RhJ48=
 by: Lew Pitcher - Wed, 2 Mar 2022 20:30 UTC

On Wed, 02 Mar 2022 18:29:50 +0000, Scott Lurndal wrote:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>>I took Lew Pitcher's program and gave myself the exercise of just
>>switching parent and child's role. Briefly, Lew Pitcher's program forks
>>and the child writes data to the parent, which executes less to page the
>>output. What you find below is my own writing of that program (with the
>>roles switched) --- so all errors are mine, of course.
>>
>
> Keep in mind that there is no guarantee as to whether the child
> process runs before the parent, or that the parent runs before the
> child, or that they don't run _at the same time_ (e.g. on a multicore
> processor). In this case, the child could have run to completion before
> the parent returns from the fork() system call.

If I understand correctly, /that/ situation should result in a pipe
that contains all the data from the child process, and (other than the
write fd in the parent process) no open write fds.

So long as the parent process closes that lone pipe write fd (which
it does, in Meredith's code) the reader should properly receive EOF
once it has read the entire contents of the (buffered) pipe.

--
Lew Pitcher
"In Skills, We Trust"

Re: a pipe exercise

<svoku8$jba$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17027&group=comp.unix.programmer#17027

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: a pipe exercise
Date: Wed, 2 Mar 2022 20:43:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <svoku8$jba$2@dont-email.me>
References: <868rts8d1w.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 2 Mar 2022 20:43:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8613ade514610477fe71534f6577e731";
logging-data="19818"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WzSqxICUtPyjc8zBY8pvuu3Zsvg9diVo="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:YdENKTDWQ4Nb4wlS4zxyIxX20Rg=
 by: Lew Pitcher - Wed, 2 Mar 2022 20:43 UTC

On Wed, 02 Mar 2022 14:44:11 -0300, Meredith Montgomery wrote:

> I took Lew Pitcher's program and gave myself the exercise of just
> switching parent and child's role. Briefly, Lew Pitcher's program forks
> and the child writes data to the parent, which executes less to page the
> output. What you find below is my own writing of that program (with the
> roles switched) --- so all errors are mine, of course.

It looks like a reasonable test program to me. I walked through it and
didn't see any obvious problems.

> I think this program is wrong because the parent doesn't wait for the
> child, so the child finishes ``quickly'' and must be left in a zombie
> state until less exits. When less exits, it does so without calling
> wait(), so the child ends up being wait()ed by the init program. Things
> might end up well, but it's a totally flawed design.

Perhaps not a CYA thorough design, but I wouldn't say "totally flawed".
It /would/ be better to have the parent wait() for the child process, but
since it doesn't, that child process (if it hasn't terminated) becomes an
orphan process, not a zombie. (It would only be a zombie during the lifetime
of the parent process.)

As you alluded to, init(8) is specifically tasked with cleaning up the
status of orphaned child processes. So long as the parent process exits
in a timely manner (and yours does, more or less) there's no need to worry.
Or, to put it another way, so long as the zombie doesn't live /too/ long,
and there aren't /too/ many zombies, the system will live on.

Remember, zombie processes starve the system by holding on to a limited
resource: their place in the system's process state table. The more zombies,
the fewer live processes can run. But, zombie processes are tolerable, so
long as they don't live long enough to impact performance. They may be
undesirable, and (consequently) a sign of "bad design", but so long as they
/do/ die in a reasonable time, you can get away with them.

> That's my understanding. I appreciate if you correct any
> misunderstandings here. Thank you.

[snip code]

--
Lew Pitcher
"In Skills, We Trust"

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor