0

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

我使用 boost网站上的示例创建了匿名信号量。信号量在开发机器上工作正常,但无法在测试机器上运行。一个进程在发布到另一个进程后卡在等待状态,但另一个进程尚未退出等待状态。

这是我的信号量减速:

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

系统信息:

太阳能 10

gcc:4.1.2 使用 binutils 2.18 自构建

提升 1.47

sparc架构

4

1 回答 1

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();
...
于 2013-02-22T12:47:03.023 回答