UTSA CS 4953 Special Studies in Computer Science:
Advanced Topics in Concurrency and Communication Fall 2003


Assignment 2, Due Thursday, October 9

In this assignment you are asked to run some experiments. Do this on at least two systems, say under Solaris and Linux. You can use Mac OSX for one of the systems if you have access to it. When writing up this assignment, be sure to indicate what hardware and OS you used. Be sure to state the clock rate of the CPU.


Part 0:
The book states that a mutex lock is the most efficient thread synchronization mechanism. Think about what this means. Design a program to test how long it takes to do a mutex lock followed by a mutex unlock when the lock is free. The time may be smaller than the resolution you can get with the clocks and timers we have discussed. You may have to time multiple mutexes to get accurate numbers. What can interfere with your timing? How do you know if your results are accurate?

Part 1:
Mutexes are not useful if, as in Part 0, they are always free. Think about the following:
How long does it take to do a mutex lock followed by a mutex unlock when the mutex is not free?
Don't think about this one too long.

Suppose that multiple threads are incrementing a shared integer.
First determine how fast a given thread can increment an integer in a loop. Now try this with 10 threads attempting to increment the same shared integer. Compare the throughput with 10 threads and the throughput with one thread. For this first experiment we are not doing any synchronization so the incrementing may not be done correctly. However, you should try to make sure that all of the threads are active concurrently.

Now protect the shared integer with a mutex. How does this affect the throughput? What happens to the throughput when there are 100 threads?

Part 2:
Are mutexes fair? That is, if 10 threads are incrementing a shared integer protected by a mutex, in what order do they do their work? Can one thread increment the integer many times before another thread get in? Design an experiment to test this. You may have to do more than just increment an integer in the mutex in order to keep track of which thread is doing the work. Think about the following: How did you make sure that all of the threads have started before keeping track of which thread gains the lock?

Parts 3-5:
Pick one of the following that is supported on the two systems you used for Parts 0-2 and redo these parts using this other synchronization method.

Part 6:
Find out about advisory record locking using fcntl or flock. This material is not in USP. Possible sources:

Pick a particular system to study the exclusive (write) blocking (LOCK_NB not used) locks on that system. In each case, lock the entire file rather than just part of the file.
Answer the following questions. Indicate how you got your answer, either by reading a document (say which one) or performing an experiment (include the source code and describe the experiment in words). Note that the answers may depend on the system.
  1. What happens if a process that owns a lock tries to obtain the lock again?
    If it can get the same lock twice, does it need to unlock it twice?
  2. Can the locks be used between threads or only between processes?
    Think about what this question means.
  3. How fast is the locking mechanism compared to the use of semaphores or mutexes? Test this between threads if possible or between processes otherwise. Think carefully about what this question means.

Handing in the assignment
You do not need a cover sheet. Hand in your source code along with a discussion describing the tests you did and comparing the results on your test systems. Include output when it will be helpful. Make sure you clearly label the different parts of the assignment.