#Non blocking I/O

25 messages · Page 1 of 1 (latest)

azure gyro
#

Hey, I want to know how the file descriptors use epoll for non-blocking I/O. Also how the epoll_wait system call is triggered when I/O is ready? I want to know the exact flow behind the non-blocking I/O. Could anyone guide me with this?

brisk narwhal
#

@azure gyro It doesn't "trigger". You explicitly call epoll_wait on the thread and the OS will then either give you the events that happened (which you can then process at your own pace) or it will hang at epoll_wait waiting for the next event

#

It's "non-blocking" because while you can receive I/O events while your code is still executing, the OS will just give them later when you call epoll_wait

azure gyro
#

Ohh so the reactive frameworks out there handles these system calls

#

Like they itself call epoll_wait() ?

brisk narwhal
#

Depends

#

Eg. Node.js afaik uses it

azure gyro
#

Do you have a flow diagram or something for non blocking I/o ?

#

So that I could understand the whole process completely?

brisk narwhal
#

But basically if you remember, if you use read() or write() your process would block until the I/O operations are complete, which usually takes significantly longer right?

azure gyro
#

Yes

brisk narwhal
#

Non-blocking IO kinda means you give a command to the OS, ignores it, and then later just check with the OS if it has completed or not

#

Your code runs and doesn't have to care/block immediately about if the I/O operations have succeeded or not

#

It's this way because relative to execution speed, I/O is slow, so waiting around for it is dumb

azure gyro
#

Suppose there is a network call and it's non blocking. The rest of the code will be executed or function will return. In the background, the framework issues epoll_wait() for this network I/o. So once it's done the os will be notified and the data shall be returned. Is it right?

brisk narwhal
#

@azure gyro It's kinda difficult to imagine but to your process it will still look as if it's blocking

#

epoll_wait() will still hang the thread

azure gyro
#

So what will make that thread free?

brisk narwhal
#

What is asynchronous/non-blocking is that between epoll_ctl (issuing the I/O operation) and epoll_wait you're free to do whatever you want

#

You only epoll_wait when you actually wanted to

#

Unlike blocking I/O where the moment you issue an operation you have to wait for it to complete before any other code in the flow would run

azure gyro
#

@brisk narwhal A file descriptor is said to be io ready if any io operation could be performed without blocking. So how is it determined whether a file descriptor is ready or not?

#

Are there any other things done by kernel or os for this?

brisk narwhal