Open In App

Blocking and Nonblocking IO in Operating System

Improve
Improve
Like Article
Like
Save
Share
Report

Blocking and non-blocking IO are two different methods used by OS to handle I/O operations. A blocking call is where the OS decides that it needs to wait for a certain operation to complete before allowing the program to continue execution. The result of this is that user can’t do anything else while waiting on a system call; if your program doesn’t have any other activity, then it should be able to wait indefinitely without causing any problems. On the other hand, non-blocking calls (also known as asynchronous operations) don’t block the thread until they finish their work; instead, this type of system call returns immediately after completing its job without having any effect on what’s happening in other threads.

Blocking System call

A blocking system call is a system call that blocks the process execution until the requested operation is completed. The blocking system call can be used in synchronous I/O or asynchronous I/O.

Synchronous (blocking) input 
and output methods: read(), write()
Asynchronous input and output methods: 
select(), poll()

The following example shows how to use the read() system call. The read() function returns the number of bytes actually read, which may be less than the number of bytes requested. The difference between the two is not always returned as an error condition.

The following example shows how to use the write() system call. The write() function writes up to count bytes from the buffer pointed to by buf to file descriptor fd.

It returns the number of bytes written, which may be less than the count if a write error occurs. The following example shows how to use the select() system call. The select() function blocks until input or output become possible for any of a set of open files. The descriptors will be updated with information about whether I/O is possible for them or not.

The following example shows how to use poll() system call. The poll() function returns the number of file descriptors that are ready for reading (0) and writing (1), respectively. It returns -1 on failure with errno set appropriately.

Non-Blocking System Call

A non-blocking system call is a system call that does not block. The calling process can continue execution while the operation is in progress and returns immediately when it’s complete.

The most important difference between blocking and non-blocking IO is how code behaves during the I/O operation: with a blocking IO, users must wait until data has been received before continuing execution; with a non-blocking IO, users don’t have to wait for anything at all!

The main advantage of non-blocking IO is that it allows users to continue with other tasks while waiting for the I/O operation to complete. This can be especially useful when writing concurrent programs, where there are many things happening at once (like multiple threads).

Non-blocking IO is a bit harder to write than blocking IO, but it’s also much more powerful and flexible. This is because non-blocking IO allows users to perform multiple I/O operations at once, in any order.

Asynchronous I/O

Asynchronous I/O is a technique to handle I/O operations without blocking the process. In other words, asynchronous I/O performs an operation on some data or resource and returns immediately without waiting for a response from another system. It’s used when the user wants to perform an operation that does not need constant interaction with your application, but the user still wants it done as soon as possible (e.g., printing something out). This approach allows more flexibility in program flow since it allows users to work with multiple threads at once and avoid deadlocks between them.

Asynchronous I/O can also be used for network communication since it provides better performance than synchronous IO (blocking IO), but there are some restrictions on how this needs to be implemented:

Users cannot use asynchronous reads or write if any other part of your application depends on them having completed before moving on to another task; otherwise, this could cause issues later down the line when running under load conditions!

Synchronous I/O

Synchronous I/O is a mechanism in which the process is blocked until the I/O operation is completed. This can happen when there is no way for processes to continue until their scheduled event has finished. In this case, synchronous I/O needs to wait patiently till it has been given permission by its supervisor program to proceed with further processing.

Asynchronous I/O, or nonblocking I/O (NBIO), is a mechanism in which the process continues its execution without waiting for the I/O operation to complete. For example: if the user wants to read data from disk and then write that data back onto disk later on – then asynchronous IO would suit this scenario perfectly well because it means that your program will continue running even after completing its task of writing new records onto disk or reading existing ones off.

Difference Between Blocking and Non-Blocking I/O

Blocking I/O Non-Blocking I/O

Blocking I/O is when a process is waiting for an I/O operation to complete. 

Non-blocking I/O means that when user call GetData().

For example, if users are trying to write data from your application into a file on disk, then if another process wants access too at that same time then they would have their own read or write operations started at the same time as yours. This can cause problems because there’s no guarantee that they’ll finish before the user does. 

In our example above, it returns immediately without waiting until all the requested data has been transferred over (and returned). The result here could be different depending on how busy those other processes were when calling GetData(), So this doesn’t really matter much here; just know that what we’re doing now won’t block any other programs from getting their jobs done either since everything happens on its own schedule – even though it might seem like nothing else was happening while waiting around forever.


Last Updated : 07 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads