The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: arnold@skeeve.com
To: tuhs@minnie.tuhs.org, gtaylor@tnetconsulting.net
Subject: Re: [TUHS] First appearance of named pipes
Date: Wed, 11 Mar 2020 01:02:59 -0600	[thread overview]
Message-ID: <202003110702.02B72xqj023433@freefriends.org> (raw)
In-Reply-To: <4fb5b7ab-03a4-df64-d25b-8be587b6a17b@spamtrap.tnetconsulting.net>

Hi Grant.

Grant Taylor via TUHS <tuhs@minnie.tuhs.org> wrote:

> The larger more onerous question is could I leverage exec to alter where 
> file descriptors 0 (STDIN), 1 (STDOUT), and 2 (STDERR) are set to, 
> including changing 1 to the value of a FIFO, and 0 of a subsequent 
> command to also be the value of the FIFO, thus have pipe like behavior 
> between two commands without using a pipe or redirection as in ">".

There's nothing preventing you from doing that. After the fork() and
before the exec(), just close() and dup() the relevant fds in the right
order and you're set.

> This has also gotten me to wonder about the possibility of having 
> multiple commands output to a file descriptor; 1 / 2 / other, that is 
> input to a separate command.  Sort of the opposite of tee, in a manner 
> of speaking.  I'll try to articulate:
>
> $ mkfifo test.fifo
> $ exec 3>&1
> $ exec 1> test.fifo
> $ for l in {a..z}; do echo $l; sleep 1; done &
> $ for L in {A..Z}; do echo $L; sleep 1; done &
> $ for n in {1..100}; do echo $n; sleep 1; done &
> $ exec 1>&3
> $ cat test.fifo

I don't think that this is any different from:

	(for l in {a..z}; do echo $l; sleep 1; done &
	 for L in {A..Z}; do echo $L; sleep 1; done &
	 for n in {1..100}; do echo $n; sleep 1; done &) | cat

which reduces to:

	(for l in {a..z}; do echo $l; sleep 1; done &
	 for L in {A..Z}; do echo $L; sleep 1; done &
	 for n in {1..100}; do echo $n; sleep 1; done &) > /some/file

(You might want to background that whole mess given that the final
pipeline will sleep for 100 seconds.)

> This seems special to me in that I have three processes (for loops) 
> writing into what is effectively the same pipe.

It's not any different than calling `stty -tostop' and then simply
backgrounding the three loops at the terminal.  Try it!

(This is the beauty of the Unix model, an fd is just a data sink,
we don't care where it goes.)

> I would be very eager to learn from anyone who is willing to teach me 
> pointers.  :-)

At the cost of tooting my own horn, I recommend my book "Linux Programming
by Example: The Fundamentals", which, despite the "Linux" in the title,
covers basic Unix programming, including file descriptor manipulation
of the sort under discussion here.

HTH,

Arnold

  reply	other threads:[~2020-03-11  7:03 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-06 22:44 Noel Chiappa
2020-03-07 12:17 ` Paul Ruizendaal
2020-03-07 13:29   ` Clem Cole
2020-03-07 16:39   ` Derek Fawcus
2020-03-08  2:36     ` Rob Pike
2020-03-08  2:47       ` Larry McVoy
2020-03-08 13:07         ` Ralph Corderoy
2020-03-08 13:25           ` arnold
2020-03-08  3:06       ` Dave Horsfall
2020-03-08  7:16       ` arnold
2020-03-08 15:13       ` [TUHS] sockets (was Re: First appearance of named pipes) Derek Fawcus
2020-03-09 23:22         ` Grant Taylor via TUHS
2020-03-09 23:44           ` Larry McVoy
2020-03-23  8:49           ` Peter Pentchev
2020-03-24  9:47             ` Derek Fawcus
2020-03-25 23:25               ` Grant Taylor via TUHS
2020-03-25 23:47                 ` Richard Salz
2020-03-26  0:11                   ` Grant Taylor via TUHS
2020-03-26  0:18                     ` Richard Salz
2020-03-26  1:08                       ` Grant Taylor via TUHS
2020-03-26  2:38                         ` Dave Horsfall
2020-03-26  3:08                           ` Rob Pike
2020-03-26  3:43                             ` George Michaelson
2020-03-26  4:11                           ` Grant Taylor via TUHS
2020-03-28 11:12                           ` Peter Pentchev
2020-03-28 11:14                             ` Peter Pentchev
2020-03-28 16:03                               ` Steffen Nurpmeso
2020-03-26  1:20                       ` Tony Finch
2020-03-27 10:51                   ` Derek Fawcus
2020-03-09 23:22 ` [TUHS] First appearance of named pipes Grant Taylor via TUHS
2020-03-10  7:29   ` arnold
2020-03-11  2:47     ` Grant Taylor via TUHS
2020-03-11  7:02       ` arnold [this message]
2020-03-10 13:49   ` Chet Ramey
2020-03-10 20:26     ` Dave Horsfall
2020-03-10 20:37       ` Chet Ramey
2020-03-11  2:51       ` Grant Taylor via TUHS
  -- strict thread matches above, loose matches on Subject: below --
2020-03-08 21:42 Paul Ruizendaal
2020-03-08 22:04 ` Jon Steinhart
2020-03-06 20:41 Paul Ruizendaal
2020-03-06 21:06 ` Clem Cole
2020-03-06 21:10   ` Clem Cole
2020-03-07  5:08     ` Heinz Lycklama

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202003110702.02B72xqj023433@freefriends.org \
    --to=arnold@skeeve.com \
    --cc=gtaylor@tnetconsulting.net \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).