Do my (beginner) understanding of blocking and non blocking io is correct?

泄露秘密 提交于 2019-12-12 02:33:48

问题


Right now I do a lot of research about concurrency and parallelism. Could you tell me if I understand correctly (on os level):

Blocking io:

When I explicitly wait for connection (ie. in Ruby:)

conn = socket.accept

So my thread is blocked until I get something to socket, right?

(And I understand that I am pooling socket in some loop in accept for data, right?)

Non blocking:

I have thread that is asking from time to time all registered fd (filedescriptors) if they have something I need. But there is also 'dont call us, we will call you' rule, but how it is working on ios level (on libraries like eventmachine or node it is done by callbacks (?))

PS. I would welcome readings and presentations, like: http://www.paperplanes.de/2011/4/25/eventmachine-how-does-it-work.html http://www.kegel.com/c10k.html


回答1:


Blocking io:

When I explicitly wait for connection (ie. in Ruby:)

conn = socket.accept

So my thread is blocked until I get something to socket, right?

Right.

(And I understand that I am pooling socket in some loop in accept for data, right?)

Wrong. You are blocked. Period. The operating system will wake you up when something relevant happens.

Non blocking:

I have thread that is asking from time to time all registered fd (filedescriptors) if they have something I need. But there is also 'dont call us, we will call you' rule, but how it is working on ios level (on libraries like eventmachine or node it is done by callbacks (?))

What you have just described including the callbacks is 'asynchronous' I/O.

Non-blocking I/O just means that the calls don't block, so e.g. if you call read() and there is no data already there, nothing happens. When to call the calls is up to you but it is assisted by select()/poll()/epoll(), which block until various events have occurred on the socket(s).



来源:https://stackoverflow.com/questions/17796685/do-my-beginner-understanding-of-blocking-and-non-blocking-io-is-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!