EECE-4029 Operating Systems Fall 2016
Comparison of Locks

processes, mutex, semaphores, memory management, producer-consumer, files, deadlock, more..

Sample code and documentation

Archive:
   
locks.tar     All code and Makefiles in a tar file
   
Makefiles:
   
Makefile     For atomic.c, spinlock.c, mutex.c, semaphore.c in kernel threads
Makefile.other     For all other code (userland) below this
   
Kernel Code:
   
atomic.c     Using an atomic lock in a kernel module
spinlock.c     Using a spinlock in a kernel module
semaphore.c     Using a semaphore in a kernel module
mutex.c     Using a mutex in a kernel module
run_atom     Convenient way to run atomic.c (but edit yourself into /etc/sudoers first)
run_spin     Convenient way to run spinlock.c (but edit yourself into /etc/sudoers first)
run_sema     Convenient way to run semaphore.c (but edit yourself into /etc/sudoers first)
run_mutex     Convenient way to run mutex.c (but edit yourself into /etc/sudoers first)
 
Userland Code:
   
atomic_a.c     __sync_fetch_and_add to protect incrementing array index
atomic_b.c     __sync_fetch_and_add attempt to protect array index and array changes
atomic_c.c     __sync_fetch_and_add used twice to protect array index and array changes
atomic_d.c     No protection
spinlock.c     Using a spinlock in user space
semaphore.c     Using a semaphore in user space
mutex.c     Using a mutex in user space
atom.c     Some interesting CPU control functions plus atomic variables
 
Speed Tests:
   
test_atom.c     Test __sync_fetch_and_add
test_sema.c     Semaphore test
test_mutx.c     Mutex test
test_spin.c     Spin lock test
time_forks.c     Make 50000 forks
time_pthreads.c     Make 50000 threads
 
Documentation:
Memory barriers    
Difference between volatile and atomic