2

I have the following scenario that I have working with a single client process and a single server process, but I am uncertain as to how to design to scale the number of servers.

Note, in what follows the servers and the single client are processes, not threads. I have an 2D 4x4 array matrix [0-based, row-major] in shared memory that I would like several servers to be able to update a given assigned cell. The diagonal entries are always = 1. So for example, for the sake of simplicty say there are three servers 1, 2, 3. I would like to assign server 1 to always write to cell [1,0], and and server 2 to always write to cell [2,0] and server 3 to always write to [3,0].

The client's job is just to copy the matrix from shared memory to a local copy so as to block the writers for as little time as possible. The question is:

In this scenario of multiple servers and one client, do I need a semaphore set for each of the cells? If so, how do I coordinate all the servers since only one can be writing at a time while the [client potentially being multi-threaded one thread per each cell] client copies the shared matrix locally? If instead I use a binary semaphore, and there are multiple servers writing to shared memory, which of the servers is signaled to allow the write since several servers and one client are sharing a single binary semaphore set? I realize that a multi-threaded program for example actors may work better, but I need the servers to be processes not threads.

A simple example in C++ suffices, or an explanation is good too.

Ivan
  • 7,448
  • 14
  • 69
  • 134

1 Answers1

1

One simple solution is to use shared memory protected by a inter process lock. Every process need to graph the lock before access the memory. Take a look at shared memory and interprocess lock in boost library.

http://www.boost.org/doc/libs/1_57_0/doc/html/interprocess.html http://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchronization_mechanisms.html http://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/sharedmemorybetweenprocesses.html

a nice example here: Shared-memory IPC synchronization (lock-free)

Community
  • 1
  • 1
qqibrow
  • 2,942
  • 1
  • 24
  • 40
  • I am confused by your suggestion. spsc is a Single Producer Single Consumer container. While I have a single consumer. it is explicitly stated that I need several processes producing a result. Am I missing something? – Ivan Jan 18 '15 at 21:35
  • Correction, I ran the example program with two processes both pushing and one consuming, and it seems to work. I guess there is cognitive dissonance on my part because the container is a SPSC but somehow more than one producer can insert into the ring buffer. – Ivan Jan 19 '15 at 01:42