Introduction (เคชเคฐเคฟเคเคฏ)
เคเคฌ multiple processes เคเค เคนเฅ shared resource เคเฅ access เคเคฐเคคเฅ เคนเฅเค, เคคเฅ race condition เคเฅ problem เคนเฅเคคเฅ เคนเฅเฅค
เคเคธ problem เคเฅ solve เคเคฐเคจเฅ เคเฅ เคฒเคฟเค เคนเคฎเฅเค เคเคธเคพ mechanism เคเคพเคนเคฟเค เคเฅ processes เคเฅ control เคเคฐเฅ เคเคฟ:
- เคเฅเคจ เค เคเคฆเคฐ เคเคพเคเคเคพ
- เคเฅเคจ wait เคเคฐเฅเคเคพ
เคเคธเฅ control mechanism เคเฅ Semaphore เคเคนเคคเฅ เคนเฅเคเฅค
Semaphore เคเฅเคฏเคพ เคนเฅเคคเคพ เคนเฅ
Semaphore เคเค synchronization tool เคนเฅ, เคเฅ processes เคเฅ execution เคเฅ control เคเคฐเคคเคพ เคนเฅ เคเคฐ shared resources เคเฅ safely use เคเคฐเคจเฅ เคฎเฅเค เคฎเคฆเคฆ เคเคฐเคคเคพ เคนเฅเฅค
Simple Definition
Semaphore = เคเค integer variable เคเฅ process coordination เคเฅ เคฒเคฟเค use เคนเฅเคคเคพ เคนเฅ
Semaphore เคเคพ Basic Idea
Semaphore เคเค counter เคเฅ เคคเคฐเคน เคเคพเคฎ เคเคฐเคคเคพ เคนเฅ:
- เค เคเคฐ value positive เคนเฅ โ process enter เคเคฐ เคธเคเคคเคพ เคนเฅ
- เค เคเคฐ value zero เคฏเคพ negative เคนเฅ โ process wait เคเคฐเฅเคเคพ
Semaphore Operations (เคฌเคนเฅเคค important)
Semaphore เคฆเฅ basic operations เคชเคฐ เคเคพเคฎ เคเคฐเคคเคพ เคนเฅ:
1. wait() (P operation)
- Process critical section เคฎเฅเค เคเคพเคจเฅ เคเฅ เคเฅเคถเคฟเคถ เคเคฐเคคเคพ เคนเฅ
- เค เคเคฐ resource available เคนเฅ โ enter เคเคฐเฅเคเคพ
- เคจเคนเฅเค เคนเฅ โ wait เคเคฐเฅเคเคพ
Conceptually:
- semaphore value เคเคฎ เคนเฅเคคเฅ เคนเฅ
2. signal() (V operation)
- Process critical section เคเฅเคกเคผเคคเคพ เคนเฅ
- เคฆเฅเคธเคฐเฅ process เคเฅ เคฎเฅเคเคพ เคฎเคฟเคฒเคคเคพ เคนเฅ
Conceptually:
- semaphore value เคฌเคขเคผเคคเฅ เคนเฅ
Working (Step-by-Step)
เคฎเคพเคจ เคฒเฅ semaphore = 1
Step 1:
P1 เคเคคเคพ เคนเฅ โ wait()
value = 1 โ 0
P1 critical section เคฎเฅเค เคเคพเคคเคพ เคนเฅ
Step 2:
P2 เคเคคเคพ เคนเฅ โ wait()
value = 0 โ -1
P2 wait เคเคฐเคคเคพ เคนเฅ
Step 3:
P1 finish โ signal()
value = -1 โ 0
เค เคฌ P2 เคเฅ เคฎเฅเคเคพ เคฎเคฟเคฒเคคเคพ เคนเฅ
Types of Semaphores
1. Binary Semaphore
- Value เคเฅเคตเคฒ 0 เคฏเคพ 1
- Lock เคเฅ เคคเคฐเคน เคเคพเคฎ เคเคฐเคคเคพ เคนเฅ
Use:
Mutual Exclusion
2. Counting Semaphore
- Value 0,1,2,3… เคนเฅ เคธเคเคคเฅ เคนเฅ
- Multiple resources เคเฅ manage เคเคฐเคคเคพ เคนเฅ
Use:
Resource counting
Example (Critical Section Control)
เคฎเคพเคจ เคฒเฅ:
semaphore = 1
Process P1:
wait()
critical section
signal()
Process P2:
wait()
critical section
signal()
เคฏเคนเคพเค:
- เคเค เคธเคฎเคฏ เคฎเฅเค เคเฅเคตเคฒ เคเค process critical section เคฎเฅเค เคเคพเคเคเคพ
- race condition avoid เคนเฅเคเฅ
Real Life Example
เคฎเคพเคจ เคฒเฅ เคเค parking space เคนเฅ:
- เคเฅเคตเคฒ 1 car เค เคเคฆเคฐ เคเคพ เคธเคเคคเฅ เคนเฅ
- semaphore = 1
Car 1 โ เค
เคเคฆเคฐ เคเคพเคคเฅ เคนเฅ โ value = 0
Car 2 โ wait เคเคฐเฅเคเฅ
เคเคฌ car 1 เคฌเคพเคนเคฐ เคเคเคเฅ โ signal โ car 2 เค เคเคฆเคฐ เคเคพเคเคเฅ
Advantages
- Mutual Exclusion ensure เคเคฐเคคเคพ เคนเฅ
- Processes เคเฅ เคฌเฅเค synchronization เคเคฐเคคเคพ เคนเฅ
- Resource management efficient เคฌเคจเคพเคคเคพ เคนเฅ
Disadvantages
- Implementation complex เคนเฅ เคธเคเคคเคพ เคนเฅ
- Deadlock เคเคพ risk เคนเฅเคคเคพ เคนเฅ
- Starvation เคนเฅ เคธเคเคคเคพ เคนเฅ
Exam Focus Points
- wait() เคเคฐ signal() operations
- Binary เคเคฐ Counting Semaphore
- Critical Section Problem เคเคพ solution
Conclusion
Semaphore เคเค powerful synchronization mechanism เคนเฅ, เคเฅ Operating System เคฎเฅเค processes เคเฅ เคฌเฅเค coordination เคเคฐ safe resource sharing เคธเฅเคจเคฟเคถเฅเคเคฟเคค เคเคฐเคคเคพ เคนเฅเฅค
เคฏเคน race condition เคเคฐ critical section problem เคเฅ solve เคเคฐเคจเฅ เคฎเฅเค เคฎเคฆเคฆ เคเคฐเคคเคพ เคนเฅเฅค