When does write () to a file return EWOULDBLOCK?

I want to add data to a file on my local file system frequently. I want to do this without blocking for too long and without any worker threads. In Linux kernel 2.6.18.

It looks like the POSIX AIO implementation for glibc on Linux makes user thread space and blocks those threads. Which is cool, but I could just as easily spin my own dedicated dedicated file locking thread.

And I understand that the Linux Kernel AIO implementation is currently blocking when added. Adding is the only thing I want to do.

I am considering opening the file with O_NONBLOCK and then doing a kind of lazy email where if it is EWOULDBLOCK

, please try again later. Something like that:

  • open(pathname, O_CREAT | O_APPEND | O_NONBLOCK);

  • call write()

    , check for errorEAGAIN | EWOULDBLOCK

  • if EAGAIN | EWOULDBLOCK

    , then just save the recorded data and try again write()

    later.

Is this a good idea? Is there any actual advantage to doing this? If I am the only one who has an open file descriptor and I try to use write()

and EWOULDBLOCK

, is it less likely EWOULDBLOCK

later? Will it ever be EWOULDBLOCK

? If I am write()

not EWOULDBLOCK

, does that mean write()

fast?

In other words, under what circumstances, if any, will a write()

local file crash with EWOULDBLOCK

in Linux 2.6.18?

+3


source to share


2 answers


under ... what circumstances ... will write () to local file with error EWOULDBLOCK

Perhaps there is no circumstance for the file. The Linux man page for entry (2) states that EWOULDBLOCK will only be returned for a file descriptor that refers to a socket .



EAGAIN or EWOULDBLOCK The
    file descriptor fd belongs to a socket and is marked non-blocking (O_NONBLOCK) and writing will block. POSIX.1-2001 allows an error to be returned for this case, and does not require these constants to have the same value, so the portable application must test both possibilities.

Apparently, this behavior is due to the fact that the socket will use write locks, whereas a simple file will not.

+2


source


I'm not sure about the local filesystem, but I'm sure you can get EWOULDBLOCK when you try to write a file to a mounted filesystem (like nfs). The problem here is that you usually don't know if it is really a "local" hard drive, unless you specifically check it every time you create / open a file. How to check this is, of course, system dependent.



Even if the system creates some extra stream for the actual writing, that stream will have a buffer (which won't be infinite), so if you write fast enough you might end up with an EWOULDBLOCK.

+3


source







All Articles