Open In App

Blocking and Nonblocking IO in Operating System

Last Updated : 07 Jan, 2023
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.


Similar Reads

Traps and System Calls in Operating System (OS)
Traps and system calls are two mechanisms used by an operating system (OS) to perform privileged operations and interact with user-level programs. Here is an overview of each mechanism: Traps: A trap is an interrupt generated by the CPU when a user-level program attempts to execute a privileged instruction or encounters an error. When a trap occurs
3 min read
Difference between System Software and Operating System
1. System Software: System software is a type of computer program that is designed to run a computer's hardware and application programs it controls a computer's internal functioning, chiefly through an operating system. It also controls peripherals devices such as monitors, printers, and storage devices. 2. Operating System: An operating system or
3 min read
System Protection in Operating System
Introduction: System protection in an operating system refers to the mechanisms implemented by the operating system to ensure the security and integrity of the system. System protection involves various techniques to prevent unauthorized access, misuse, or modification of the operating system and its resources. There are several ways in which an op
3 min read
User View Vs Hardware View Vs System View of Operating System
User View of Operating System: The Operating System is an interface, hides the details which must be performed and present a virtual machine to the user that makes it easier to use. Operating System provides the following services to the user. Execution of a programAccess to I/O devicesControlled access to filesError detection (Hardware failures, a
1 min read
File System Implementation in Operating System
Prerequisite - File Systems in Operating Systems A file is a collection of related information. The file system resides on secondary storage and provides efficient and convenient access to the disk by allowing data to be stored, located, and retrieved. File system implementation in an operating system refers to how the file system manages the stora
11 min read
Xv6 Operating System -adding a new system call
Prerequisite - Xv6 Operating System -add a user program In last post we got to know how to add user program in Xv6 Operating System. Now here you will see how to add new system call in Xv6 Operating System. A dding new system call to xv6: A system call is way for programs to interact with operating system. A computer program makes system call when
6 min read
Fork System Call in Operating System
In many operating systems, the fork system call is an essential operation. The fork system call allows the creation of a new process. When a process calls the fork(), it duplicates itself, resulting in two processes running at the same time. The new process that is created is called a child process. It is a copy of the parent process. The fork syst
5 min read
System Programs in Operating System
System Programming can be defined as the act of building Systems Software using System Programming Languages. According to Computer Hierarchy, Hardware comes first then is Operating System, System Programs, and finally Application Programs. Program Development and Execution can be done conveniently in System Programs. Some of the System Programs ar
3 min read
Privileged and Non-Privileged Instructions in Operating System
In any Operating System, it is necessary to have a Dual Mode Operation to ensure the protection and security of the System from unauthorized or errant users. This Dual Mode separates the User Mode from the System Mode or Kernel Mode. In an operating system, instructions are divided into two categories: privileged and non-privileged instructions. Pr
6 min read
Difference between Loading and Linking in Operating System
An operating system acts as an intermediary between the user of a computer and computer hardware. The purpose of an operating system is to provide an environment in which a user can execute programs conveniently and efficiently. Linking and Loading are utility programs that play an important role in the execution of a program. Linking intakes the o
4 min read