简体   繁体   中英

Is this a possible race with select/poll/epoll/kqueue?

I'm studying the semantics of select() / poll() / epoll() and kqueue() to decide how to emulate them in user space for my operating system .

Note that I only care about file read/write/exceptions multiplexing, not signals, child process states, timers and so on...

I've read several resource about them, their comparison, their advantages and their issues, but I can't find anything about a particular race that to my understanding affects all of them.

Suppose you have a multiplexing process waiting in a select (or in any other of group) for a file to be readable. After the select returns, but before the process can read that specific fd, another process sharing that same descriptor read the file. As far as I can tell, the read in the multiplexing process will block (until more data are available).

I know that this is a bug in the application (the multiplexed descriptors should not be available to any other process concurrently using them) but still I'd like to know if it's a possible scenario and if the various operating systems do anything to prevent such race.
Indeed, despite being probably complex, they could prevent the race, for example blocking the read of a particular descriptor "returned as readable" by select on all but the multiplexing process, till such process has consumed the event (aka read the fd). Obviously for more exotic events, this may be impossible, but I'm considering only files reads/writes here.

Note: this question is about the race, not the specifics of each system call, so please do not try to explain the differences/advantages and so on, if they are not related to this particular race.

Edit To clarify, I'm thinking about files that can actually block on read/write, since as explained by the select(2) manual on Linux

A file descriptor is considered ready if it is possible to perform a corresponding I/O operation (eg, read(2) without blocking, or sufficiently small write(2)).

当读取文件时,一旦达到EOF,读取将自动返回。这个问题是否与SOCKET读取有关?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM