Thread: Mutex and Shared Memory Segment Questions.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    So, does anyone know of a method which would allow me to get the shmid of a shared memory segment that was previously marked for deletion? I need to be able to get a memory segment that previously marked for deletion, attach to it and read/write to it...

    Any help would be appreciated

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MadDog View Post
    So, does anyone know of a method which would allow me to get the shmid of a shared memory segment that was previously marked for deletion? I need to be able to get a memory segment that previously marked for deletion, attach to it and read/write to it...

    Any help would be appreciated
    You can enumerate all shmids with shmids().

    There is no standard way to determine if the deletion flag was set. You may be able to poke through /proc to extract this info.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Thanks brewbuck,
    Is there any way to know if a shmid was created through a particular key? I don't really care if the segment is marked for deletion or not. I just want to have a common shared segment for many independent processes. My problem is that attempting to get the shmid of a segment - that was previously created and marked for deletion - through shmget(my_key, ...) creates a new segment... What I want is that segment (the one I created in the first place), not just any segment marked for deletion...

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MadDog View Post
    Thanks brewbuck,
    Is there any way to know if a shmid was created through a particular key? I don't really care if the segment is marked for deletion or not. I just want to have a common shared segment for many independent processes. My problem is that attempting to get the shmid of a segment - that was previously created and marked for deletion - through shmget(my_key, ...) creates a new segment... What I want is that segment (the one I created in the first place), not just any segment marked for deletion...
    You should be able to find the key with:

    Code:
    struct shmid_ds ds;
    
    shmctl(shmid, IPC_STAT, &ds);
    ds.shm_perm.key; /* Here's the key */
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Thanks Brewbuck,
    I tried what you suggested but shmids() doesn't seem to exist in Linux man pages; the only reference I found of it is in the Sun Microsystem system calls man pages... I'm not a linux/unix guru so I'm guessing that it's a function call that is proper to system running Solaris or something... I however got around that by reading /proc/sysvipc/shm as you suggested but ran into another problem.
    The problem is that ds.shm_perm.key gets reseted to 0 whenever the shared segment associated to shmid is marked for deletion and I can't tell if it's mine or not anymore...

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MadDog View Post
    Thanks Brewbuck,
    I tried what you suggested but shmids() doesn't seem to exist in Linux man pages; the only reference I found of it is in the Sun Microsystem system calls man pages... I'm not a linux/unix guru so I'm guessing that it's a function call that is proper to system running Solaris or something... I however got around that by reading /proc/sysvipc/shm as you suggested but ran into another problem.
    The problem is that ds.shm_perm.key gets reseted to 0 whenever the shared segment associated to shmid is marked for deletion and I can't tell if it's mine or not anymore...
    Bummer...

    I think in your situation it might be better to use named, memory-mapped files instead of SysV shared memory. The filename acts as both key and id at the same time, is easily detectable by looking in the file system, can be deleted before the last person unmaps the segment, i.e. it meets your requirements.

    It even provides the added benefit of never leaking -- once the last user unmaps the file, the pages are freed, unlike when the last user of a shm segment forgets to delete it!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Quote Originally Posted by MadDog View Post
    Thanks Brewbuck,
    I tried what you suggested but shmids() doesn't seem to exist in Linux man pages; the only reference I found of it is in the Sun Microsystem system calls man pages... I'm not a linux/unix guru so I'm guessing that it's a function call that is proper to system running Solaris or something... I however got around that by reading /proc/sysvipc/shm as you suggested but ran into another problem.
    The problem is that ds.shm_perm.key gets reseted to 0 whenever the shared segment associated to shmid is marked for deletion and I can't tell if it's mine or not anymore...
    ftok() may return key in "int shmget(key_t key, int size, int shmflg);".
    BTW, PTHREAD_PROCESS_SHARED and PTHREAD_MUTEX_RECURSIVE are mutually exclusive.
    Last edited by cjrcl; 04-28-2010 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with shared memory
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 01-06-2009, 02:26 PM
  2. shared memory & mutex
    By lollobrigido in forum Linux Programming
    Replies: 3
    Last Post: 02-22-2008, 04:34 PM
  3. Threading and mutex's
    By megatron09 in forum C++ Programming
    Replies: 14
    Last Post: 09-07-2006, 02:40 PM
  4. thread and shared memory manipulation
    By taher84 in forum C Programming
    Replies: 4
    Last Post: 07-25-2006, 04:05 AM
  5. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM

Tags for this Thread