Pipes: Difference between revisions
pc>Yuron No edit summary |
![]() ![]() ![]() m (1 revision imported) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
-->{{Path|IPC|6}}<!-- | -->{{Path|IPC|6}}<!-- | ||
-->{{#invoke:Dependencies|add|Queues,3|Streams,3}} | -->{{#invoke:Dependencies|add|Queues,3|Streams,3}} | ||
In [https://en.wikipedia.org/wiki/Unix Unix] (and similar operating systems) a | In [https://en.wikipedia.org/wiki/Unix Unix] (and similar operating systems) a ‘[https://en.wikipedia.org/wiki/Pipeline_%28Unix%29 pipe]’ is a means of passing a <em>serial stream</em> of data from one process to another. A pipe is a [[Queues|FIFO queue]] with two interfaces – one for inserting data and one for removing them. | ||
‘[https://en.wikipedia.org/wiki/Pipeline_%28Unix%29 pipe]’ is a means of passing a <em>serial stream</em> of data from one process to another. A pipe is a [[Queues|FIFO queue]] with two interfaces – one for inserting data and one for removing them. | |||
Pipes can be created to link processes, as is illustrated in this | Pipes can be created to link processes, as is illustrated in this [[Exercises:Pipes|exercise]]. | ||
[[Exercises:Pipes|exercise]]. | |||
A typical use for pipes is as a <em>redirection</em> of standard I/O streams, | A typical use for pipes is as a <em>redirection</em> of standard I/O streams, often invoked from a [[shell]]. As a random sort of example, find how many filenames in the current directory contain the letter ‘a’: | ||
often invoked from a [[shell]]. As a random sort of example, | |||
find how many filenames in the current directory contain the letter | |||
‘a’: | |||
<code>ls | grep a | wc</code> | <code>ls | grep a | wc</code> | ||
[[Image:pipe_example.png|link=|alt=Pipe example]] | [[Image:pipe_example.png|link=|alt=Pipe example]] | ||
In an example like this (or, maybe, one which generates more traffic) | In an example like this (or, maybe, one which generates more traffic) the three child processes run independently, blocking if and when they are unable to input or output due to a pipe becoming empty or full or, possibly, timeslicing at other times. | ||
the three child processes run independently, blocking if and when they | |||
are unable to input or output due to a pipe becoming empty or full or, | |||
possibly, timeslicing at other times. | |||
---- | ---- | ||
== Named pipes == | == Named pipes == | ||
It is sometimes said of Unix “[[ | It is sometimes said of Unix “[[Everything is a File|Everything looks like a file]]”. Whilst not <em>entirely</em> true, lots of things <em>can</em> look like files – including pipes. Like a file, these need a name. | ||
file]]”. Whilst not <em>entirely</em> true, lots of | |||
things <em>can</em> look like files – including pipes. Like a file, these | |||
need a name. | |||
Try this <strong>experiment</strong>: | Try this <strong>experiment</strong>: | ||
Line 39: | Line 28: | ||
*When finished, try to remember to <code>rm FIFO</code> | *When finished, try to remember to <code>rm FIFO</code> | ||
Such a named pipe can also be created and used from within software | Such a named pipe can also be created and used from within software etc. Note that (unlike a file) the <code>open</code> call can block; opening one end of the pipe will block the opening process until the other end is open too. In Unix see <code>man 3 mkfifo</code> for the C language interface, for example. | ||
etc. Note that (unlike a file) the <code>open</code> call can block; opening one | |||
end of the pipe will block the opening process until the other end is | |||
open too. In Unix see <code>man 3 mkfifo</code> for the C language interface, | |||
for example. | |||
Similar mechanisms are available in Windows et alia, although the | Similar mechanisms are available in Windows et alia, although the <em>details</em> of the interface are different. | ||
<em>details</em> of the interface are different. | |||
---- | ---- | ||
==== Standard Streams ==== | ==== Standard Streams ==== | ||
Unix processes have three standard I/O '''[[streams]]''', | Unix processes have three standard I/O '''[[streams]]''', created when the process is started: | ||
created when the process is started: | |||
* stdin - the default input: typically the keyboard | * stdin - the default input: typically the keyboard | ||
Line 57: | Line 40: | ||
* stderr - the diagnostic output: typically also the display (window) | * stderr - the diagnostic output: typically also the display (window) | ||
A process will inherit its streams from a parent, so, for example, a | A process will inherit its streams from a parent, so, for example, a process started by a [[Shell]] will run ‘within’ the | ||
process started by a [Shell | |||
shell window unless instructed otherwise. | shell window unless instructed otherwise. | ||
<code>stdin</code> and <code>stdout</code> can conveniently be redirected. One application is | <code>stdin</code> and <code>stdout</code> can conveniently be redirected. One application is to use pipes to connect one process to another. | ||
to use pipes to connect one process to another. | |||
---- | ---- | ||
[[Image:MagrittePipe.png|link=https://en.wikipedia.org/wiki/The_Treachery_of_Images|alt=Magritte Pipe]] | [[Image:MagrittePipe.png|link=https://en.wikipedia.org/wiki/The_Treachery_of_Images|alt=Magritte Pipe]] | ||
---- | ---- | ||
{{BookChapter|3.7.4|139-145}} | |||
{{PageGraph}} | {{PageGraph}} | ||
{{Category|IO}} | {{Category|IO}} | ||
{{Category|Processes}} | {{Category|Processes}} | ||
{{Category|User}} | {{Category|User}} |
Latest revision as of 10:03, 5 August 2019
On path: IPC | 1: Processes • 2: Interprocess Communication • 3: Shared Memory • 4: Files • 5: Unix Signals • 6: Pipes • 7: Sockets • 8: Synchronisation • 9: Synchronisation Barrier • 10: Atomicity • 11: Mutual exclusion • 12: Signal and Wait |
---|
Depends on | Queues • Streams |
---|
In Unix (and similar operating systems) a ‘pipe’ is a means of passing a serial stream of data from one process to another. A pipe is a FIFO queue with two interfaces – one for inserting data and one for removing them.
Pipes can be created to link processes, as is illustrated in this exercise.
A typical use for pipes is as a redirection of standard I/O streams, often invoked from a shell. As a random sort of example, find how many filenames in the current directory contain the letter ‘a’:
ls | grep a | wc
In an example like this (or, maybe, one which generates more traffic) the three child processes run independently, blocking if and when they are unable to input or output due to a pipe becoming empty or full or, possibly, timeslicing at other times.
Named pipes
It is sometimes said of Unix “Everything looks like a file”. Whilst not entirely true, lots of things can look like files – including pipes. Like a file, these need a name.
Try this experiment:
- Find a nice, secluded directory somewhere and open two separate shell windows upon it.
- In one window, type
mkfifo -m 666 FIFO
(“FIFO” is the name, “666” specifies the access permission.) - Try
ls -lsa
to see the pipe. - Attach one window to the pipe output with:
cat < FIFO
- Attach the other window to the pipe input with:
cat > FIFO
- Type things in your input window.
- Marvel!
- When finished, try to remember to
rm FIFO
Such a named pipe can also be created and used from within software etc. Note that (unlike a file) the open
call can block; opening one end of the pipe will block the opening process until the other end is open too. In Unix see man 3 mkfifo
for the C language interface, for example.
Similar mechanisms are available in Windows et alia, although the details of the interface are different.
Standard Streams
Unix processes have three standard I/O streams, created when the process is started:
- stdin - the default input: typically the keyboard
- stdout - the default output: typically the display (window)
- stderr - the diagnostic output: typically also the display (window)
A process will inherit its streams from a parent, so, for example, a process started by a Shell will run ‘within’ the shell window unless instructed otherwise.
stdin
and stdout
can conveniently be redirected. One application is to use pipes to connect one process to another.
Also refer to: | Operating System Concepts, 10th Edition: Chapter 3.7.4, pages 139-145 |
---|