The code in this repository implements the Producer–Consumer problem using shared memory and System V semaphores with strict atomicity requirements.
- Create a shared memory segment using:
shmgetshmat
- The parent process must remove the shared memory segment using:
shmctl
- Use exactly three semaphores:
- Mutex semaphore – controls exclusive access to the shared buffer
- Empty semaphore – counts empty slots in the buffer
- Full semaphore – counts full slots in the buffer
- Semaphore operations must be performed using:
semop
- Atomic P() and V() operations must be used
- Do NOT use separate semaphore calls for individual operations
- The program must create three child processes using
fork():- Two producers
- One consumer
.
├── main.c # Process creation, synchronization, and cleanup
├── buff.h # Shared buffer definition
├── buff.c # Buffer operations
└── README.md # Assignment documentation
Compile using GCC:
gcc -Wall -o ass2 main.c buff.cRun the program:
./ass2-
Producers:
- Wait on
empty - Acquire
mutex - Insert item into buffer
- Release
mutex - Signal
full
- Wait on
-
Consumer:
- Wait on
full - Acquire
mutex - Remove item from buffer
- Release
mutex - Signal
empty
- Wait on
All semaphore operations are performed using atomic semop() calls.