linux write()

Linux write():ssize_t write (int fd, const void * buf, size_t count)

有可能返回成功值,但实际并未成功写入的情况

因此,使用write()返回值作为文件写成功与否不完全正确,还需判断一下errno

 

open()

open("in.txt", O_RDONLY);  // 阻塞模式

open("in.txt", O_RDONLY|O_NONBLOCK);  // 非阻塞模式

O_NONBLOCK

When opening a FIFO with O_RDONLY or O_WRONLY set:

* If O_NONBLOCK is set, an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no
process currently has the file open for reading.

* If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until a thread opens the file for writing. An open()
for writing-only shall block the calling thread until a thread opens the file for reading.

 

Related Posts

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注