tags:

views:

193

answers:

3

I am building a small shell interpreter which executes various commands, and I accomplish this by forking my process.

However, I want the child process to stop waiting for input in the standard input stream, and it does so by expecting an EOF. How do I push an EOF deliberately into that stream?

More specifically, if I am looping on this condition:

while (fgets(&input, 1024, stdin) != NULL) { // .....

How can I cause it to become false?

+1  A: 

The normal method is to close the stream at the sending (parent) end.

anon
how would I do such a thing?
Yuval A
How did you open it? Normally, you would use the close() system call or similar.
anon
like I said, I'm just reading chars from the stdin with fgets()
Yuval A
So the parent isn't providing the child with data? I think you need to describe your architeture in a bit more detail in your question.
anon
A: 

Don't you just close the other end of the pipe?

Nikolai N Fetissov
+1  A: 

Not sure of what your shell is doing - but I would have thought the the way of doing this would be to close the "standard input stream" in the child side of the fork() and then do not bother reading from it again. If you have forked a child to do something, why does it drop back into the main input handling loop.

In psuedo code

if (pid = fork())
{
    // parent - wait for child to do it's thing and then process another command
}
else
{
    // child
    close(0);
    // do some sort of command processing and then exit 
}
Beano
sounds reasonable. How do I "close" the stdin in C?
Yuval A
fclose(), got it
Yuval A