In this assignment, you will take a non thread-safe version of a hash table and modify it so that it correctly supports running with multiple threads.
This does not involve xv6; xv6 doesn't currently support multiple threads of execution, and while it is possible to do parallel programming with processes, it is tricky to arrange access to some shared resource.
Instead you will do this assignment on a multicore machine. Essentially any desktop machine made in the past 7 years or so should have multiple cores. On OS X you can check how many processors you have by running sysctl hw.ncpu
. On Linux (or in the Linux Virtualbox VM) you can run nproc
. If your Virtualbox VM does not currently have multiple processors, you can increase the number of processors by powering off the VM and then going to Machine -> Settings -> System -> Processor
and setting it to something greater than 1.
Start by downloading parallel_hashtable.c and compile it with:
$ gcc -pthread parallel_hashtable.c -o parallel_hashtable
Now run it with one thread:
$ ./parallel_hashtable 1
[main] Inserted 100000 keys in 0.006545 seconds
[thread 0] 0 keys lost!
[main] Retrieved 100000/100000 keys in 4.028568 seconds
So with one thread the program is correct. But now try it with more than one thread:
$ ./parallel_hashtable 8
[main] Inserted 100000 keys in 0.002476 seconds
[thread 7] 4304 keys lost!
[thread 6] 4464 keys lost!
[thread 2] 4273 keys lost!
[thread 1] 3864 keys lost!
[thread 4] 4085 keys lost!
[thread 5] 4391 keys lost!
[thread 3] 4554 keys lost!
[thread 0] 4431 keys lost!
[main] Retrieved 65634/100000 keys in 0.792488 seconds
Play around with the number of threads. You should see that, in general, the program gets faster as you add more threads up until a certain point. However, sometimes items that get added to the hash table get lost.
Find out under what circumstances entries can get lost. Update parallel_hashtable.c
so that insert
and retrieve
do not lose items when run from multiple threads. Verify that you can now run multiple threads without losing any keys. Compare the speedup of multiple threads to the version that uses no mutex -- you should see that there is some overhead to adding a mutex.
You will probably need:
pthread_mutex_t lock; // declare a lock
pthread_mutex_init(&lock, NULL); // initialize the lock
pthread_mutex_lock(&lock); // acquire lock
pthread_mutex_unlock(&lock); // release lock
You can use man
to get more documentation on any of these.
Does retrieving an item from the hash table require a lock? Update the code so that multiple retrieve
operations can run in parallel.
Update the code so that some insert
operations can run in parallel. Hint: if two inserts are being done on different buckets in parallel, is a lock needed? What are the shared resources that need to be protected by mutexes?
Upload the modified parallel_hashtable.c
and your partner.txt
to NYU Classes.
Credit: adapted from MIT 6.828 homework Threads and Locking