Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking IO vs non-blocking IO; looking for good articles [closed]

People also ask

Why is non blocking IO better?

Why non-blocking IO? The main benefit of non-blocking IO is that we need less threads to handle the same amount of IO requests. When multiple calls to IO are done using blocking IO, for each call a new thread is created. A thread costs around 1MB, and there are some costs due to context switching.

What is the difference between blocking and non blocking I O model?

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution.

What is non-blocking network IO?

Non-blocking I/O avoids the client being blocked while waiting for a request to be accepted by the transport layer during one-way messaging for connection-oriented protocols. For connection-oriented protocols, there is a limit to the amount of data that can be put in a network protocol queue.

What are available for non blocking IO operations?

Non blocking I/O Java NIO non- blocking mode allows the thread to request writing data to a channel, but not wait for it to be fully written. The thread is allowed to go on and do something else in a mean time.


Well blocking IO means that a given thread cannot do anything more until the IO is fully received (in the case of sockets this wait could be a long time).

Non-blocking IO means an IO request is queued straight away and the function returns. The actual IO is then processed at some later point by the kernel.

For blocking IO you either need to accept that you are going to wait for every IO request or you are going to need to fire off a thread per request (Which will get very complicated very quickly).

For non-blocking IO you can send off multiple requests but you need to bear in mind that the data will not be available until some "later" point. This checking that the data has actually arrived is probably the most complicated part.

In 99% of applications you will not need to care that your IO blocks. Sometimes however you need the extra performance of allowing yourself to initiate an IO request and then do something else before coming back and, hopefully, finding that the IO request has completed.

Anyway, just my tuppence.

Edit: To answer how to design an application for handling blocking IO while have good performance, coroutines could be a good fit.


The positives and negatives are pretty clear cut:

Blocking - Linear programming, easier to code, less control.
Non-blocking - Parallel programming, more difficult to code, more control.