Shared Memory & Semaphore

NOTE: BinarySemaphore is the same as Mutex (for one shared resource).

1. SharedMemoryBinarySemaphoreWriter & SharedMemoryBinarySemaphoreReader
First, run SharedMemoryBinarySemaphoreWriter.exe (this only writes data on the shared memory segment.)


Then, run SharedMemoryBinarySemaphoreReader.exe (it only reads data from the shared memory segment.)


2. SharedMemoryBinarySemaphoreMasterSlave (Both master/slaves write/reads data to/from the shared memory segment.)
First, run SharedMemoryBinarySemaphoreMasterSlave.exe –master
Then, run SharedMemoryBinarySemaphoreMasterSlave.exe



NOTE: Semaphore allows a specific number of processes/threads to enter.

3. SharedMemoryGeneralSemaphoreMasterSlave (classic reader-writer problem)

The writer fills data until the buffer is full. It waits emptyBufferSemaphore (e–) to be available. If so, it fills data. After that, it signals fullBufferSemaphore(f++) ready.

The reader reads data until the buffer is empty. It waits fullBufferSemaphore(f–) to be available. If so, it reads data. After that, it signals emptyBufferSemaphore(e++) to be ready.

1 master is the writer (fills data the shared memory buffer).

2 slaves are the readers (read data from the shared memory buffer; but their reader index must be synchronized).
First, run SharedMemoryGeneralSemaphoreMasterSlave.exe –master
Next, run SharedMemoryGeneralSemaphoreMasterSlave.exe
Then, run SharedMemoryGeneralSemaphoreMasterSlave.exe