School Lecture Study/Linux System Application Design

6-1. Synchronization (1)

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

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

Synchronization

The need for synchronization

  • Shared resources (data structures, variables)
    • When multiple threads are dependent on a shared resource and they need to access it at the same time, it needs to ensure that only one thread accesses it at a given point in time.

** child thread는 parent thread의 global variable에 접근 가능

** child process는 parent process의 global variable에 접근 불가

Critical Section and Race Condition

  • Critical Section
    • a piece of code that accesses resources shared among multiple threads (tasks)
    • 여러 스레드들 사이에서 공유된 자원에 접근하는 code 부분
  • Race condition
    • Multiple threads (tasks) access the same resource and the final result depends on interleaving of multiple threads
    • 여러 개의 thread 들이 동일한 resource에 접근하고, 결과는 여러 thread들의 interleaving에 달려있다.
    • Interleaving : 주기억 장치를 구성할 때 한 기억 장치 모듈 내의 연속적인 기억 장치 소자들에 연속적으로 주소를 붙이지 않고, 일정한 수의 배수만큼 거리를 두고 배정하는 방법.

Critical Section

  • A critical section is a code segment that accesses shared variables and has to be executed as an atomic action (all or nothing)
    • 오직 하나의 thread만이 critical section을 실행할 수 있어야 한다.
    • 다른 thread가 critical section을 실행하기를 원하는 경우, critical section을 실행중인 thread가 끝나기를 기다려야 한다.

An Example of Critical Section

  • updating a single global variable
    • 두 개의 thread가 동시에 i를 update한다면 result가 항상 똑같다는 것을 보장할 수 없다

  • Updating local variables
    • a와 b를 동시에 update 한다면 결과가 항상 동일한 것을 보장할 수 있다.

⇒ Local variable은 critical section이 아님 (shared resource가 아니기 때문)

Sharing Resources

  • Local variable은 thread 사이에서 공유되지 않는다.
    • 각 thread들은 각자의 stack을 가짐
  • Global variable은 thread를 통해 공유된다.
    • data segment에 저장되어 모든 thread가 접근 가능하다.
  • Dynamic objects 또한 thread를 통해 공유된다.
    • heap에 저장되어 pointer를 통해 공유된다.

  •  

Synchronization

  • Linux kernel provides synchronization primitives
    • single core
      • disabling preemption (or interrupts)
      • prevent other tasks (or ISRs) from running
    • multi-core
      • atomic operations : perform multiple actions
      • Locking primitives : prevent other tasks from entering a critical section
        • spinlock
        • semaphore
        • mutex
728x90

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

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