How to check if stdin is still opened without blocking?

后端 未结 5 539
野性不改
野性不改 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:24

    I wonder if fctntl() with O_NONBLOCK flag applied to stdin descriptor would allow me to use read() function in non-blocking mode?

    Running stdin with O_NONBLOCK has advantages over select. Select says that there is some data, but not how much. There are times that you want to get all available data, but not block, regardless of how much in in the queue. Running select for each character seems like a lot of busy work... O_NONBLOCK did not work for me. It is an internal flag, not exposed in most tty drivers.

    Check out ioctl(..., FIONBIO). It seems to get the job done.

提交回复
热议问题