进程间信号量有时无法按预期工作

发布于 2024-12-22 07:23:06 字数 1838 浏览 5 评论 0原文

我有以下 C 代码,其中以 sm 为前缀的变量由两个进程 proc1proc2 共享。因此信号量也是共享的。这段代码会被重复调用。因此,如果我说先前的值,那就意味着先前迭代的值。

我注意到在我的程序中,proc1有时会传递sem_wait(sem_f2l),而proc2则不执行sem_post(sem_f2l)。我注意到这一点,因为 sm_value_proc1sm_value_proc2 在我的程序中应该具有相同的值,它们也确实如此,这也由 printfs 与 >> 确认。 >。但是,具有 <<< 的 printf 有时会显示不同的值。差异是由于 proc1 打印了 sm_value_proc2 的先前值,因为 proc1 有时神秘地不等待 sm_f2lproc2 发布。

知道这里出了什么问题吗?

// semaphores are initialized like this -> sem_init( sm_l2f, 1, 0 );
// Note that sm_l2f and sm_f2l are pointers to sem_t

// sm_condition is assigned here by proc1

if ( is_proc1 )
{
    sem_post( sm_l2f );
    // proc2_value should be updated by now here, but sometimes sem_wait
    //  passes without waiting for proc2 to post sm_f2l!
    sem_wait( sm_f2l );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();
        printf( ">>> proc1 value = %u!\n", sm_value_proc1 );

        // If sem_wait and sem_post are working properly, printf will print
        //  the same value for sm_value_proc1 and sm_value_proc2 here, which it 
        //  sometimes doesn't, as the previous value of 
        //  sm_value_proc2 is printed.  
        printf( "<<< proc1 value = %u, proc2 value = %u, barrier_no = %d!\n",
                sm_value_proc1, sm_value_proc2, barrier_no[tid] );
    }
}
else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc2 = calc_value();
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

I have the following C code, where variables prefixed by sm are shared by two processes proc1 and proc2. Therefore the semaphores are also shared. This code is called repeatedly. So if I say previous value, that means value of a previous iteration.

I notice in my program that proc1 sometimes passes sem_wait( sem_f2l ) without proc2 executing sem_post( sem_f2l ). This I notice because sm_value_proc1 and sm_value_proc2 are supposed to be of same value in my program, which they are, as also confirmed by the printfs with >>>. However, the printf with <<< sometimes show different values. The difference is due to proc1 printing the previous value of sm_value_proc2 since proc1 mysteriously doesn't sometimes wait for sm_f2l to be posted by proc2.

Any idea what is going wrong here?

// semaphores are initialized like this -> sem_init( sm_l2f, 1, 0 );
// Note that sm_l2f and sm_f2l are pointers to sem_t

// sm_condition is assigned here by proc1

if ( is_proc1 )
{
    sem_post( sm_l2f );
    // proc2_value should be updated by now here, but sometimes sem_wait
    //  passes without waiting for proc2 to post sm_f2l!
    sem_wait( sm_f2l );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();
        printf( ">>> proc1 value = %u!\n", sm_value_proc1 );

        // If sem_wait and sem_post are working properly, printf will print
        //  the same value for sm_value_proc1 and sm_value_proc2 here, which it 
        //  sometimes doesn't, as the previous value of 
        //  sm_value_proc2 is printed.  
        printf( "<<< proc1 value = %u, proc2 value = %u, barrier_no = %d!\n",
                sm_value_proc1, sm_value_proc2, barrier_no[tid] );
    }
}
else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc2 = calc_value();
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2024-12-29 07:23:06

也许这是问题中的复制/粘贴错误(您正在使用真实代码中的复制/粘贴,对吧?),但看起来 proc2 的处理中有一个错误:

// ....

else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();  // <---  this should be assigning to
                                        //       sm_value_proc2
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

话又说回来,也许这是一个复制/粘贴错误实际的代码?

另外 - 不要忘记 sem_wait() 可以因信号而解除阻塞。

Maybe this is a copy/paste error in the question (you are using copy/paste from the real code, right?), but it looks like you have a bug in proc2's handling:

// ....

else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();  // <---  this should be assigning to
                                        //       sm_value_proc2
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

Then again, maybe it's a copy/paste error in the actual code?

Also - don't forget that sem_wait() can unblock due to a signal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文