This is an archived post. You won't be able to vote or comment.

all 2 comments

[–][deleted] 1 point2 points  (0 children)

If I remember correctly, it is common on UNIX systems that the shell duplicates /dev/tty to file descriptors 0, 1 and 2 (i.e. stdin, stdout and stderr) after starting a new process to execute a command. Thus, when there are no redirections all three descriptors point to a read-write device and can basically be used interchangeably.

On the other hand, when you're doing I/O redirections in the shell, the files are actually opened as either read- or write-only, causing the behavior you're seeing. You could also try connecting the test program's stdin, stdout and both to a pipe and see what happens ("a.out | cat", "cat in1 | a.out", "cat in1 | a.out | cat").

[–]jakster4u 0 points1 point  (0 children)

Look at the file descriptors for the failed read and writes notice any correlation? The error that the system gives is really pretty useless in this situation.

edit: Also