进程间信号量有时无法按预期工作
我有以下 C 代码,其中以 sm 为前缀的变量由两个进程 proc1 和 proc2 共享。因此信号量也是共享的。这段代码会被重复调用。因此,如果我说先前的值,那就意味着先前迭代的值。
我注意到在我的程序中,proc1有时会传递sem_wait(sem_f2l),而proc2则不执行sem_post(sem_f2l)。我注意到这一点,因为 sm_value_proc1 和 sm_value_proc2 在我的程序中应该具有相同的值,它们也确实如此,这也由 printfs 与 >> 确认。 >。但是,具有 <<< 的 printf 有时会显示不同的值。差异是由于 proc1 打印了 sm_value_proc2 的先前值,因为 proc1 有时神秘地不等待 sm_f2l由 proc2 发布。
知道这里出了什么问题吗?
// 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这是问题中的复制/粘贴错误(您正在使用真实代码中的复制/粘贴,对吧?),但看起来 proc2 的处理中有一个错误:
话又说回来,也许这是一个复制/粘贴错误实际的代码?
另外 - 不要忘记 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:
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.