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
- Is a protected variable with methods for restricting access to
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