cwindowsfilestdoutstdin

Where are stdin, stdout files located in Windows?


Whenever I execute a C program, there are 3 standard files, stdin, stdout, stderr. Theses map to /proc/self/fd/0, /proc/self/fd/1, /proc/self/fd/2 in Linux, which link to /dev/pts/0 in my computer. This is pseudo-terminal, to which this process outputs to and takes inputs from.

What is the equivalent of this in Windows? Where do these stdin, stdout, stderr point to, when I execute same program in Windows?


Solution

  • On a Linux kernel, the stdin, stdout and stderr streams have corresponding entries in /proc. That /proc filesystem is an informational structure which provides visibility into the system; it is not the implementation of these streams.

    Firstly, stdout is a C concept: an instance of a FILE * I/O stream. The operating system kernel (whether it be Linux or Windows) doesn't know anything about this. These streams hold operating system file descriptors/handles. A Linux or Windows program has a stdout stream due to being linked to a C library, which may not be true of a program that is not written in C, or a C-based language that uses a C run-time.

    A process in a Unix-like operating system has numbered file descriptors, starting at zero. The first three—0, 1 and 2—are, by convention, input, output and error.

    In Microsoft Windows, there is a similar concept. A process has three handles of type HANDLE which serve the same purpose. When you create a process using CreateProcess, they are specified in the STARTUPINFO structure which has these members:

    HANDLE hStdInput;
    HANDLE hStdOutput;
    HANDLE hStdError;
    

    which are meaningful if the STARTF_USESTDHANDLES flag is specified.

    Microsoft Windows doesn't have a /proc filesystem. It has API-based mechanisms for inspecting various system states. System utilities are written to these APIs. For instance, the Handle program can be used for inspecting what processes have what files open. A similar application on Linux would traverse /proc under the hood.