3

From Wikipedia:

In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three I/O connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this."

As I understood from wiki the Stdin is traditionally the keyboard and the stdout is the monitor (output). But today what are these since most modern computers use GUI. For example, if I open cmd.exe of Linux Shell or whatever and I ran Python with this command:

# In cmd.exe   
>>> import sys 
>>> sys.stdout
<open file '<stdout>', mode 'w' at 0x000000000224B0C0>

A) I notice sys.stdout is a file (object) in memory, is it just an object created by Python interpreter? Or is sys.stdout physically stored in the a hard drive or any means of storage?

B) I think cmd.exe is a Win32 application, so does it have stdin, stdout or stderr and if so where are they? Are they just stored in memory?

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • http://stackoverflow.com/questions/3385201/confused-about-stdin-stdout-and-stderr – Padraic Cunningham Apr 17 '16 at 19:12
  • A Windows process has 3 standard handles that you can get and modify via `GetStdHandle` and `SetStdHandle`. Internally these set the process parameters `StandardInput`, `StandardOutput`, and `StandardError` handles. In Windows 8+, console handles reference kernel `File` objects for the `ConDrv` device that talks to the console host process, conhost.exe, and communication uses standard I/O system calls (`NtWriteFile`, etc). Prior to Windows 8, they aren't kernel handles, but specially marked console handles (low 2 bits set) managed by the console itself and communication uses LPC system calls. – Eryk Sun Apr 17 '16 at 19:13
  • Note that Windows doesn't use POSIX file descriptors at the system level, with the special files 0 (`STDIN`), 1 (`STDOUT`), and 2 (`STDERR`). Instead those are provided by the C runtime low I/O layer that maps Windows handles to POSIX file descriptors. Every CRT in a process (there's typically 2 or more) maintains its own mapping, which is a problem for Python extension modules built with MinGW that link with msvcrt.dll instead of the CRT that python.exe uses. – Eryk Sun Apr 17 '16 at 19:23
  • Also note that Python 2 uses C `FILE` streams. So, for example, `sys.stdout` references C `stdout`, which on Windows references the low I/O `STDOUT`, which references `StandardOutput`. Python 3 doesn't use C standard I/O. It has its own hierarchy of text, buffer, and raw streams on top of the POSIX layer. – Eryk Sun Apr 17 '16 at 19:35

2 Answers2

4

They are called streams and not files for a reason: they represent sources or sinks of data. Sometimes data sent to a stream are stored in a file in disk, but sometimes the data is simply received by another program.

For example, when you pipe two commands together:

$ ls | sort

The standard output of the first command is connected to the standard input of the second command.

About where the standard streams are connected in a simple shell, the standard streams are connected to the virtual terminal program. In linux, it would be the program shell (xterm, gnome-terminal, kdeterminal...). In Windows it would be the console manager, that is created directly by a OS module (usually csrss.exe but details vary between Windows version).

Then, when you write to the standard output the written characters are shown on the screen, and when you read from the standard input, the terminal waits for keyboard input.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 2
    In Windows 7+ it's separate instances of conhost.exe, not the csrss.exe system process. In Windows 8+ part of the work is also handled by the condrv.sys kernel device driver. – Eryk Sun Apr 17 '16 at 19:26
  • 1
    What do you mean by a stream exactly? I'm not really advanced regarding this. – GIZ Apr 17 '16 at 19:33
  • @direprobs: A _stream_ is an object (an abstraction) that represents something from which you can read data or to which you can write data. There are a lot of kinds of streams: files, sockets, pipes, fifos, consoles... The general idea is that you use more or less the same functions to write to / read from a stream, no matter the kind. You can consider _stream_ as a synonym for a general _file_. When in Python (or anywhere else) you use the word _file_ you sometimes refer to any kind of _stream_, not specifically filesystem files. – rodrigo Apr 17 '16 at 19:36
  • @rodrigo So basically, when I open cmd.exe for example, three streams are created for (connected to) the window? – GIZ Apr 18 '16 at 03:54
  • 1
    @direprobs: Yes, exactly that. And when you run a console program from that command window, the new program will inherit the handles to those three streams and share them with the parent cmd. – rodrigo Apr 18 '16 at 13:51
1

A file in Python is an abstraction of a file descriptor, which is just a low-level way of dealing with files and streams (and other things as well on *nix). In no way does it imply that there is anything on disk.

In addition, Windows applications don't have the standard streams available by default; the application must open them explicitly in order to make them available.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358