From: Chad on
Given the following...

#include <unistd.h>
#include <stdio.h>

int main(void)
{
char buf[BUFSIZ];
ssize_t n;

while ((n = read(0, buf, BUFSIZ)) > 0)
write(1, buf, n);

return 0;
}

When I compile and run it, the program(or process?) waits for input
from the standard input.

The question is, does read() get the input from the terminal or the
shell? My initial guess is the terminal, because the process itself
doesn't invoke shell.


Chad
From: Barry Margolin on
In article
<04a4f1a7-4d20-45b3-94c9-8087b645413e(a)y21g2000pro.googlegroups.com>,
Chad <cdalten(a)gmail.com> wrote:

> Given the following...
>
> #include <unistd.h>
> #include <stdio.h>
>
> int main(void)
> {
> char buf[BUFSIZ];
> ssize_t n;
>
> while ((n = read(0, buf, BUFSIZ)) > 0)
> write(1, buf, n);
>
> return 0;
> }
>
> When I compile and run it, the program(or process?) waits for input
> from the standard input.
>
> The question is, does read() get the input from the terminal or the
> shell? My initial guess is the terminal, because the process itself
> doesn't invoke shell.

It gets the input from whatever device is connected to standard input.
If you've redirected input to a file, it gets it from that file. If
you've set up a pipeline, it gets it from a pipe, which gets the data
from whatever program's output is connected to that pipe. If your
standard input is a terminal, it gets it from there.

If you do:

echo foo | yourprogram

it gets the input from the shell, since the shell's built-in echo
command is writing to the pipe. But change that to:

/bin/echo foo | yourprogram

and now the echo program is writing to the pipe, not the shell.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Rainer Weikusat on
Chad <cdalten(a)gmail.com> writes:
> Given the following...
>
> #include <unistd.h>
> #include <stdio.h>
>
> int main(void)
> {
> char buf[BUFSIZ];
> ssize_t n;
>
> while ((n = read(0, buf, BUFSIZ)) > 0)
> write(1, buf, n);
>
> return 0;
> }
>
> When I compile and run it, the program(or process?) waits for input
> from the standard input.
>
> The question is, does read() get the input from the terminal or the
> shell?

The shell is a program which reads commands written in a particular
language and executes according to the defined meanings of the 'words'
of this language. It's not a device, not even a virtual device (like a
'pseudo terminal').
From: Emacs on
On Jul 12, 1:18 am, Rainer Weikusat <rweiku...(a)mssgmbh.com> wrote:
> Chad <cdal...(a)gmail.com> writes:
> > Given the following...
>
> > #include <unistd.h>
> > #include <stdio.h>
>
> > int main(void)
> > {
> >   char buf[BUFSIZ];
> >   ssize_t n;
>
> >   while ((n = read(0, buf, BUFSIZ)) > 0)
> >     write(1, buf, n);
>
> >   return 0;
> > }
>
> > When I compile and run it, the program(or process?) waits for input
> > from the standard input.
>
> > The question is, does read() get the input from the terminal or the
> > shell?
>
> The shell is a program which reads commands written in a particular
> language and executes according to the defined meanings of the 'words'
> of this language. It's not a device, not even a virtual device (like a
> 'pseudo terminal').

What the program really cares is the stdin (standard input), no matter
what it is, such as shell, pipe, terminal etc.
From: Rainer Weikusat on
Emacs <jonathan.admin(a)gmail.com> writes:
> On Jul 12, 1:18�am, Rainer Weikusat <rweiku...(a)mssgmbh.com> wrote:
>> Chad <cdal...(a)gmail.com> writes:
>> > Given the following...
>>
>> > #include <unistd.h>
>> > #include <stdio.h>
>>
>> > int main(void)
>> > {
>> > � char buf[BUFSIZ];
>> > � ssize_t n;
>>
>> > � while ((n = read(0, buf, BUFSIZ)) > 0)
>> > � � write(1, buf, n);
>>
>> > � return 0;
>> > }
>>
>> > When I compile and run it, the program(or process?) waits for input
>> > from the standard input.
>>
>> > The question is, does read() get the input from the terminal or the
>> > shell?
>>
>> The shell is a program which reads commands written in a particular
>> language and executes according to the defined meanings of the 'words'
>> of this language. It's not a device, not even a virtual device (like a
>> 'pseudo terminal').
>
> What the program really cares is the stdin (standard input), no matter
> what it is, such as shell, pipe, terminal etc.

When I was still in school, I remember that I was once required to
partake an intelligence test and among the question where a couple of
'which of these terms ....' does not belong among the others. You
should be abel to solve that for 'shell, pipe, terminal' when using my
text as a hint.