Pipes: Difference between revisions

From COMP15212 Wiki
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
gravatar W81054ch [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
 
(One intermediate revision by one other user 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 “[[Everything_is_a_File|Everything looks like a
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] will run ‘within’ the
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 QueuesStreams

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

Pipe example

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.


Magritte Pipe


Also refer to: Operating System Concepts, 10th Edition: Chapter 3.7.4, pages 139-145


Articles on IO
Cacheability • Device Drivers • Direct Memory Access (DMA) • IO • Interprocess Communication • Interrupt Controller • Interrupt Service Routines (ISRs) • Interrupts • Libraries • Peripheral devices • Pipes • Queues • Queues Extra • Resources • Shell • Sockets • Spooling and Buffering • Starvation • Streams • System Calls • Thrashing • Timers • Using Peripherals • Virtualisation • Write Buffer
Articles on Processes
About this resource • Atomicity • Containers • Context • Context Switching • Daemons • Fork Unix • Hypervisor • Idle • Interprocess Communication • Multi Threading • Mutual exclusion • Pipes • Pointer Arithmetic • Process Control Block (PCB) • Process Priority • Process Scheduling • Process States • Processes • Queues • Queues Extra • Race Conditions • Real Time • Resources • Scheduler • Signal and Wait • Sleep • Starvation • Synchronisation • Thrashing • Threads • Unix Signals
Articles on User
"Everything is a File" • Application Binary Interface (ABI) • Arrays • Boot • Buffer Overflow • Containers • Daemons • Disk Partition • Dynamic Memory Allocation • Emulator traps • Environment Variables • Errors • Exceptions • File Attributes • File Locking • File Permissions • Introduction to Operating Systems • Journalling File System • Links • Locks • Man(ual pages in Unix) • Memory Mapped Files • Monitoring • Network File System (NFS) • PATH • Pipes • Pointers • Relocatable Code • Reset • SETUID • Shell • Sockets • Spooling and Buffering • Streams • Structures • Superuser • System Calls • Unix Signals • User • Using Peripherals