2013-02-19 84 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(); 
...