在solaris上增强库信号量

时间:2013-02-19 10:15:04

标签: c++ boost solaris semaphore solaris-10

我在RHEL中使用boost信号量,目前我正在将代码移植到solaris 10.我遇到了一个奇怪的问题,它会增强信号量无法正常工作。

我使用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架构

1 个答案:

答案 0 :(得分: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();
...