> For the complete documentation index, see [llms.txt](https://krjaeh0.gitbook.io/j-log/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://krjaeh0.gitbook.io/j-log/programming/cs_basics/os_memory/concurrency-and-synchronization/criticalsection.md).

# CriticalSection

## 임계 구역(Critical Section) 이란?

* 둘 이상의 프로세스나 스레드가 공유 자원(변수, 파일, 메모리 등)에 접근하는 코드 영역을 의미한다.
* 이 구역에서는 동시에 하나의 프로세스만 접근할 수 있어야 \*\*데이터 일관성(Data Consistency)\*\*이 유지될 수 있다.

## 임계 구역 문제

```c
// 공유 변수
int counter = 0;

// 임계 구역
counter = counter + 1;
```

* 두개의 프로세스가 동시에 `counter = counter + 1`을 수행하면, 값은 1만큼 증가하지 않는다.
* 예상하지 못하는 값 수정으로 인해 해당 값(메모리, 파일, 변수 등등)을 공유하는 프로세스들이 정상 작동하지 못할 수 있다.

## 임계 구역을 다룰 때 지켜야 할 조건 (3대 조건)

{% stepper %}
{% step %}

### 상호 배제 (Mutual Exclusion)

* 하나의 프로세스만 임계 구역에 들어갈 수 있다.
  {% endstep %}

{% step %}

### 진행 (Progress)

* 임계 구역에 들어갈 수 있는 프로세스를 결정하는 데 관여하지 않는 프로세스가 있다면, 그 결정은 무한히 지연되면 안 된다.
  {% endstep %}

{% step %}

### 한정 대기 (Bounded Waiting)

* 한 프로세스가 임계 구역 진입을 요청하면, 다른 프로세스가 무한히 먼저 들어가는 일은 없어야 한다.
  {% endstep %}
  {% endstepper %}

## 해결 방법들

### 1. 알고리즘 기반

* Peterson's Algorithm
* Dekker's Algorithm

### 2. 하드웨어 지원 명령어

* Test-and-Set
* Compare-and-Swap
* Load-Link/Store-Conditional

### 3. 운영체제 및 언어 수준 지원

* Mutex
* Semaphore
* Monitor
* Spinlock

## 발생 가능한 문제

| 문제 유형                 | 설명                                 |
| --------------------- | ---------------------------------- |
| 경쟁 상태(Race Condition) | 두 프로세스가 동시에 자원에 접근하면서 예기치 않은 결과 발생 |
| 교착 상태(Deadlock)       | 서로가 가진 자원을 기다리면서 무한 대기             |
| 기아 상태(Starvation)     | 어떤 프로세스가 계속해서 임계 구역에 진입하지 못함       |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://krjaeh0.gitbook.io/j-log/programming/cs_basics/os_memory/concurrency-and-synchronization/criticalsection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
