0

When a background process outputs to stdout to appear on terminal, will the output be taken as stdin input to the foreground process? Note that the background and foreground process are not related by pipe.

I have this question because when a foreground process waits for stdin input, I can type in the terminal to feed it. The stdout output of a background process seems to do the same thing, i.e. the background process "types" in the terminal, while the foreground process is waiting for stdin input. So why don't the foreground process take the background process's stdout output as stdin input?

In my experience, it seems no. If I am correct, why does it not?

  • 1
    It will if you pipe one into the other -- that's what pipes are for. If you don't, then it will not. – DopeGhoti Apr 11 '16 at 19:07
  • My question is why it will not if I don't? – Tim Apr 11 '16 at 19:16
2

The terminal is conceptually separate from process stdin and stdout. This can be confusing since we talk about "terminal" both for the keyboard input and the screen output.

However, a process can read input from the keyboard without echoing it back to the screen. E.g., when you log in, your password is read from your typed input, but it doesn't appear on the screen.

And of course a process can print output to the screen without you typing that on your keyboard.

It's possible (e.g. using tee) to both write an output stream to the screen and pass it to another process, but that doesn't make them the same.

In a nutshell, printing something on the screen is not the same as typing it in the keyboard, even though we use the word "terminal" to describe both.

  • Thanks. When the shell is waiting at a prompt and I am typing on keyboard, is the shell process which decide to output to stdout what I type? When a program is running in the shell and I am typing on keyboard, is the program's process which decide whether to output to stdout what I type? – Tim Apr 11 '16 at 20:39
  • @Tim, see the last section of this answer. Actually the whole answer would be enlightening, but the last section answers the above comment. (It's the shell.) – Wildcard Apr 11 '16 at 20:43
  • In the second question in my comment, I guess that it is not the shell process but the foreground process running in the shell which decides whether to echo to the stdout what I type. – Tim Apr 11 '16 at 20:58
0

No, because stdin and stdout are separate channels. Unless you explicitly pipe, there is no connection. You don't expect the output from your print statements to be the input for a subsuquent input statement, do you?

  • when a foreground process waits for stdin input, I can type in the terminal to feed it. The stdout output of a background process seems to do the same thing, so why don't the foreground process take the background process's stdout output as stdin input? – Tim Apr 11 '16 at 19:18
  • Because you're missing the pipe which is the literal thing that is used to attach the output of one process to the input of another. This is so that you can monitor the output of one process while providing your own, unrelated, input to another. – DopeGhoti Apr 11 '16 at 20:01
  • @Dope: I am not asking whether a pipe can connect two processes. I am asking why not in my post, without mentioning using pipe. – Tim Apr 11 '16 at 20:16
  • And I'm telling you, the reason not is because you're not using a pipe. That's what a pipe is for. It's like asking why a siphon doesn't work without a tube, or why an internal combustion engine doesn't work without pistons. – DopeGhoti Apr 11 '16 at 20:37
  • Output is not input even if the terminal, for your convenience, echoes your input. – Kevin Apr 11 '16 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.