15

I read about pipes and streams and I’m still confused on how it’s implemented.

A program is started and it reads data from “standard input” stream (stdin), which is where the keyboard sends data to.

My question is, how is that different from a pipe? Piping allows me to have a process that sends data to a pipe, and another process is reading data from it.

When the keyboard is pressed, data is sent to stdin and a program is reading data from this same stream.

A "read" operation is executed as soon as data is sent to this stream, just like a pipe.

Are these streams piped?

2 Answers2

10

Unix terminal i/o has traditionally been implemented as some sort of queue. Older kernels used clists. V8 Unix used streams. In most cases, clists and streams are used to implement a portion of the link between a user process (specifically, a file descriptor) and a character device driver (for example, a serial port or a pty).

Pipes are also a queue, but they link user processes (specifically, a pair of file descriptors). There are a variety of implementations of pipes, including sockets; a special type of file; and even STREAMS (STREAMS is a derivative of V8 streams.)

So, streams and pipes are both implementations of a queue, but they are used in different situations.

Mark Plotnick
  • 25,413
  • 3
  • 64
  • 82
8

This may not be completely accurate from a technical perspective, but may help your confusion. I tend to think of a stream as something intrinsic to the program, such as STDIN, STDOUT, and STDERR, whereas a pipe is external to the program. For example, in the command cat foo.txt | grep bar, the cat command sends to it's intrinsic STDOUT, the external pipe then connects that to grep's intrinsic STDIN.

John
  • 17,011
  • You say a stream is not external (like a pipe), but the connection between the program and the keyboard is being handled externally by a stream. The program does not read directly from the keyboard, it instead reads from an abstraction which is the stream. – user1091856 Jan 06 '15 at 14:43
  • But you don't have to do anything special to get that stream - it's just there automatically. That's the important point to me. Again, I may not have been completely technically accurate, but I'm trying to help clear up the confusion. – John Jan 06 '15 at 14:44