You could also use select() for a more portable and accurate solution.