How to check if stdin is still opened without blocking?

后端 未结 5 562
野性不改
野性不改 2021-01-02 00:57

I need my program written in pure C to stop execution when stdin is closed.

There is indefinite work done in program main cycle, and there is no way I can use blocki

5条回答
  •  天涯浪人
    2021-01-02 01:45

    Yes, you can use select (with a zero timeout). You don't need to set the file descriptor non-blocking, though - if select tells you that the file descriptor is readable, then a read on it will definitely not block.

    So, poll file descriptor 0 occaisionally with select, and if it's readable, read it. If read returns 0, then that means it's been closed.

提交回复
热议问题