I am trying to create a system that consists of a server and several clients that are attempting to communicate with each other. The clients place data into shared memory (created using ftok / shmget), and then are able to write data into this memory. These consist of 2 separate programs.
The server reads values that the clients write into the shared memory, and write them to a file. I can get the clients to write data to the shared memory, and the server to read the data, but I am having trouble with locking the shared memory so multiple clients don’t write at the same time.
I tried creating a semaphore (using sem_open), but this is not shared between the processes. How do I go about sharing the semaphore between processes (or mutexes, as these would probably work better)?
You want to use a named semaphore or mutex:
Also, make sure you set the permissions correctly.