14

I am implementing two processes on a LynxOS SE (POSIX conformant) system that will communicate via shared memory.

One process will act as a "producer" and the other a "consumer". In a multi-threaded system my approach to this would be to use a mutex and condvar (condition variable) pair, with the consumer waiting on the condvar (with pthread_cond_wait) and the producer signalling it (with pthread_cond_signal) when the shared memory is updated.

How do I achieve this in a multi-process, rather than multi-threaded, architecture?

Is there a LynxOS/POSIX way to create a condvar/mutex pair that can be used between processes?
Or is some other synchronization mechanism more appropriate in this scenario?

GrahamS
  • 9,980
  • 9
  • 49
  • 63
  • Thanks @nos, I'd noticed there was a `pthread_condattr_setpshared` but I missed the `pthread_attr_setscope`. If you change your comment to an answer I'll upvote it. – GrahamS Apr 06 '10 at 13:12
  • Seems i was a bit misinformed, pthread_condattr_setpshared is indeed what you would use, pthread_attr_setscope deals with thread scheduling. – nos Apr 06 '10 at 17:41

4 Answers4

28

Credit goes to @nos, but I'd like to expand a little bit on his answer.
In the end (excluding error handling for clarity) I did as follows:

1. Define the shared memory structure

This contains the inter-process sync objects and the data to be shared.

typedef struct
{
    // Synchronisation objects
    pthread_mutex_t ipc_mutex;
    pthread_cond_t  ipc_condvar;
    // Shared data
    int number;
    char data[1024];
} shared_data_t;

2. Create the shared memory and set size (Master process)

On the Master process create a new shared memory object:

fd = shm_open(SHAREDMEM_FILENAME, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
ftruncate(fd, sizeof(shared_data_t));

2. OR Open the shared memory (Slave process)

On the Slave just attach to existing object:

fd = shm_open(SHAREDMEM_FILENAME, O_RDWR, S_IRUSR|S_IWUSR);

3. Mmap into process space

// Specify addr of calling address, mostly use NULL is most portable way
shared_data_t* sdata = (shared_data_t*)mmap(NULL, sizeof(shared_data_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);

4. Init the sync variables (Master process only)

pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&sdata->ipc_mutex, &mutex_attr);

pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&sdata->ipc_condvar, &cond_attr);

That's it.

Mutex and cond can now be used as normal to control access to the shared data.

The only real gotchas are making sure the Master process has created the shared memory and initialised the sync variables before the Slave process is started. And making sure you tidy up with munmap() and shm_unlink() as required.

Note: XSI Alternative

The POSIX:XSI extension has other functions for sharing memory (shmget(), shmat() etc) which may be more useful if they are available, but they are not on the version of LynxOS-SE I am using.

Louis Go
  • 2,213
  • 2
  • 16
  • 29
GrahamS
  • 9,980
  • 9
  • 49
  • 63
  • NOTE: We ran into issues with this approach on LynxOS. If any other memory is mmap'ed in before this is set up then it will fail. See http://stackoverflow.com/questions/2782883/condition-variable-in-shared-memory-is-this-code-posix-conformant – GrahamS May 07 '10 at 11:26
  • 1
    In the #2 code for the slave process, I also had to call ftruncate(fd, sizeof(shared_data_t)) after calling shm_open. It's just the parameters for shm_open that differ between master and slave for #2. Without the ftruncate in the slave, I was getting EFAULT and other errors when trying to use a flexible array member at the end of my struct. – Bill Menees Oct 24 '16 at 02:08
8

This is done with unnamed POSIX semaphores, i.e. the semaphores themselves are placed in shared memory.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
8

The standard way to create a process shared mutex/cond. variable is to initialize them with an attribute where you set pthread_mutexattr_setpshared/pthread_condattr_setpshared. Check if LynxOS supports that.

You'll naturally need to place such mutexes/cond. variables in shared memory somehow, so all processes can use it.

nos
  • 223,662
  • 58
  • 417
  • 506
6

You can achive this with the shared memory/semaphore IPC.

Here's an article with a nice example:

http://www-personal.washtenaw.cc.mi.us/~chasselb/linux275/ClassNotes/ipc/shared_mem.htm

mcabral
  • 3,524
  • 1
  • 25
  • 42