I was using boost semaphores in RHEL and currently i'm porting the code to solaris 10. I ran into a strange issue that boost semaphores are not working properly.
I have created anonymous semaphores using example on boost's website. The semaphores works fine on the dev machine but fails to operate on test machine. One process is stuck at wait state after posting to other but the other process has not came out of wait state.
Here is my semaphores deceleration:
...
//in global space
struct iSema
{
interprocess_semaphore ASync;
interprocess_semaphore BSync;
iSema()
:ASync(0), BSync(0)
{}
}*m_Sema;
mapped_region SemaRegion;
#define SHM_SIZE 512
...
...
//in main process 1
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object::remove("xyz"); //remove previous instance
shared_memory_object shm(create_only, "xyz", read_write); //create new
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write); //get into local scope region
SemaRegion.swap(region); //swap with global scope region
m_Sema = new (SemaRegion.get_address()) (struct iSema); //map it
}
catch(exception& e)
{//logging
}
...
//Do some thing
m_Sema->ASync.post();
m_Sema->BSync.wait();//stuck at this place
...
...
//in main second process
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object shm(open_only, "xyz", read_write);
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write);
SemaRegion.swap(region);
m_Sema = new (SemaRegion.get_address()) (struct iSema);
}
catch(exception& e)
{
//logging
}
m_Sema->ASync.wait();
m_Sema->BSync.post();
...
System Info:
solaris 10
gcc: 4.1.2 self build with binutils 2.18
boost 1.47
sparc architecture
This was entirely related to usage of semaphores and solaris implementation. In My case the process 1 was posting before the process 2 could open the shared memory for semaphores. Hence the process two was not getting any post from process 1. I got the above code working with minor changes listed below:
...
//in global space
struct iSema
{
interprocess_semaphore ASync;
interprocess_semaphore BSync;
interprocess_semaphore CSync;
iSema()
:ASync(0), BSync(0), CSync(0)
{}
}*m_Sema;
mapped_region SemaRegion;
#define SHM_SIZE 512
...
...
//in main process 1
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object::remove("xyz"); //remove previous instance
shared_memory_object shm(create_only, "xyz", read_write); //create new
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write); //get into local scope region
SemaRegion.swap(region); //swap with global scope region
m_Sema = new (SemaRegion.get_address()) (struct iSema); //map it
}
catch(exception& e)
{//logging
}
...
//Do some thing
m_Sema->CSync.wait();
m_Sema->ASync.post();
m_Sema->BSync.wait();
...
...
//in main second process
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object shm(open_only, "xyz", read_write);
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write);
SemaRegion.swap(region);
m_Sema = new (SemaRegion.get_address()) (struct iSema);
}
catch(exception& e)
{
//logging
}
m_Sema->CSync.post();
m_Sema->ASync.wait();
m_Sema->BSync.post();
...