Reputation: 1491
I'm trying to use serial ports in an async manner. I can use select
, poll
, or epoll
with O_NONBLOCK
to do async read and writes. But what about open
and close
?
I've seen close
block for more than a second.
Upvotes: 0
Views: 845
Reputation: 9732
There are very few operating systems which implement true asynchronous open()
and close()
(specifying O_NONBLOCK
to open()
means don't sleep waiting for connection or input, not actually perform the operation truly in the background). Two that come to mind are QNX and the Hurd, both are micro-kernel operating system designs where every syscall is by definition multiplexable and therefore asynchronous.
As to why, historically you can't do anything until an open()
completes so API designers never thought to make it async. More recently, if you really want it to be async, do the call from a threadpool. close()
is a bit more interesting, it's actually pretty hard to make closing a file descriptor fast without making it lose valuable information the loss of which will cause data loss e.g. "the buffered data I just tried to write out failed". But again, if you really need close()
to be async, just call it from a threadpool.
As a general rule, you cannot expect high performance if you call open()
and close()
a lot. Both inevitably involve making the kernel run a lot of code checking perms, allocating kernel structures, taking locks on kernel structures etc. Generally for high performance file i/o for example, you open the files you need at the beginning and never close them. That gets good to superb performance on most operating systems.
Upvotes: 2