[Boost Interprocess] - Shared Memory File - Corruption
Hi, I am facing an issue with the 'Shared Memory File'.. Sometimes I get the 'SharedMemory File Name' as the first element of the structure that I used, to create a 'interprocess::list' and when this happens, it means that the SharedMemory got corrupted and my program crashes. Has anyone faced this issue, or does anyone know what might be wrong? Here's how I am using the Boost::Interprocess struct *struct_a* { char sym[6]; char desc[8]; double price; }; typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager; typedef boost::interprocess::allocator<*struct_a*, SegmentManager> shMemAllocator; typedef boost::interprocess::list<*struct_a*, shMemAllocator> shMemList; typedef boost::interprocess::managed_shared_memory shMemObject boost::scoped_ptr< boost::interprocess::managed_shared_memory > m_shMemPtr; shMemList* m_pList; m_shMemPtr.reset( new shMemObject(boost::interprocess::open_or_create, "shmemfile.shmem" , 100000*sizeof(*struct_a*))); m_shMemPtr->reserve_named_objects(100000); m_pList = m_shMemPtr->find_or_construct<shMemList>("SHMEM_NAME")(m_shMemPtr->get_segment_manager()); struct_a stOne; strcpy(stOne.sym, "ABCDE"); strcpy(stOne.desc, "FGHIJK"); stOne.price = 10.25; m_pList->push_back(stOne); Another program Opens this sharedmemory file and reads the struct from the list and '*pops*' it. What happens sometimes is, In the first field of the struct I see '* SHMEM_NAME*' and the program crashes when this happens. I am not sure what's causing the *struct.field *to comein as Sharedmemory name? If I clean the shared memory file and restart it works fine but I end up with this problem some ther time. This does not happen regularly but it sure does. I'd really appreciate any help figuring the solution out or if there's any mistake I am making. Thanks in Advance, Srinivas Gardas
Srinivas Gardas escribió:
Hi, I am facing an issue with the 'Shared Memory File'.. Sometimes I get the 'SharedMemory File Name' as the first element of the structure that I used, to create a 'interprocess::list' and when this happens, it means that the SharedMemory got corrupted and my program crashes. Has anyone faced this issue, or does anyone know what might be wrong?
I don't see any problem with your approach, can you give me more information? Also, take in care that you need to use mutexes to protect concurrent accesses to the list (an interprocess_mutex constructed in the segment should work). Regards, Ion
Hi Ion,
Probably that's the reason.. I haven't used any mutexes thought it's all
implemented in the 'interprocess'. I am kind of new to this stuff.. Could
please give me a code example where 'interprocess_mutex' is used by a
'Server' and 'Client' to lock the managed_shared_memory ?
I really appreciate your help.
Thanks & Regards
Sreenu
On 1/8/08, Ion Gaztañaga
Hi, I am facing an issue with the 'Shared Memory File'.. Sometimes I get the 'SharedMemory File Name' as the first element of the structure that I used, to create a 'interprocess::list' and when this happens, it means that
Srinivas Gardas escribió: the
SharedMemory got corrupted and my program crashes. Has anyone faced this issue, or does anyone know what might be wrong?
I don't see any problem with your approach, can you give me more information? Also, take in care that you need to use mutexes to protect concurrent accesses to the list (an interprocess_mutex constructed in the segment should work).
Regards,
Ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi,
I am a little confused about how to make 'interprocess_mutex' part of the
'managed_shared_memory' object which actually represents the shared_memory?
Is there a base class or something that can take the mutex?
I placed an '*interprocess_mutex*' as a member variable *in the class that
contains my 'managed_shared_memory'* object as a member variable and was
locking it in the methods that accessed the shared memory. I guess this did
not work out as I believe what I did effectively locked the object of my
class that was holding the embers. And since this is not actually the shaed
resource between the processes it didn't work out..
I'd really appreciate if you could provide an example of just show me bbased
on my initial code.. how the 'managed_shared_memory' can be locked using the
mutex.
Sreenu
On 1/9/08, Srinivas Gardas
Hi Ion, Probably that's the reason.. I haven't used any mutexes thought it's all implemented in the 'interprocess'. I am kind of new to this stuff.. Could please give me a code example where 'interprocess_mutex' is used by a 'Server' and 'Client' to lock the managed_shared_memory ?
I really appreciate your help.
Thanks & Regards Sreenu
On 1/8/08, Ion Gaztañaga
wrote: Hi, I am facing an issue with the 'Shared Memory File'.. Sometimes I get
'SharedMemory File Name' as the first element of the structure that I used, to create a 'interprocess::list' and when this happens, it means that
SharedMemory got corrupted and my program crashes. Has anyone faced
Srinivas Gardas escribió: the the this
issue, or does anyone know what might be wrong?
I don't see any problem with your approach, can you give me more information? Also, take in care that you need to use mutexes to protect concurrent accesses to the list (an interprocess_mutex constructed in the segment should work).
Regards,
Ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Srinivas Gardas escribió:
Hi, I am a little confused about how to make 'interprocess_mutex' part of the 'managed_shared_memory' object which actually represents the shared_memory? Is there a base class or something that can take the mutex?
I placed an '*interprocess_mutex*' as a member variable *in the class that contains my 'managed_shared_memory'* object as a member variable and was locking it in the methods that accessed the shared memory. I guess this did not work out as I believe what I did effectively locked the object of my class that was holding the embers. And since this is not actually the shaed resource between the processes it didn't work out..
I'd really appreciate if you could provide an example of just show me bbased on my initial code.. how the 'managed_shared_memory' can be locked using the mutex.
Sreenu
Just create the mutex like you create the list and use scoped_lock to
lock and unlock it more easily (pseudo_code, not compiled):
using namespace boost::interprocess;
interprocess_mutex *mut = m_shMemPtr->find_or_construct
Thanks so much Ion!
It worked fine.. now I just have to wait and see that my problem doesn't
arise again..
Sreenu
On 1/10/08, Ion Gaztañaga
Hi, I am a little confused about how to make 'interprocess_mutex' part of
'managed_shared_memory' object which actually represents the shared_memory? Is there a base class or something that can take the mutex?
I placed an '*interprocess_mutex*' as a member variable *in the class
contains my 'managed_shared_memory'* object as a member variable and was locking it in the methods that accessed the shared memory. I guess this did not work out as I believe what I did effectively locked the object of my class that was holding the embers. And since this is not actually the shaed resource between the processes it didn't work out..
I'd really appreciate if you could provide an example of just show me bbased on my initial code.. how the 'managed_shared_memory' can be locked using
Srinivas Gardas escribió: the that the
mutex.
Sreenu
Just create the mutex like you create the list and use scoped_lock to lock and unlock it more easily (pseudo_code, not compiled):
using namespace boost::interprocess;
interprocess_mutex *mut = m_shMemPtr->find_or_construct
("PROCESS-SHARED MUTEX")(); struct_a stOne; strcpy(stOne.sym, "ABCDE"); strcpy(stOne.desc, "FGHIJK"); stOne.price = 10.25;
{ //Begin of locked scope
//Now lock the mutex to protect concurrent access //to the list scoped_lock
locker(*mut); //Do the operation m_pList->push_back(stOne);
//Mutex will be unlocked by locker's destructor } //End of locked scope
The other process should also lock the same mutex when operating with the list so that one process is blocked while the other one is manipulating the list. There are several alternatives to build the mutex in the segment but I think this is the simplest one.
Regards,
Ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Ion Gaztañaga
-
Srinivas Gardas