School Lecture Study/Linux System Application Design

6-2. Synchronization (2)

vㅔ로 2022. 12. 20. 00:26
728x90

중앙대학교 3-2 리눅스 응용 설계 (손용석 교수님) 과목 정리입니다.

Disabling Preemption

  • task들 사이의 race condition을 막는다. (single core)
    • task1이 critical section을 실행하는 동안 task2에게 preempt 당함
    • 다른 Task가 critical section을 실행하는 동안에는 preemption disable 하고, ciritical section을 종료하면 preemption enable

  • multi-core의 race condition은 막지 못한다.
    • task 1에서 preemption을 막아도 다른 core에서 critical section에 접근하는 것을 막을 수 없다.

  • task와 ISR 사이의 race condition을 막을 수 없다.
    • task 1이 critical section에 진입했을 때 interrupt가 발생한 경우 ISR이 실행되어 critical section에 접근할 수 있게 된다.

How to Enable/Disable Preemption

  • Kernel Functions for Preemption Disable/Enable

  • 각 task가 preemption count를 관리한다.
  • preempt_count가 0이면 선점 가능
  • preempt_count가 0보다 크면 선점 불가

Disabling Interrupts

Disabling Interrupts prevents …

  • task와 ISR 사이의 Race condition을 막을 수 있다. (single core)

  • task 1이 critical section에 진입하면 ISR이 실행되지 못하도록 한다.
  • critical section 작업이 끝나고 ISR을 실행한다.

How to enable / disable interrupts

  • interrupt를 불가능하게 하고 interrupt enable states를 복원한다.
    • Local_irq_save : Local CPU의 IRQ disable
      • 현재 IRQ state를 flag로 저장
    • Local_irq_restore : Local CPU의 IRQ enable
      • IRQ state를 flag로부터 복원

Atomic operation & Instruction

Atomic operation

  • process 사이에서 완전히 독립적으로 동작한다.
  • 많은 modern OS, 병렬처리 시스템에서 사용된다.
  • Concurrent programming은 core가 많이 존재할 때 매우 중요하다.
  • atomic operation은 fine-grained operatoin으로 lockless 또는 lock-free concurrent programming을 가능하게 하는 components를 제공한다.

  • read & increment & write-back을 single operation으로 atomic하게 실행할 수 있다.
    • atomic operation은 나눠질 수 없다.
    • Hardware (processor)는 atomic operation을 보장 / 지원한다.

Usage and data type of Atomic operation

  • atomic_set : v에 4를 대입한다.
  • atomic_add : v에 2를 더한다.
  • atomic_inc : v에 1을 더한다.

Implementation : Atomic Add in x86

  • inline : compiler가 각 function call이 존재하는 곳에 함수 코드를 삽입하여 function call을 실제 함수 코드로 대체할 것을 제안하는 compiler 지시어 역할
  • __asm__ : Compiler가 assembly language로 쓰인 low-level code를 허용한다.
  • __volatile__ : 특정 compiler optimization을 하지 않도록 설정(Ex. relocating code)

GCC atomic memory access

  • __sync_fetch_and_add (type *ptr, type value)
  • __sync_lock_test_and_set (type *ptr, type value)
  • __sync_val_compare_and_swap (type *ptr, type oldval, type
    newval)
  • Etc

Fetch-and-add instruction

  • x가 메모리 주소, a가 어떤 값일 때 x의 값을 a만큼 증가시키고 x의 old value를 리턴한다.
  • concurrent system의 한 process에서 실행되는 경우, 다른 process들은 중간 결과를 볼 수 없다. (local variable이기 때문)

ret : 0

i : 1

Test-and-set instruction

  • x에 a를 대입하고 x의 이전 값을 리턴한다.i의 값은 1
  • ret은 0

Compare-and-swap instruction

  • x의 값이 oldvalue 와 같으면 x에 newval을 대입하고 x의 이전 값을 리턴한다.

__sync_val_compare_and_swap의 값이 1이 될 때까지 (값이 변경될 때까지) while 반복

728x90

'School Lecture Study > Linux System Application Design' 카테고리의 다른 글

6-4. Synchronization (4)  (0) 2022.12.20
6-3. Synchronization (3)  (0) 2022.12.20
6-1. Synchronization (1)  (0) 2022.12.20
5. Process and Thread  (0) 2022.12.20
4-2. Kernel module  (1) 2022.12.19