Streams: Difference between revisions

From COMP15212 Wiki
pc>Yuron
No edit summary
 
gravatar W81054ch [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
A ‘stream’ is a serial flow of data.  It is not the same as a [[Files|file]] – files can be addressed in a ‘random’ manner – but files are not infrequently read or written as serial streams.  Indeed it is often said of [https://en.wikipedia.org/wiki/Unix Unix-family] operating systems that “[https://en.wikipedia.org/wiki/Everything_is_a_file everything is a file]”.
A ‘stream’ is a serial flow of data.  It is not the same as a [[Files|file]] – files can be addressed in a ‘random’ manner – but files are not infrequently read or written as serial streams.  Indeed it is often said of [https://en.wikipedia.org/wiki/Unix Unix-family] operating systems that “[https://en.wikipedia.org/wiki/Everything_is_a_file everything is a file]”.


In this case, the stream is an unstructured, serial succession of bytes.  Input from the stream will give the next byte – if and <em>when</em> there is one – and the byte is removed from the input sequence in the process.  Output to a stream will output – assuming no ‘back-pressure’ from something (such as writing a file)
In this case, the stream is an unstructured, serial succession of bytes.  Input from the stream will give the next byte – if and <em>when</em> there is one – and the byte is removed from the input sequence in the process.  Output to a stream will output – assuming no ‘back-pressure’ from something (such as writing a file) being slow further downstream; again the byte is then ‘gone’.
being slow further downstream; again the byte is then ‘gone’.


I/O devices can also be treated as streams.  Your keyboard provides an
I/O devices can also be treated as streams.  Your keyboard provides an input stream, for example.
input stream, for example.


=== Unix Standard Streams ===
=== Unix Standard Streams ===
Line 16: Line 14:
*Standard error (<code>stderr</code>) – usually also defaults to display.
*Standard error (<code>stderr</code>) – usually also defaults to display.


Processes can ‘get’ and ‘put’ characters to
Processes can ‘get’ and ‘put’ characters to these streams in various ways.  They are typically the default source/sink of input/output in system applications. You should be quite used to using them by now!
these streams in various ways.  They are typically the default
source/sink of input/output in system applications.
You should be quite used to using them by now!


=== Redirection ===
=== Redirection ===
Unix can redirect a stream to a file or, via a [[Pipes|pipe]], to
Unix can redirect a stream to a file or, via a [[Pipes|pipe]], to another process.  From a Unix command line try something like:
another process.  From a Unix command line try something like:
<blockquote>
<blockquote>
<code>ls | wc > clutter</code>
<code>ls | wc > clutter</code>
</blockquote>
</blockquote>
That example redirects the <code>stdout</code> from the <code>ls</code> process to the
That example redirects the <code>stdout</code> from the <code>ls</code> process to the <code>stdin</code> of <code>wc</code>, and <code>wc</code>'s <code>stdout</code> to a file. This sort of thing is often jolly useful!
<code>stdin</code> of <code>wc</code>, and <code>wc</code>'s <code>stdout</code> to a file.
This sort of thing is often jolly useful!


=== Streams as files ===
=== Streams as files ===
Line 48: Line 40:
</syntaxhighlight>
</syntaxhighlight>
----
----
{{PageGraph}}
{{PageGraph}}
{{Category|IO}}
{{Category|IO}}
{{Category|User}}
{{Category|User}}

Latest revision as of 10:03, 5 August 2019

Depends on QueuesShell

A ‘stream’ is a serial flow of data. It is not the same as a file – files can be addressed in a ‘random’ manner – but files are not infrequently read or written as serial streams. Indeed it is often said of Unix-family operating systems that “everything is a file”.

In this case, the stream is an unstructured, serial succession of bytes. Input from the stream will give the next byte – if and when there is one – and the byte is removed from the input sequence in the process. Output to a stream will output – assuming no ‘back-pressure’ from something (such as writing a file) being slow further downstream; again the byte is then ‘gone’.

I/O devices can also be treated as streams. Your keyboard provides an input stream, for example.

Unix Standard Streams

All Unix processes are provided with three standard streams:

  • Standard input (stdin) – usually defaults to keyboard.
  • Standard output (stdout) – usually defaults to display.
  • Standard error (stderr) – usually also defaults to display.

Processes can ‘get’ and ‘put’ characters to these streams in various ways. They are typically the default source/sink of input/output in system applications. You should be quite used to using them by now!

Redirection

Unix can redirect a stream to a file or, via a pipe, to another process. From a Unix command line try something like:

ls | wc > clutter

That example redirects the stdout from the ls process to the stdin of wc, and wc's stdout to a file. This sort of thing is often jolly useful!

Streams as files

Standard streams can often be both implicit or explicit when programming.

Example

The C statement:

printf("Hello World!\n");

can also be written as:

fprintf(stdout, "Hello World!\n");

in a similar way to ‘printing’ to a file:

fprintf(file_handle, "Hello World!\n");


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 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