[OS] Interleaving, Race condition, Critical Section

Ko Hyejung·2021년 10월 16일
0

Operating Systems

목록 보기
21/26

Need for Process Synchronization

Concurrent access to shared data may result in data inconsistency.
공유 데이터에 동시에 액세스하면 데이터 불일치가 발생할 수 있습니다.

Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.
데이터 일관성을 유지하기 위해서는 협력 프로세스의 질서 있는 실행을 보장하는 메커니즘이 필요합니다.

Example: Shared-memory solution to bounded-buffer problem.

Producer and Consumer Code

Possibility of Interleaving

The statement “counter++” may be implemented in machine language as:

The statement “counter--” may be implemented in machine language as:

If both the producer and consumer attempt to update the buffer concurrently, the assembly statements may get interleaved.
생산자와 소비자가 동시에 버퍼를 업데이트하려고 하면 assembly statements이 interleaved될 수 있습니다.

Interleaving depends upon how the producer and consumer processes are scheduled.
인터리빙은 생산자와 소비자 프로세스의 스케줄에 따라 달라집니다.

Example: Incorrect Interleaving

Assume counter is initially 5.

One interleaving of statements is:

The value of counter may be either 4 or 6, where the correct result should be 5.

Race Condition

Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last.

여러 프로세스가 공유 데이터에 동시에 액세스하고 조작하는 상황입니다. 공유 데이터의 최종 값은 어떤 프로세스가 마지막으로 완료되느냐에 따라 달라집니다.

To prevent race conditions, concurrent processes must be synchronized.
race condition을 방지하려면 동시 프로세스를 동기화해야 합니다.

The statements

  • counter++;
  • counter--;
    must be performed atomically.

Atomic operation means an operation that completes entirely without interruption.
중단 없이 완전히 완료되는 작업을 의미합니다.

Critical Section (CS)

n processes all competing to use some shared data.

Each process has a code segment, called critical section, in which the shared data is accessed.

각 프로세스에는 critical section이라는 코드 세그먼트가 있으며, 여기서 공유 데이터에 액세스합니다.

Problem - ensure that when one process is executing in its critical section, other process is not allowed to execute in its critical section.

한 프로세스가 해당 중요 섹션에서 실행 중일 때 다른 프로세스는 중요 섹션에서를 실행할 수 없도록 해야 합니다.

Approaches to CS Implementation

  • Software-based approaches (software checks to ensure CS) : No guarantee that they will work correctly on modern computers
  • Synchronization instructions such as TestAndSet and Swap
  • Semaphore
  • High-level synchronization construct such as monitor

Properties of a CS Implementation

Mutual Exclusion
If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.

상호 제외
프로세스 Pi가 임계 부분에서 실행 중이면 다른 프로세스는 해당 임계 부분에서 실행될 수 없습니다.

Progress
If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.

진행
해당 중요 섹션에서 실행 중인 프로세스가 없고 해당 중요 섹션에 들어가려는 일부 프로세스가 있는 경우 다음으로 중요 섹션에 들어갈 프로세스의 선택을 무기한 연기할 수 없습니다.

Bounded Waiting
A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.

  • Assume that each process executes at a nonzero speed.

바인딩 대기
한 프로세스가 해당 중요 섹션에 들어가도록 요청한 후 해당 요청이 승인되기 전에 다른 프로세스가 해당 중요 섹션에 들어갈 수 있는 횟수에 제한이 존재해야 합니다.

  • 각 프로세스가 0이 아닌 속도로 실행된다고 가정합니다.
  • No assumption concerning relative speed of the n processes.

0개의 댓글