in reply to Need to empty STDIN

select is your friend, read about it here. Specifically you're looking at the multi-io option with a timeout field.

The idea is to read STDIN when there is data available, and otherwise to sleep for a bit, then check select to see if there is more data, sleep, etc.

-Scott