next up previous
Next: Implement semaphores Up: Assignment 6: Synchronization Primitives Previous: Reading

Break the lock

1.
In class I presented a simple implementation of locks that does not really guarantee mutual exclusion. You can pick up this code from the class web page. It is in a tar file called lock.tar. lock.c is the incorrect implementation; lock.s is (I think) correct.
2.
Write a program that uses at least two threads (created with sproc) and that accesses a shared variable concurrently. Use your lock to enforce exclusive access to the shared variable. Now test whether your program is in fact achieving mutual exclusion. There are many ways to do this. Here is just one suggestion: make the shared variable a counter that hands out unique identifiers in sequence. Create a big array and count the number of times each identifier is handed out. If the lock is working correctly, each array element should be 1.

3.
What is the frequency of synchronization errors using the defective lock implementation?

4.
Replace the defective lock implementation with the correct one and confirm that the error rate goes to zero.

NOTE: What if each thread were to put its pid into the array? Then the array would contain long streaks of one pid followed by long streaks of another pid. Does that give you any ideas?


next up previous
Next: Implement semaphores Up: Assignment 6: Synchronization Primitives Previous: Reading
Allen B. Downey
4/7/1998