Semaphore λž€?


κ³΅μœ μžμ›μ— μ—¬λŸ¬ ν”„λ‘œμ„ΈμŠ€κ°€ λ™μ‹œμ— μ ‘κ·Όν•˜λ©΄μ„œ λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆλ‹€. μ΄λ•Œ κ³΅μœ μžμ›μ˜ λ°μ΄ν„°λŠ” ν•œ λ²ˆμ— ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€λ§Œ μ ‘κ·Όν•  수 μžˆλ„λ‘ μ œν•œμ„ μ€˜μ•Όν•œλ‹€.

Semaphore λŠ” λ©€ν‹° ν”„λ‘œκ·Έλž˜λ° ν™˜κ²½μ—μ„œ κ³΅μœ μžμ›μ— λŒ€ν•œ 접근을 μ œν•œν•˜λŠ” 방법이닀.

μž„κ³„ ꡬ역

μ—¬λŸ¬ ν”„λ‘œμ„ΈμŠ€κ°€ 데이터λ₯Ό κ³΅μœ ν•˜λ©° μž‘μ—…μ„ μˆ˜ν–‰ν•  λ•Œ, 각 ν”„λ‘œμ„ΈμŠ€μ—μ„œ κ³΅μœ μžμ›μ„ μ ‘κ·Όν•˜λŠ” ν”„λ‘œκ·Έλž¨ μ½”λ“œ 뢀뢄을 μ˜λ―Έν•œλ‹€.

ν•œ ν”„λ‘œμ„ΈμŠ€κ°€ μž„κ³„ ꡬ역을 μˆ˜ν–‰ν•  λ•Œ, λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€κ°€ μ ‘κ·Όν•˜μ§€ λͺ»ν•˜λ„둝 ν•΄μ•Ό ν•œλ‹€.

Semaphore P, V μ—°μ‚°

  • P(S) - μž„κ³„ ꡬ역에 λ“€μ–΄κ°€κΈ° 전에 μˆ˜ν–‰
  • V(S) - μž„κ³„ κ΅¬μ—­μ—μ„œ λ‚˜μ˜¬ λ•Œ μˆ˜ν–‰

Mutex λž€?


Mutual Exclusion, Mutex λŠ” μž„κ³„ ꡬ역을 가진 μŠ€λ ˆλ“œλ“€μ˜ μ‹€ν–‰μ‹œκ°„μ΄ μ„œλ‘œ κ²ΉμΉ˜μ§€ μ•Šκ³  λ‹¨λ…μœΌλ‘œ μ‹€ν–‰λ˜κ²Œ ν•˜λŠ” κΈ°μˆ μ„ μ˜λ―Έν•œλ‹€.

ν•΄λ‹Ή 접근을 μ‘°μœ¨ν•˜κΈ° μœ„ν•΄ lock κ³Ό unlock 을 μ‚¬μš©ν•œλ‹€.

  • lock - ν˜„μž¬ μž„κ³„ ꡬ역에 λ“€μ–΄κ°ˆ κΆŒν•œμ„ μ–»μ–΄μ˜¨λ‹€.
  • unlock - ν˜„μž¬ μž„κ³„ ꡬ역을 λͺ¨λ‘ μ‚¬μš©ν–ˆμŒμ„ μ•Œλ¦°λ‹€.

Semaphore 와 λ‹€λ₯΄κ²Œ lock 을 νšλ“ν•œ ν”„λ‘œμ„ΈμŠ€λ§Œ lock 을 λ°˜ν™˜ν•  수 μžˆλ‹€.

Mutex μ•Œκ³ λ¦¬μ¦˜


Dekker μ•Œκ³ λ¦¬μ¦˜

flag 와 turn λ³€μˆ˜λ₯Ό 톡해 μž„κ³„ ꡬ역에 λ“€μ–΄κ°ˆ μŠ€λ ˆλ“œλ₯Ό κ²°μ •ν•˜λŠ” 방식이닀.

Peterson μ•Œκ³ λ¦¬μ¦˜

Dekker 와 μœ μ‚¬ν•˜μ§€λ§Œ, μƒλŒ€ μŠ€λ ˆλ“œμ—κ²Œ μ§„μž… 기회λ₯Ό μ–‘λ³΄ν•˜λŠ” 것에 차이가 μ‘΄μž¬ν•œλ‹€.

Bakery μ•Œκ³ λ¦¬μ¦˜

μ—¬λŸ¬ μŠ€λ ˆλ“œμ— λŒ€ν•œ μ²˜λ¦¬κ°€ κ°€λŠ₯ν•œ μ•Œκ³ λ¦¬μ¦˜μ΄λ‹€. κ°€μž₯ μž‘μ€ 수의 λ²ˆν˜Έν‘œλ₯Ό 가지고 μžˆλŠ” ν”„λ‘œμ„ΈμŠ€κ°€ μž„κ³„ ꡬ역에 μ§„μž…ν•œλ‹€.

Semaphores


  • A tool that can be used to enforce mutual exclusion
    • Is a protected variable with methods for restricting access to critical resources
    • A binary semaphore takes values 0 and 1
    • A counting semaphore takes 0 ~ infinity

Producer-Consumer Problem

  • It is a classical example of a multi-process synchronization problem
  • Also known as the Bounded-Buffer Problem
  • Occurs when a critical section is not mutually exclusive
  • It can be solved by enforcing mutual exclusion

Problem using semaphore - Deadlock

  • Permanent blocking of a set of processes that either compete for the system resources
  • Deadlock only occurs in a system where all 4 conditions are met
  • Conditions
    • Mutual Exclusion: Only one process can use a resource at a time
    • Hold and Wait: Processes already holding resources may request new resources
    • No Preemption: No resource can be forcibly removed from a process holding it
    • Circular Wait: Two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds
  • Dining Philosopher Problem is a famous example of deadlock
    • It explains the solution by introducing binary semaphore as a waiter

Monitor

  • Improved version of multi-process synchronization
  • Provided through OOP languages
  • Enforces one process at a time to ensure mutual exclusion

Problem using semaphore - Starvation

  • The process is perpetually denied necessary resources
  • Solution
    • Use a scheduling algorithm with a priority queue that also uses the aging technique

References