Critical Section Problem

Introduction (เคชเคฐเคฟเคšเคฏ)

Operating System เคฎเฅ‡เค‚ เค•เคˆ processes เคฏเคพ threads เคเค• เคธเคพเคฅ run เค•เคฐเคคเฅ‡ เคนเฅˆเค‚เฅค
เค•เคˆ เคฌเคพเคฐ เคฏเฅ‡ processes เคเค• เคนเฅ€ shared resource (เคœเฅˆเคธเฅ‡ variable, file, memory) เค•เฅ‹ access เค•เคฐเคคเฅ‡ เคนเฅˆเค‚เฅค

เค…เค—เคฐ เค‡เคจ processes เค•เฅ‹ properly control เคจเคนเฅ€เค‚ เค•เคฟเคฏเคพ เค—เคฏเคพ, เคคเฅ‹ system เคฎเฅ‡เค‚ error เคฏเคพ wrong output เค† เคธเค•เคคเคพ เคนเฅˆเฅค

เค‡เคธเฅ€ เคธเคฎเคธเฅเคฏเคพ เค•เฅ‹ Critical Section Problem เค•เคนเคพ เคœเคพเคคเคพ เคนเฅˆเฅค

Critical Section เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ

Program เค•เคพ เคตเคน เคนเคฟเคธเฅเคธเคพ เคœเคนเคพเค shared resource access เค•เคฟเคฏเคพ เคœเคพเคคเคพ เคนเฅˆ, เค‰เคธเฅ‡ Critical Section เค•เคนเคคเฅ‡ เคนเฅˆเค‚เฅค

Simple Definition

Critical Section = code เค•เคพ เคตเคน part เคœเคนเคพเค shared data access เคนเฅ‹เคคเคพ เคนเฅˆ

Critical Section Structure

เคนเคฐ process เค•เฅ‡ execution เค•เฅ‹ เคšเคพเคฐ parts เคฎเฅ‡เค‚ divide เค•เคฟเคฏเคพ เคœเคพเคคเคพ เคนเฅˆ:

1. Entry Section

เคฏเคนเคพเค process check เค•เคฐเคคเคพ เคนเฅˆ เค•เคฟ เคตเคน critical section เคฎเฅ‡เค‚ enter เค•เคฐ เคธเค•เคคเคพ เคนเฅˆ เคฏเคพ เคจเคนเฅ€เค‚เฅค

2. Critical Section

เคฏเคน เคตเคน เคญเคพเค— เคนเฅˆ เคœเคนเคพเค shared resource use เคนเฅ‹เคคเคพ เคนเฅˆเฅค

3. Exit Section

เคฏเคนเคพเค process critical section เค›เฅ‹เคกเคผเคคเคพ เคนเฅˆเฅค

4. Remainder Section

เคฌเคพเค•เฅ€ normal code เคฏเคนเคพเค execute เคนเฅ‹เคคเคพ เคนเฅˆเฅค

Race Condition เค•เฅเคฏเคพ เคนเฅˆ

เคœเคฌ เคฆเฅ‹ เคฏเคพ เค…เคงเคฟเค• processes เคเค• เคนเฅ€ เคธเคฎเคฏ เคชเคฐ shared data เค•เฅ‹ access เค•เคฐเคคเฅ‡ เคนเฅˆเค‚ เค”เคฐ result unpredictable เคฏเคพ เค—เคฒเคค เคนเฅ‹ เคœเคพเคคเคพ เคนเฅˆ, เค‰เคธเฅ‡ Race Condition เค•เคนเคคเฅ‡ เคนเฅˆเค‚เฅค

Example (เคธเคฎเคเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค)

เคฎเคพเคจ เคฒเฅ‹ เคเค• shared variable เคนเฅˆ:

count = 5

เคฆเฅ‹ processes เคนเฅˆเค‚:

P1: count = count + 1
P2: count = count + 1

เค…เค—เคฐ เคฆเฅ‹เคจเฅ‹เค‚ processes เคเค• เคธเคพเคฅ execute เค•เคฐเฅ‡เค‚:

  • เคฆเฅ‹เคจเฅ‹เค‚ 5 read เค•เคฐเฅ‡เค‚เค—เฅ‡
  • เคฆเฅ‹เคจเฅ‹เค‚ 6 write เค•เคฐเฅ‡เค‚เค—เฅ‡

Final result = 6 (เค—เคฒเคค)
Correct result = 7 (5+1+1) เคนเฅ‹เคจเคพ เคšเคพเคนเคฟเค

Critical Section Problem เค•เคพ Goal

เคฏเคน ensure เค•เคฐเคจเคพ เค•เคฟ:

  • เคเค• เคธเคฎเคฏ เคฎเฅ‡เค‚ เค•เฅ‡เคตเคฒ เคเค• process critical section เคฎเฅ‡เค‚ เคœเคพเค
  • data corruption เคจ เคนเฅ‹
  • correct output เคฎเคฟเคฒเฅ‡

Solution Requirements

Critical Section Problem เค•เคพ เคธเคนเฅ€ solution เค‡เคจ เคคเฅ€เคจ conditions เค•เฅ‹ satisfy เค•เคฐเคจเคพ เคšเคพเคนเคฟเค:

1. Mutual Exclusion

เคเค• เคธเคฎเคฏ เคฎเฅ‡เค‚ เค•เฅ‡เคตเคฒ เคเค• process critical section เคฎเฅ‡เค‚ เคนเฅ‹เคจเคพ เคšเคพเคนเคฟเคเฅค

2. Progress

เค…เค—เคฐ critical section เค–เคพเคฒเฅ€ เคนเฅˆ, เคคเฅ‹ เค•เคฟเคธเฅ€ waiting process เค•เฅ‹ enter เค•เคฐเคจเฅ‡ เคฆเคฟเคฏเคพ เคœเคพเคจเคพ เคšเคพเคนเคฟเคเฅค

3. Bounded Waiting

เคนเคฐ process เค•เฅ‹ limited time เคฎเฅ‡เค‚ chance เคฎเคฟเคฒเคจเคพ เคšเคพเคนเคฟเคเฅค
เค•เฅ‹เคˆ process indefinite wait เคจ เค•เคฐเฅ‡เฅค

Real Life Example

เคฎเคพเคจ เคฒเฅ‹ เคเค• bank account เคนเฅˆ:

  • Balance = 1000
  • เคฆเฅ‹ เคฒเฅ‹เค— เคเค• เคธเคพเคฅ เคชเฅˆเคธเฅ‡ เคจเคฟเค•เคพเคฒ เคฐเคนเฅ‡ เคนเฅˆเค‚

เค…เค—เคฐ system properly control เคจเคนเฅ€เค‚ เค•เคฐเคคเคพ:

  • เคฆเฅ‹เคจเฅ‹เค‚ same balance เคฆเฅ‡เค–เฅ‡เค‚เค—เฅ‡
  • เคฆเฅ‹เคจเฅ‹เค‚ เคชเฅˆเคธเฅ‡ เคจเคฟเค•เคพเคฒ เคฒเฅ‡เค‚เค—เฅ‡

Result: เค—เคฒเคค balance

Solution Techniques (Short Idea)

Critical Section Problem เค•เฅ‹ solve เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค:

  • Locks
  • Semaphores
  • Monitors

เค•เคพ use เค•เคฟเคฏเคพ เคœเคพเคคเคพ เคนเฅˆ (เค‡เคจ topics เค•เฅ‹ เค†เค—เฅ‡ เคชเคขเคผเฅ‡เค‚เค—เฅ‡)

Key Points (Exam Focus)

  • Critical Section definition
  • Race Condition example
  • Mutual Exclusion, Progress, Bounded Waiting
  • Structure (Entry, Critical, Exit, Remainder)

Conclusion

Critical Section Problem Operating System เค•เคพ เคเค• fundamental concept เคนเฅˆ, เคœเฅ‹ shared resources เค•เฅ‡ safe use เค•เฅ‹ ensure เค•เคฐเคคเคพ เคนเฅˆเฅค

เค…เค—เคฐ เค‡เคธเฅ‡ เคธเคนเฅ€ เคคเคฐเฅ€เค•เฅ‡ เคธเฅ‡ handle เคจเคนเฅ€เค‚ เค•เคฟเคฏเคพ เคœเคพเค, เคคเฅ‹ system เคฎเฅ‡เค‚ เค—เคฒเคค data เค”เคฐ inconsistent results เค† เคธเค•เคคเฅ‡ เคนเฅˆเค‚เฅค

Leave a Comment

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

Scroll to Top