2013-02-19 38 views
0

我在RHEL中使用boost信号量,并且目前正在将代码移植到solaris 10.我遇到了一个奇怪的问题,它提高了信号量不能正常工作。在solaris上增强库信号灯

我使用boost的website上的示例创建了匿名信号量。信号量在开发机器上运行良好,但未能在测试机器上运行。一个进程在发布到其他进程后仍处于等待状态,但另一个进程没有退出等待状态。

这里是我的信号灯减速:

... 
//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(); 
... 

系统信息:

的Solaris 10

GCC 4.1.2建立自我到binutils 2.18

提升1.47

sparc架构

+0

哪个版本的Boost?你在两台机器上都有相同的版本吗? – 2013-02-19 10:17:01

+0

我使用提升1.47 – bikram990 2013-02-19 10:27:57

+0

有一点点击和跟踪我得到了,如果我在进程1中发布两次,那么它在测试机器上正常工作,但它总是在开发机器上失败。但它在测试机器上仍然有些失败。 – bikram990 2013-02-19 10:36:23

回答

1

这完全与使用信号量和solaris实现有关。在我的情况下,流程1在流程2可以打开信号量的共享内存之前发布。因此,流程2没有从流程1中获得任何信息。我​​得到了上面的代码,并在下面列出了一些小改动:

... 
//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(); 
...