Help with pipes, buffering and pseudoterminals

Ian Kelly ian.g.kelly at
Wed Apr 8 00:28:44 CEST 2015

On Tue, Apr 7, 2015 at 4:06 PM, Cameron Simpson <cs at> wrote:
> On 07Apr2015 20:38, Chris Angelico <rosuav at> wrote:
>> On Tue, Apr 7, 2015 at 3:48 PM, Cameron Simpson <cs at> wrote:
>>> The operating system arranges the commection of the shell to the
>>> terminal.
>>> Your usual program has by default a stdin, stdout and stderr. These are
>>> _all_ the same file handle, duplicated to each of the three file
>>> descriptors
>>> 0, 1 and 2 respectively. On the operating system side, the OS has
>>> performed
>>> _one_ open() call on the terminal device and handed the caller a single
>>> file
>>> descriptor. The caller then calls dup() (or modernly, dup2()) to present
>>> the
>>> open terminal as stdin, stdout and stderr.
>> Really? I can believe that stdout and stderr are initially duplicates,
>> but stdin as well? Isn't stdin opened for reading only, and
>> stdout/stderr for writing only?
> No. Have a look with lsof:
>  lsof -p $$
> in a terminal.

sys.stdin and sys.stdout are distinct, uni-directional io objects. At
a lower level though, the only thing stopping you from calling
os.write on fd 0 or on fd 1 or 2 is convention.

More information about the Python-list mailing list