1

I have a server which is multiprocess and multithreaded. The child processes while handling request updates some statistics. This statistics data is a struct updated by all the child processes. Each of these child processes is again multithreaded. The number of child processes is dynamic(increases or decreases) based on the number of requests.

To synchronize the writes to this stat struct, I use mmap. Here is how the map is initialized.

    fd = open(mapfile, O_CREAT|O_RDWR, 0666);
    write(fd, dummy, MMAP_FILE_SIZE); //dummy is all zeros
    void *addr = mmap(0, sizeof(stat_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);

    if (addr == (void *)-1) {
       mapped = 0;
    }
    else {
       gStat = (stat_t*)addr;
    }

   // here gStat struct is initialized 

Also in the code where I manipulate the stats, I use a lock to synchronize across multiple threads within the process.

Now, the issue I am having is that under heavy load, the writes don't seem to synchronize. The statistics are not updated correctly. Under normal load, the stats get incremented correctly.

According to my understanding, If MAP_SHARED is specified, write references change the underlying object and the mapping type is retained across fork(). What am I missing?

Kamal
  • 3,068
  • 5
  • 26
  • 26
  • What kind of synchronization primitives are you using to coordinate updates to the contents of the `stat_t` structure? – Celada Dec 07 '12 at 20:10
  • My bad.. I thought the writes to the struct(mmaped region) are synchronized across processes. I just have a mutex lock for synchronizing across threads with in the same process. I did some research and found some links which suggested using semaphores or file locks to lock the mmaped memory region. Any suggestions/examples on which is better and how to do this would be helpful. – Kamal Dec 07 '12 at 22:59
  • Can I do this? Have a semaphore in that struct and use it for synchronization across processes like here http://stackoverflow.com/a/7938454/262107 – Kamal Dec 07 '12 at 23:12
  • 2
    Yes, you can use semaphores, that will work. It you are using Linux however, keep in mind that the pthread synchronization primitives perform MUCH MUCH better than semaphores though, so you might consider using them instead. Just make sure you use `pthread_mutexattr_setpshared()` to configure the mutex to be used across processes since by default they're meant for use between threads of a single process. See [this question](http://stackoverflow.com/questions/2389353/do-pthread-mutexs-work-across-threads-if-in-shared-memory) for details. – Celada Dec 08 '12 at 04:00

0 Answers0