A quick word about semaphores. Unless you need reference a semaphore acrosss multiple processes I highly recommend using
sem_init over sem_open. sem_open creates semaphores that can persist beyond the life of your process if you don’t properly close them.
Say if you ctrl+c to stop a deadlocked program.
C code for a producer consumer buffer using mutexes and semaphores for synchronization.
Note that all the code is valid I just haven’t included the struct used for passing around the semaphores, mutex, and buffer as
that will be specific to your buffer.
The approach is thoroughly described in the code comments.