Interprocess semaphores sometimes not working as expected

后端 未结 1 1558
面向向阳花
面向向阳花 2020-12-21 02:11

I have the following C code, where variables prefixed by sm are shared by two processes proc1 and proc2. Therefore the sem

相关标签:
1条回答
  • 2020-12-21 02:59

    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.

    0 讨论(0)
提交回复
热议问题