SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
What is Race Condition
Race Condition or Race Hazard is an undesirable situation of software, electronics, or other systems. When the output of the system or program depends on the sequence or timing of other uncontrolled events, this condition is called Race Condition.
This condition occurs mainly in the logic circuits, distributed and multithreaded software programs.
A race condition is categorized as either a critical or non-critical race condition. The critical race conditions are those conditions that occur when the order of the internal variable regulates the last state of the machine. On the other hand, the non-critical race conditions are those conditions which occur when the order of internal variables does not regulate the last state of the machine.
Race Condition in Electronics
The following example describes the race condition in electronic systems:
In this example, we use a logic gate which handles the Booleans values.
Let's take an AND logic gate, which accepts two A and B inputs, and gives only one output in the result.
When the inputs of A and B are TRUE, this gate will give the TRUE output, and when the value of one or both inputs are false, then AND gate will give Boolean FALSE.
Here, the race condition occurs when the program checks the output of the logic gate before the insertion of value in both A and B variables.
The correct set of operations for finding the output of AND gate is mentioned below:
- Input value in Variable A,
- Input value in Varibale B,
- Output of AND logic gate.
The race condition occurs in the following set of operations:
- Input value in Variable A,
- Output of AND logic gate.
- Input value in Varibale B,
Race Condition in Software
A race condition occurs in the software when the computer program depends on the threads or processes of a program.
In software, we cannot debug the race condition because the final result is non-deterministic and based on the timing of multiple threads.
Now, we take the following example to describe how the race condition occurs in the process:
Suppose two threads Thread 1 and Thread 2reduce the value of the global integer variable by 2.
The following table shows the consecutive order of operations:
Thread 1 | Thread 2 | Integer value |
---|---|---|
10 | ||
Read the Value | 10 | |
Reduce the Value | 10 | |
Write the value by 2 | 8 | |
Read the value | 8 | |
Reduce the Value by2 | 8 | |
Write the value | 6 |
In the above table, the final value is 6, as expected.
However, if the operations of these two threads execute concurrently without the concept of locking or synchronization, the outcome of the operations could be wrong.
The alternative sequence of the operations is shown in the following scenario:
Thread 1 | Thread 2 | Integer value |
---|---|---|
Read the Value | 10 | |
Read the value | 10 | |
Reduce the Value by 2 | 10 | |
Reduce the Value by 2 | 10 | |
Write the value | 8 | |
Write the value | 8 |
In this scenario, the final value is 8 instead of 6. This happens because the decrement operations of Thread 1 and Thread 2 are not mutually exclusive.
Those operations which cannot create interruptions while accessing some resources are called mutually exclusive operations.