Introduction to critical section with animation

In modern operating systems, with support for multithreading and multiprocessor almost becoming default, it is common to come across the scenario where more than one process would try to access a resource simultaneously. The resource could be just a memory area or a hardware resource. Allowing access to a resource by multiple processes at the same time might not be recommended and the modern operating systems provide various tools to prevent such simultaneous access.

The piece of code which tries to access the shared resource is termed as the critical section of the program.

The following animation explains the concept of critical section using a general example.

Two people A and B want to enter into a room at the same time. But only one person is allowed into the room at any given time.To ensure that only one person is allowed, the room is locked and only the person with the key to the lock is allowed into the room.

Let us a say both A and B reach the entrance but A gets the key before B then A will be allowed into the room and B will have to wait till A does not exit the room and frees the key. Only after A exits can B get hold of the key and enter the room.

With respect to operating systems, the persons A and B were equivalent to two processes, the room was the shared resource. The key was the tool used to ensure only one process gets access to the resource at any given time.

Two of the common tools available in operating systems to prevent multiple access to a shared resource are

Follow by Email