I want to append data often to a file on the local filesystem. I want to do this without blocking for too long, and without making any worker threads. On Linux kernel 2.6.18.
It seems that the POSIX AIO implementation for glibc on Linux makes a userspace threadpool and blocks those threads. Which is cool, but I could just as easily spin off my own special dedicated file blocking thread.
And it’s my understanding that the Linux Kernel AIO implementation currently blocks on append. Appending is the only thing I want to do.
- http://code.google.com/p/kernel/wiki/AIOUserGuide
- https://groups.google.com/forum/#!msg/linux.kernel/rFOD6mR2n30/13VDXRTmBCgJ
I’m considering opening the file with O_NONBLOCK, and then doing a kind of lazy writing where if it EWOULDBLOCK, then try the write again later. Something like this:
open(pathname, O_CREAT | O_APPEND | O_NONBLOCK);- call
write(), check for errorEAGAIN | EWOULDBLOCK - if
EAGAIN | EWOULDBLOCK, then just save the data to be written and try thewrite()again later.
Is this a good idea? Is there any actual advantage to this? If I’m the only one with an open file descriptor to that file, and I try a write() and it EWOULDBLOCK, then is it any less likely to EWOULDBLOCK later? Will it ever EWOULDBLOCK? If I write() and it doesn’t EWOULDBLOCK, does that mean write() will return swiftly?
In other words, under exactly what circumstances, if any, will write() to a local file fail with EWOULDBLOCK on Linux 2.6.18?
Perhaps there is no circumstance for a file. The Linux man page for write(2) states that EWOULDBLOCK would only be returned for a file descriptor that refers to a socket.
Apparently this behavior is related to the fact that a socket would employ record locks, whereas a simple file would not.