Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-27-2011, 10:16 PM   #1
Registered: Feb 2004
Posts: 96

Rep: Reputation: 15
synchronization of two processes with share memory


I have two processes that share a piece of memory, and i want to use the shared memory to send data from one process to the other. it's like a simple consumer-producer problem. when the producer fills the shared memory, it waits until the consumer can consume some data in the memory; the consumer needs to wait if there is no data in the memory. The thing gets complicated when both threads are allowed to sleep and wait for the other to wake it up.

i wanted to use condition variable of pthread for synchronization, but it doesn't work in multiple processes. i tried semaphore, but it's quite complicated and i still cannot make it right.

I believe it's a common problem and someone should have written similar code before, or maybe the code is even wrapped in a library, but when I search for it on Internet, I only found information about how to share memory between processes. Does anyone know where I can find this kind of code or library?

Old 02-27-2011, 10:38 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Yes, it is a common problem, and yes, there are examples.

You're definitely on the right track: I would recommend semaphores in this scenario (synchronizing multiple processes).

Strong suggestion: check out Beej's Guide to *nix IPC:
Old 02-28-2011, 12:16 AM   #3
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
If the items are all fixed size, you can make it a lockless cyclic buffer, using GCC atomic built-ins (that are available in most current C compilers) to update the reader and writer side pointers without any locks at all.

For variable-length messages of up to size*sizeof(size_t)-sizeof(size_t) bytes, you could use this shared memory structure:
struct shared {
    size_t          size;
    pthread_mutex_t mutex;
    pthread_mutex_t waitmutex;
    pthread_cond_t  waitcond;
    size_t          head;
    size_t          tail;
    size_t          data[];    /* data[size] */
Remember to mark either the shared memory volatile, or use volatile references when reading the values within a loop.

Each message is aligned to a size_t, with the first size_t describing the message payload length in bytes. Note that a message may be split in two, if it crosses the boundary; head and tail are modulo size. When reading messages, you need to copy them to local storage first. If a payload is bytes long, it takes bytes/sizeof(size_t) + (bytes % sizeof(size_t)) ? 1 : 0 data slots.

Both readers and writers take the mutex. If there is not enough space for a message, writers take the waitmutex, and wait on the waitcond releasing the mutex (NOT waitmutex), until there is enough free space. After releasing a message, readers try to take the waitmutex; if busy, they will signal the waiting writer using pthread_cond_signal on waitcond. If the reader gets the waitmutex, there are no waiting writers, and the waitmutex can be immediately released.

In normal use, the buffer should be large enough for at least three messages. Otherwise, a writer with a long message may wait indefinitely, never getting enough free space in the buffer to write the message. (You could try to alleviate that by trying to take the waitmutex early in a writer: if busy, there is already a waiting writer; if not busy, release the mutex immediately. However, there is no guarantee which writer is woken by the pthread_cond_signal in Linux, and taking and releasing the mutex in the normal case is just a waste of cycles.)

I can write you example code for this, but I believe you will gain more from trying to apply the above recipe. If you have trouble getting it working, post your (example) code here, and I'll try to help. If I don't notice, drop me a private message.

Hope this helps,


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to share socket between processes Aga Programming 16 06-24-2020 06:32 PM
top used memory vs ps processes memory a6speedo Linux - Newbie 7 06-13-2010 01:23 AM
Share socket within different processes servel Programming 8 05-26-2009 09:12 PM
IPC - suggestions for synchronization of processes emge1 Programming 12 03-27-2007 02:51 PM
Standard Way To Share Memory Among Processes? Sys-V IPC? overbored Programming 1 06-21-2003 02:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration