Critical section (CS)

At any point in time, only 1 process can execute in the critical section.

Elaboration

This makes our mutations atomic.

For instance, suppose we had 2 processes executing concurrently, writing to file. They will write in an interleaving fashion. However, if we want 1 process to write, followed by second process, we need someway to control that behaviour.

Properties

  • We want only 1 process to access critical section at once

Mutual exclusion

  • At least 1 process should be accessing

    Progress

  • A process should eventually execute

    Bounded wait. If this doesn’t happen, the resources are blocked forever.

  • Other processes outside of the critical section should not block the process within. Otherwise this leads to deadlock.

Independence property

Another interesting occurrence of this is livelock. This happens when processes change state, to avoid deadlock. If that’s all they do, this is a livelock.

Implementation

naive Critical section implementation

peterson’s algorithm

Using testandset instruction