sam32
sam32

Reputation: 335

non blocking thread-safe file write

I am trying to write in a file at different offset locations using threads that shouldn't get blocked. I am not very sure how to proceed with the same. I am guessing that I need to open the file with O_NONBLOCK flag.

Is it just that I need to open the file with O_NONBLOCK flag and writing process would be the same ?

Any sample piece of code would be helpful as an explanation

Thanks

Upvotes: 0

Views: 688

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136218

On UNIX/Linux file descriptors associated with files are always ready for read and write. In other words, O_NONBLOCK has no effect on regular files.

Normally, writing to a file just copies the data to the kernel page cache and returns. Unless the file was opened with O_DIRECT flag, or the kernel page cache has too many dirty pages in which case the write becomes blocking.

If you need to do non-blocking writes into a file either create a dedicated thread that does all the writing, or use asynchronous I/O.

Upvotes: 1

Related Questions