Yuval Adam
Yuval Adam

Reputation: 165242

Feeding an input stream with EOF

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?

Upvotes: 0

Views: 434

Answers (3)

Beano
Beano

Reputation: 7841

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 
}

Upvotes: 1

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

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

Upvotes: 0

anon
anon

Reputation:

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

Upvotes: 1

Related Questions