Semaphores

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 เค•เคฐเคจเฅ‡ เคฎเฅ‡เค‚ เคฎเคฆเคฆ เค•เคฐเคคเคพ เคนเฅˆเฅค

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top