Shared Memory, Mutexes, and Semaphores
at Sunday, September 02, 2007
Inter-process Communication (IPC) is a capability of the Operating Systems to allow communication between two processes running either in same or in a different computer.
Shared memory is one of the ways of achieving inter-process communication. In this way of IPC, one program writes data to a memory portion and the other reads it (and vice versa). The memory portion so used is shared between two programs; and hence is the name.
Since same memory portion is shared between two different independent programs/processes, there can be a situation where both programs can try to write into the shared memory. This may result in data corruption and the effect can be of any severity. The part of the program which read/write the shared memory is called critical section.
To avoid data corruption there is a need for some check before entering critical section. This mechanism of avoiding conflicting access of shared resources is called mutual exclusion. A mutex has two states referred to as locked and unlocked. A locked mutex indicates the busy critical section (is under use) whereas an unlocked mutex indicates empty critical section. If the mutex state is locked by a process, other process waits for its unlock. The process which locked the mutex should unlock it before leaving critical section so that other process can enter it.
A semaphore is an extension of a mutex. If resources need to be shared among n processes (where n>2), then a semaphore is used instead of a mutex. A mutex can allow sharing of one resource whereas a semaphore can allow more than one processes to use multiple resources. A semaphore is initialized according to the number of resources it is implemented to control.
Definition from Dijkstra: A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and initialization operation called "Semaphoiinitislize".
Binary semaphores can assume only the value 0 or the value 1; counting semaphores also called general semaphores can assume only non-negative values. If a mutex is locked by a process, only that process can unlock it. Other processes will either spin or block on that mutex. Semaphore is maintained as a counter between 0 and a specified maximum number equal to the number of shared resources. A process increments it while entering critical section and decrements it while leaving critical section. A process can decrement a semaphore which was incremented previously by a different process. Read More...
Shared memory is one of the ways of achieving inter-process communication. In this way of IPC, one program writes data to a memory portion and the other reads it (and vice versa). The memory portion so used is shared between two programs; and hence is the name.
Since same memory portion is shared between two different independent programs/processes, there can be a situation where both programs can try to write into the shared memory. This may result in data corruption and the effect can be of any severity. The part of the program which read/write the shared memory is called critical section.
To avoid data corruption there is a need for some check before entering critical section. This mechanism of avoiding conflicting access of shared resources is called mutual exclusion. A mutex has two states referred to as locked and unlocked. A locked mutex indicates the busy critical section (is under use) whereas an unlocked mutex indicates empty critical section. If the mutex state is locked by a process, other process waits for its unlock. The process which locked the mutex should unlock it before leaving critical section so that other process can enter it.
A semaphore is an extension of a mutex. If resources need to be shared among n processes (where n>2), then a semaphore is used instead of a mutex. A mutex can allow sharing of one resource whereas a semaphore can allow more than one processes to use multiple resources. A semaphore is initialized according to the number of resources it is implemented to control.
Definition from Dijkstra: A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and initialization operation called "Semaphoiinitislize".
Binary semaphores can assume only the value 0 or the value 1; counting semaphores also called general semaphores can assume only non-negative values. If a mutex is locked by a process, only that process can unlock it. Other processes will either spin or block on that mutex. Semaphore is maintained as a counter between 0 and a specified maximum number equal to the number of shared resources. A process increments it while entering critical section and decrements it while leaving critical section. A process can decrement a semaphore which was incremented previously by a different process. Read More...
