Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

What is Race Condition in OS?

Author Nilesh Kumar
0 upvote
Operating system track
Free guided path
14 chapters
83+ problems
Earn badges and level up


In Operating Systems (OS), a Race Condition is a situation that occurs when two or more threads or processes access a shared resource, such as a file or a variable, at the same time.

What is Race Condition in OS?

In this blog, we will learn about race conditions in OS, along with the effects of race conditions, examples, and techniques to prevent them. So, without further ado, let's start!

FCFS Scheduling

What is Race Condition in OS?

A race condition is a problem that occurs in an operating system (OS) where two or more processes or threads are executing concurrently. The outcome of their execution depends on the order in which they are executed. In a race condition, the exact timing of events is unpredictable, and the outcome of the execution may vary based on the timing. This can result in unexpected or incorrect behavior of the system. 

Race condition in OS with example

If two threads are simultaneously accessing and changing the same shared resource, such as a variable or a file, the final state of that resource depends on the order in which the threads execute. If the threads are not correctly synchronized, they can overwrite each other's changes, causing incorrect results or even system crashes.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Types of Race Conditions in OS

There are two types of race conditions that occur in the critical section, which are discussed below.


A Read-Modify-Write race condition occurs when multiple processes or threads attempt to read, modify, and write the same shared resource concurrently, leading to unexpected behavior and inconsistencies in the final result.

The race condition arises because each process or thread assumes that the shared resource's value remains unchanged between the read and write operations. However, in the presence of other processes or threads modifying the same resource simultaneously, this assumption may not hold, leading to incorrect results.

To prevent read-modify-write race conditions, synchronization mechanisms such as locks, semaphores, or atomic operations can ensure that only one process or thread accesses the shared resource at a time. By enforcing mutual exclusion, the likelihood of inconsistent results is greatly reduced.


A Check-Then-Act race condition occurs when multiple processes or threads are checking for the same condition and then acting upon it without any synchronization mechanism to prevent race conditions.

Consider you have two threads that check the availability of a resource, such as a file. Both threads may check the availability of the file and find that it is available. However, between the time that the first thread checks the file and when it tries to access it, the second thread may also check the file and find that it is available. This can result in both threads trying to access the file simultaneously, leading to unexpected behavior and potential errors.

To prevent a "check-then-act" race condition, we can use synchronization mechanisms (locks, semaphores, or mutexes) to ensure that only one thread can access the resource at a time.

Must Read Process Management in OS

Effects of Race Condition in OS

In an operating system, a race condition can have several effects, which are discussed below:


A race condition can cause deadlocks, where two or more processes are awaiting the completion of a job by one another but are unable to move forward, can be brought on by a race condition. This can happen if two processes try to access the same resource simultaneously, and the operating system doesn't have a mechanism to ensure that only one can access the resource at a time.

Data corruption 

A race condition can cause data corruption, where two or more processes simultaneously try to write/update the exact memory location. This can cause the data to be overwritten or mixed up, resulting in incorrect results or program crashes.

Security vulnerabilities

A race condition can also create security vulnerabilities in an operating system. For example, an attacker may be able to exploit a race condition to gain unauthorized access to a system or to escalate their privileges.

Performance degradation

In some cases, a race condition can also cause performance degradation in an operating system. This can happen if multiple processes are competing for the same resources, such as CPU time or memory, and the operating system is unable to allocate these resources efficiently.

How to identify race conditions

  • Analysis of Source Code: Examine the source code of the OS kernel for potential race conditions, focusing on areas involving shared resources or synchronization primitives.
  • Static Analysis Tools: Use static analysis tools designed for OS code to detect potential race conditions, such as race detectors or code analyzers.
  • Dynamic Analysis Tools: Employ runtime analysis tools like thread analyzers, race condition detectors, or debugging tools to monitor thread interactions and shared resource access.
  • Testing: Conduct stress testing, load testing, or scenario-based testing to trigger race conditions. Monitor for system instability, data corruption, or crashes.
  • Logging and Monitoring: Implement extensive logging and monitoring to capture irregular system behavior, anomalies, or inconsistent data.
  • Code Profiling: Analyze system performance, thread interactions, and resource utilization with profiling tools to identify bottlenecks and potential race conditions.
  • Critical Section Inspection: Examine areas in the OS where shared resources are accessed and ensure that proper synchronization mechanisms (e.g., locks or semaphores) are used appropriately.
  • Peer Review: Collaborate with experienced OS developers to review the code and identify potential trouble spots or race condition vulnerabilities.
  • Documentation Review: Study the OS documentation and architecture to understand concurrency models and synchronization mechanisms.

How to Prevent Race Condition in OS

To prevent race conditions, you can use the following ways.

Use locks or semaphores 

Use synchronization mechanisms such as locks or semaphores to ensure that only one process or thread can access a shared resource at any given time.

Atomic operations

Use atomic operations, which are operations that cannot be interrupted, to manipulate shared data.

Avoid global variables

Avoid using global variables because they can be accessed and modified by multiple processes or threads simultaneously.

Use message passing

Use message passing instead of shared memory to communicate between processes or threads. This ensures that only one process or thread can access a particular message at a time.

Use thread-safe libraries

Use thread-safe libraries that are designed to prevent race conditions.

Design for concurrency

When designing your application, consider concurrency issues from the start. Make sure that your design can handle multiple processes or threads accessing shared resources.

Examples of Race Condition

We can understand race conditions by taking real-life examples. Consider a situation where two employees are working on the same document in a company's shared folder. Employee A opens the document and starts editing it, but before they can save their changes, Employee B opens the same document and starts making changes. 

When Employee A tries to save their changes, he is unable to change because the document has been updated by Employee B, and their changes are lost.

This is an example of a race condition because the two employees are "competing" to access and modify the same resource (in this case, the document). Depending on the timing and order of the actions performed by employees, the final state of the document can be unpredictable and inconsistent.

To avoid race conditions in this, the company may implement a version control system or a check-out/check-in mechanism that allows employees to work on different copies of the document and merge their changes later.

Read about Batch Operating System here.

Frequently Asked Questions

What is a race condition in operating system?

A race condition in an operating system occurs when multiple processes or threads access shared resources concurrently, leading to unpredictable and undesirable outcomes due to the timing and order of execution. It can result in data corruption, crashes, or other unexpected behavior.

What is race condition and deadlock in OS?

Race Condition: A race condition occurs when multiple processes or threads access shared resources concurrently, leading to unpredictable outcomes due to timing, potentially causing data corruption or unexpected behavior.

Deadlock: Deadlock is a state where multiple processes are stuck, unable to proceed, as they wait for resources held by each other, resulting in system unresponsiveness. It's caused by improper resource synchronization.

What is the race condition in data race?

A race condition in the context of a data race occurs when two or more threads in a concurrent program access and manipulate shared data concurrently, leading to unpredictable and potentially incorrect results due to the lack of proper synchronization mechanisms. Data races are a specific type of race condition that affect shared data access.

What is an example of a race condition in software?

A common race condition example is a banking application where multiple users attempt to withdraw funds from the same account simultaneously. If not properly synchronized, the balance may be incorrect due to simultaneous withdrawals.


In this blog, we have discussed race conditions in os, along with the effects of race conditions, examples, and techniques to prevent them. We hope the blog has helped you enhance your knowledge regarding race conditions in OS. 

If you wish to learn more about Operating Systems, you can refer to blogs on

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Previous article
Weighted-Job Scheduling
Next article
Introduction to Process Synchronization
Guided path
Operating system track
14 chapters
83+ Problems
Earn badges and level up
Live masterclass