shared memory & mutex

This is a discussion on shared memory & mutex within the Linux Programming forums, part of the Platform Specific Boards category; I've to share memory between two applications. I readed several documents about it an I find that I've to use ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    35

    shared memory & mutex

    I've to share memory between two applications. I readed several documents about it an I find that I've to use a mutex (semaphore) to see if the shared memory is in use or not.
    I wonder if I can create a structure for example:
    Code:
    struct
    {
    char data[..];
    bool semaphore;
    } shared_data;
    where I set/clear semaphore variable to see if is in use or not.

    I find it much more simple than using the system semphores...

    Does someone can explain me if and why is better to use system semaphores and in the case, why the approach i described is not good?

    thx
    Lollo

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You use system semaphores because your approach doesn't work. It's unsafe. Furthermore, only system semaphores allow you to actually block until the semaphore is clear.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    Excuse me but I don't understand yet because my approach is unsafe.
    If I check my boolean flag before use the data it's the same that if I check a system mutex...which is the difference? It's my application that wait for the semaphore, not the OS .. I believe ...
    In which manner I can use the OS for waiting for semaphore?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    A simple variable is not thread-safe at all, and provides no guarantee of mutual exclusion. At the very least, you need guaranteed atomic access and store/load barriers.
    Also, what happens if the test tells you that you can't access the data? What do you do?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partly shared memory
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 01-21-2009, 02:35 AM
  2. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  3. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21