2

Lets take an example : i have an executable named a.out. This contains binary information to print some text to STDOUT cos of printf. So when I give ./a.out, i see output of printf at the console STDOUT

Say if i do './a.out > tempFile' in console. How does this work? Since there is printf inside a.out, ideally i except the text to be printed in STDOUT. How does redirection consume this text and why do we not see any output in console and only in the file we see the printf text

purvaja10
  • 31
  • 2
  • When you type `./a.out > tmpFile`, tempFile *is* stdout, and when printf causes data to be written to stdout it is written to the file. Your confusion is based on the misconception that stdout is the tty. – William Pursell Oct 10 '12 at 15:48

4 Answers4

3

In UNIX, everything is a file. All stdout is by default is the (for example) /dev/tty file which is a device driver hooked up to your console/terminal/window. Output is just sent to that file (device driver) which causes it to be output to whatever you're using for interactive I/O.

All the a command like a.out >xyzzy.txt does is first connect the standard output of the program to that file rather than /dev/tty, hence the output shows up there instead.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • So what I am confused about now is that who does this linking of stdout to the /dev/tty...when you say command, it is not the exe file(a.out) cos it is a binary which does not have the logic of redirection...So then it should be redirection > which handles this. So is > a separate process? How is it different from tee? Why do i see tee's o/p on console and file? Is a.out and redirection 2 separate process which share different address space? – purvaja10 Oct 10 '12 at 17:44
  • 1
    @purvaja, it's the shell itself that does this linking. What happens is that it interprets the command, acting on the redirection-type stuff, changing the stdin/out/err handles, then it sets up the other arguments into an array that the program can understand, then it calls that program. That's a necessarily simplified explanation but should give you the basic idea. – paxdiablo Oct 10 '12 at 20:19
  • Also, piping is a slightly more complex issue. It's done the same way with the shell controlling it but the stdout of one process is "redirected" on to the stdin of another process (rather than to a file or device driver). The reason you see output and file content for `tee` is because that's how `tee` is written. Every character is reads is then written _twice,_ once to a file and once to its stdout. – paxdiablo Oct 10 '12 at 20:23
2

in unix, everything is a file / filestream

a unix process has got 3 file streams connected by default:

0 = stdin
1 = stdout
2 = stderr

"normally", stdin is connected to the terminal emulation which will parse your keyboard input and stdout/stderr is connected to the terminal emulation that will provide your display.

the terminal emulator might be an xterm, gnome-terminal, kterm , or the linux virtual console ("textmode-console")

when you redirect, the stream is simply connected to a different source/destination. SO every text that would have gone to the terminal emulation will go to the file instead.

If you want both, "tee" might be an option:

./a.out | tee tempFile   

will print it out to the stdout (of tee, which you might redirect again) AND write it to the tempFile

DThought
  • 1,340
  • 7
  • 18
0

The shell executes a.out and replaces stdout with the file tempFile. There are a few functions (dup2, fropen) one can use to do this depending on the sort of redirection you want to achieve.:

See here: Redirecting the output of a child process

Community
  • 1
  • 1
sashang
  • 11,704
  • 6
  • 44
  • 58
0

Redirection is be a separate process which does the linking of the standard output of a.out to tempFile instead of /dev/tty(driver for printing to your terminal). So you see the output only in file and not in your console. This should be done before a.out executes. Once the linking is done by redirection operator , your execution of a.out starts and ends up printing in file.

user982845
  • 29
  • 2
  • 4
  • But how would a.out process know to wait till the redirection completes its linking of stdout to the tmp file? If you say its a separate process, how does the a.out and redirection communicate as to who should go first? – purvaja10 Oct 10 '12 at 19:17
  • ./a.out > tmpFile is interpreted by the SHELL process. So the shell process first executes redirection and then a.out. – user982845 Oct 10 '12 at 20:20