Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

You will lose an important tape file.


devel / comp.unix.programmer / Re: a problem on pipes: making the child run less and parent writes to it

SubjectAuthor
* a problem on pipes: making the child run less and parent writes to itMeredith Montgomery
+- Re: a problem on pipes: making the child run less and parent writes to itSiri Cruise
+* Re: a problem on pipes: making the child run less and parent writesLew Pitcher
|+* Re: a problem on pipes: making the child run less and parent writes to itWayne Harris
||`- Re: a problem on pipes: making the child run less and parent writesLew Pitcher
|`* Re: a problem on pipes: making the child run less and parent writesLew Pitcher
| +* Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat
| |`- Re: a problem on pipes: making the child run less and parent writesLew Pitcher
| `* Re: a problem on pipes: making the child run less and parent writes to itMeredith Montgomery
|  +- Re: a problem on pipes: making the child run less and parent writes to itScott Lurndal
|  `* Re: a problem on pipes: making the child run less and parent writesLew Pitcher
|   `* Re: a problem on pipes: making the child run less and parent writes to itRalf Fassel
|    +- Re: a problem on pipes: making the child run less and parent writes to itScott Lurndal
|    `* Re: a problem on pipes: making the child run less and parent writesLew Pitcher
|     `* Re: a problem on pipes: making the child run less and parent writes to itScott Lurndal
|      `* Re: a problem on pipes: making the child run less and parent writesLew Pitcher
|       `* Re: a problem on pipes: making the child run less and parent writes to itScott Lurndal
|        `* Re: a problem on pipes: making the child run less and parent writes to itMeredith Montgomery
|         `- Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat
`* Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat
 +* Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat
 |`* Re: a problem on pipes: making the child run less and parent writesGeoff Clare
 | `* Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat
 |  `- Re: a problem on pipes: making the child run less and parent writesGeoff Clare
 `* Re: a problem on pipes: making the child run less and parent writes to itWayne Harris
  `- Re: a problem on pipes: making the child run less and parent writes to itRainer Weikusat

Pages:12
Re: a problem on pipes: making the child run less and parent writes to it

<877d8x1swn.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: a problem on pipes: making the child run less and parent writes to it
Date: Sun, 13 Mar 2022 20:47:36 +0000
Lines: 94
Message-ID: <877d8x1swn.fsf@doppelsaurus.mobileactivedefense.com>
References: <86a6ijvro4.fsf@levado.to> <sm17gg$tp4$1@dont-email.me>
<sm9asb$g1o$2@dont-email.me> <86o82o8gbm.fsf@levado.to>
<svon2m$jba$3@dont-email.me> <ygapmn35rdc.fsf@akutech.de>
<svqlcs$tmm$1@dont-email.me> <tY5UJ.94353$Gojc.29821@fx99.iad>
<svqriq$tmm$3@dont-email.me> <5K6UJ.233963$Rza5.136066@fx47.iad>
<864k43p75i.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net ggiCRBiHK9FqmZyOcz/c+gJMtWXBS8tyC0F/kDyMfxwzTXfu4=
Cancel-Lock: sha1:ZSPF0h5eyF/xtXT2UsYqQlrkkug= sha1:0cqzFnPNiNE9Qg4i6FH1Xktk5cQ=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)
 by: Rainer Weikusat - Sun, 13 Mar 2022 20:47 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

[...]

> My exercise, therefore, needs another fork if I intend for the parent
> process to exec cat because cat won't wait for the child and so the
> child might lose its parent and, therefore, losing rights to talk to the
> terminal. Forking again before running cat allows the parent to wait
> for both cat and less, keeping both programs with the right to talk to
> the terminal. So it seems I may conclude that for a situation such as
>
> cat --> less
>
> we really need three processes, one for cat, one for less and one parent
> for waiting for both to avoid any of them from losing the right to talk
> to the terminal (assuming any of them might try to). In general, a
> pipeline of n processes requires at least n+1 processes.

This happens to be true when creating pipelines from the shell as
there's always a shell process involved. But in general, it's not
true. You're just creating the pipeline in the wrong way. There are 3
kinds of processes in a pipeline

1. The first process if there's more than one. Stdin of the first
process refers to the terminal and stdout refers to a pipe.

2. n intermediate processes, n >= 0. Stdin of an intermediate process is
the most recently create pipe. Stdout is a newly created pipe.

3. The last process reads from the most recently created pipe if there's
more than one process, otherwise from the terminal. It writes to the
terminal.

Assuming there are only well-behaved commands in the pipeline, ie
commands which actually process all of their stdin input, the initial
process needs to run the last command to ensure that everythings works
as it should:

Example:

----
#include <unistd.h>

static void exec_cmd(int in_fd, char *cmd)
{ if (in_fd != -1) {
dup2(in_fd, 0);
close(in_fd);
}

execlp("sh", "/bin/sh", "-c", cmd, (void *)0);
}

static int start_cmd(int in_fd, char *cmd)
{ int fds[2];

pipe(fds);

if (fork()) {
close(in_fd);
close(fds[1]);

return *fds;
}

dup2(fds[1], 1);
close(*fds);
close(fds[1]);

exec_cmd(in_fd, cmd);
}

int main(int argc, char **argv)
{ char *cmd, *next;
int fd;

fd = -1;
++argv;

cmd = *argv;
while (next = *++argv) {
fd = start_cmd(fd, cmd);
cmd = next;
}

exec_cmd(fd, cmd);
} ----

This shell commands as arguments an runs them in a pipeline, eg

../a.out 'cat /var/log/syslog' 'sed "s/Mar/Ram/"' less

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor