6

How exactly stdin, stderr, stdout are implemented in LINUX?

They are certainly not physical files. They must be some sort of temporary storage arrangement made by OS in the RAM for every process.

Are these array data structures attached to every process separately?

KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • possible duplicate of [confused about stdin, stdout and stderr?](http://stackoverflow.com/questions/3385201/confused-about-stdin-stdout-and-stderr) –  Oct 18 '13 at 14:40
  • 1
    i have read that post. it doesn't mention anywhere if stdin, stdout, stderr are array data structures OR not.. I am not confused about them. I just want to know how they are actually implemented.. – KawaiKx Oct 18 '13 at 14:45

1 Answers1

5

stdin, stderr, and stdout are file descriptors (or FILE* wrappers around them, if you mean the C stdio objects bearing those names). File descriptors are numbers that index a per-process data structure in the kernel. That data structure records which I/O channels a process has open, I/O channel being my ad-hoc term for a file, device, socket, or pipe.

By convention, the first entry in the table has index 0 and is called the standard input, 1 is the standard output and 2 is the standard error channel. This is just a convention in Unix programs; as far as the kernel is concerned, nothing's special about these numbers.

Each I/O system call (read, write, etc.) takes a file descriptor that indicates which channel the call should operate on.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • And how the I/O channels are implemented? – KawaiKx Oct 18 '13 at 23:58
  • @Saurabh: in various ways that comprise a very large deal of the kernel sources. Read them to find out, or pick up a book like *The Design of the UNIX Operating System* (Bach) or *Design and Implementation of the 4.3BSD UNIX Operating System* (McKusick et al.). These describe how Unix works internally and Linux is very similar. – Fred Foo Oct 19 '13 at 09:50