I have implemented peterson’ algorithm for mutual exclusion in shared memory. I am using shared memory between a c++ and a java process(using jni) for communication. The problem is that I am still seeing a race condition somehow. If I try to debug it using printf/println, the code starts running fine, but as soon as I remove those prints the processes stall.
Can somebody please enlighten me where could be the problem? I am quite sure that the implementation of peterson’s algorithm is correct.
Should I use semaphores(semget()) instead for mutual exclusion?
I have implemented peterson’ algorithm for mutual exclusion in shared memory. I am using
Share
To implement the algorithm properly you must use barriers to ensure that the memory is read/written in the order you specify in the code. Read the second paragraph from here:
And yes, use the synchronization mechanisms provided by the system (mutexes) instead of reinventing the wheel.