Skip to content

Commit

Permalink
race condition
Browse files Browse the repository at this point in the history
- critical section
  • Loading branch information
xy-241 committed Jan 17, 2025
1 parent aa0980b commit 082985c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion content/OS/Synchronization/Busy Waiting.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ References:
- Also known as **自旋锁**
- A type of [[Busy Waiting]] specifically used as a **lock mechanism** where a [[Thread]] **repeatedly checks** and attempts to **acquire a lock** until it becomes available
- This is in contrast to [[Semaphore (信号量)]] or [[Mutex (互斥体)]], which releases the [[CPU]] and **allows other threads to run** while **waiting for the condition** to be met
- Usually used to protect short **critical sections** in a [[Kernel]] or [[Device Controller#Device Driver]]
- Usually used to protect short [[Race Condition (竞态条件)#Critical Section|critical sections]] in a [[Kernel]] or [[Device Controller#Device Driver]]
2 changes: 1 addition & 1 deletion content/OS/Synchronization/Mutex (互斥体).md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ References:
## Abstract
---
- Stands for **Mutual Exclusion**
- A [[Synchronisation (同步)]] mechanism that controls **access** to **shared resources**, such as variables, data structures, or critical sections of code, in a systems that use [[Concurrency (并发)]]
- A [[Synchronisation (同步)]] mechanism that controls **access** to **shared resources**, such as variables, data structures, or [[Race Condition (竞态条件)#Critical Section|critical sections of code]], in a systems that use [[Concurrency (并发)]]
- Powered by [[Atomic Instruction]]

>[!success] Avoid corrupted data
Expand Down
7 changes: 5 additions & 2 deletions content/OS/Synchronization/Race Condition (竞态条件).md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author Profile:
tags:
- OS
Creation Date: 2023-11-07T14:27:00
Last Date: 2025-01-01T20:14:25+08:00
Last Date: 2025-01-18T00:11:36+08:00
References:
---
## Abstract
Expand All @@ -15,10 +15,13 @@ References:

## Critical Section
---
- The section of codes where [[Race Condition (竞态条件)]] happens, like changing the common variables, updating a table, wiritng to a share file etc
- A critical section is a **segment of a program’s code** where **shared resources**, such as variables, files, or devices, are accessed and modified, which may lead to [[Race Condition (竞态条件)|race conditions]]


>[!important]
> At any point of time, at most one [[Process (进程)]] can be in the critical section.
>
> Or **race conditions can occur in critical sections** due to the [[Concurrency (并发)|concurrent execution]] of [[Thread|threads]] that **access shared resources** without proper [[Synchronisation (同步)|synchronisation mechanisms]].

## References
Expand Down
14 changes: 7 additions & 7 deletions content/OS/Synchronization/Synchronisation (同步).md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ References:
- **Coordination of execution** of multiple [[Process (进程)]] or [[Thread]] in a system to ensure that they **access shared resources** in a **controlled and predictable manner**
- The table below lists some common synchronisation methods

| Synchronization Method | Usage | Advantages | Disadvantages | Example Use Case |
| ----------------------------------- | -------------------------------------- | -------------------------------- | -------------------------------------------- | -------------------------- |
| [[Barrier (屏障)]] | Synchronise threads at **a point** | Simple, ensures synchronisation | Limited use cases | Parallel computing phases |
| [[Condition Variable (条件变量)]] | Wait for condition with mutex | Handles complex conditions | Complex, potential [[Race Condition (竞态条件)]] | Producer-consumer problems |
| [[Mutex (互斥体)]] | Protect shared data | Simple, effective | Contention, potential [[Deadlock (死锁)]] | Critical sections |
| [[Semaphore (信号量)]] | Control access to resources | Flexible, versatile | Complex, potential errors | Resource pools |
| [[Busy Waiting#Spinlock\|Spinlock]] | [[Busy Waiting]] for **short periods** | Low overhead for **short waits** | Inefficient for long waits | Low-level programming |
| Synchronization Method | Usage | Advantages | Disadvantages | Example Use Case |
| ----------------------------------- | -------------------------------------- | -------------------------------- | -------------------------------------------- | ------------------------------------------ |
| [[Barrier (屏障)]] | Synchronise threads at **a point** | Simple, ensures synchronisation | Limited use cases | Parallel computing phases |
| [[Condition Variable (条件变量)]] | Wait for condition with mutex | Handles complex conditions | Complex, potential [[Race Condition (竞态条件)]] | Producer-consumer problems |
| [[Mutex (互斥体)]] | Protect shared data | Simple, effective | Contention, potential [[Deadlock (死锁)]] | [[Race Condition (竞态条件)#Critical Section]] |
| [[Semaphore (信号量)]] | Control access to resources | Flexible, versatile | Complex, potential errors | Resource pools |
| [[Busy Waiting#Spinlock\|Spinlock]] | [[Busy Waiting]] for **short periods** | Low overhead for **short waits** | Inefficient for long waits | Low-level programming |

0 comments on commit 082985c

Please sign in to comment.