LifeDocs
Published on

Understanding Critical Section

Authors
  • avatar
    Name
    Vishok Manikantan
    Twitter

Hello! Hope your preparation for the upcoming CT is going well. The syllabus includes critical section, right? So, I thought of sharing my understanding of the critical section with you all.

In this blog post, we will see how the Critical Section works in Operating Systems.

What is Critical Section?

Critical Section is a part of the code where the shared resources are accessed. It is a part of the code where the process accesses the shared resources. The shared resources can be a variable, a file, a database, etc.

In simple words, the critical section is a part of the code where the process accesses the shared resources.

To understand the critical section, let's consider the following scenario:

Phone Booth

Imagine a phone booth with the following (default) rules:

  • A person can enter and use it only if it is not occupied by another person.
  • If the door is locked, it means that the phone booth is occupied.
  • After talking on the phone, the person has to 'open' the door and leave the booth.

With the above rules, consider the following actions:

More than one person wants to talk

If more than one person wants to talk on the phone, they have to wait outside the booth until the person inside finishes talking and leaves the booth. Hence, no other person can enter the booth until the person inside leaves.

Flow of actions

Try to visualize the flow of actions in the phone booth scenario:

Now, let's see the critical section version of the above visualization.

Closing Door -> Acquiring Lock / Critical Section

Opening Door -> Releasing Lock / Critical Section

Critical Section

To avoid errors and inconsistencies, the shared resources should be protected. This protection is called Critical Section.

The critical section is executed by only one process at a time.

  • Entry Section: Handles the entry of the process into the critical section. It checks if the critical section is available and acquires the lock.
  • Exit Section: Handles the exit of the process from the critical section. It releases the lock, informing other processes that the critical section is available.

Solution

The critical section problem requires a solution to synchronize the processes. The solution should satisfy the following conditions:

  • Mutual Exclusion
  • Progress
  • Bounded Waiting

Mutual Exclusion

Only one process can execute the critical section at a time. This ensures that the shared resources are not accessed by multiple processes simultaneously.

Relative to the phone booth scenario, only one person can talk on the phone at a time.

Progress

If a process is not using the critical section, it should not prevent other processes from entering the critical section. Meaning, any process can enter the critical section if it is free.

Relative to the phone booth scenario, any person can enter the booth if it is free. No one can restrict him from entering.

Bounded Waiting

Each process must have a limited waiting time to enter the critical section. This ensures that no process waits indefinitely to enter the critical section.

Relative to the phone booth scenario, a time limit can be set for each person waiting outside the booth, avoiding indefinite waiting.

Conclusion

In this blog post, we saw how the Critical Section works in Operating Systems. The critical section is a part of the code where the shared resources are accessed. It is executed by only one process at a time to avoid errors and inconsistencies.

Hope this blog post helped you understand the critical section better. If you have any questions or feedback, feel free to share them in the comments below.

Happy Learning! 🚀