Pipes: Difference between revisions

From COMP15212 Wiki
pc>Yuron
No edit summary
 
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
(No difference)

Revision as of 12:46, 26 July 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 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



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