EECE-4029 Operating Systems Fall 2016
Pthreads

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

Sample code and documentation

Archive:
   
pthreads.tar     All the code and documentation in a tar file
   
Makefile:
   
Makefile     Make all the code below with this
   
Code:
   
00-thread.c     Elementary pthread creation and destruction
01-thread.c     Shared and private address space
02-thread.c     Arguments passed to the threaded function
03-thread.c     Arguments passed unsafely to the threaded function
04-join.c     Simple join example
05-join.c     Use pthread_attr_setdetachstate to declare threads joinable
06-join.c     Use pthread_attr_setdetachstate to declare threads detached
07-stack.c     Manipulate the stack size of each thread
08a-mutex.c     Simple mutex example
08b-mutex.c     Simple atomic example - OS distributes threads to many procs
08c-mutex.c     Same as above but threads run one at a time
08d-mutex.c     Simple atomic example but user has threads run on just one proc
09-mutex.c     Find a dot product without threads
10-mutex.c     Same problem with threads
11-mutex.c     Another simple example
12-cond.c     Use of condition variables
setaffin.c     Distribute a computation to all processors - time it
setaffin_not.c     The computation proceeds on one processor only - time it
bug1.c     Improper use of condition variables
bug1fix.c     Broadcast a condition signal to fix the above
bug2.c     Not enough stack space for this process
bug2fix.c     Use pthread_attr_setstacksize
bug3.c     Incorrect application of pthread_create argument
bug3fix-1.c     Use one object per thread
bug3fix-2.c     Use an array of objects locally
bug3fix-3.c     Use an array of objects globally
bug4.c     A race condition
bug4fix.c     Race condition above is fixed
bug5.c     All threads die before doing any work
bug5fix.c     Keep the main thread alive until all threads complete
bug6.c     A global variable is corrupted due to improper thread synchronization
bug6fix-1.c     A mutex variable protects the global variable
bug6fix-2.c     A local variable is updated, when done the global is mutex-updated
bug6fix-3.c     An atomic hardware op updates the global sum
bug7.c     Without mutex variables, competing threads stomp on each other
bug7fix.c     With mutex variables the threads cooperate
bug8.c     Require one thread at a time to exec confirm and ping   (see dead.pdf)
bug8fix.c     Use two locks
bug9.c     Second thread may have already entered the 'last' loop when idx is incremented to be equal to LOOP
bug9fix.c     Return from the thread function if idx >= LOOP
 
Documentation:
assembly.pdf     Why can multiple threads cancel each others effects?
pthread[_object]_t     Declaration and description of variables
pthread_attr_t     pthread attribute type and usage
pthread_create     Create a pthread
pthread_exit     Destroy a pthread
pthread_cancel     Request by a thread to cancel another thread
pthread_attr_init     Initialize a thread attributes object attr
pthread_attr_destroy     Destroy a thread's attr object
sched_setaffinity     Allow thread to be assigned to a cpu
pthread_join     Suspend execution of the calling thread until the target thread terminates
pthread_detach     A thread's heap space can be reclaimed when the thread terminates
pthread_attr_setdetachstate     Sets the detachstate attribute in a threads attr object
pthread_attr_getdetachstate     Gets the detachstate attribute in a threads attr object
pthread_attr_getstacksize     Get stack size
pthread_attr_setstacksize     Set stack size
pthread_attr_getstackaddr     Get stack address
pthread_attr_setstackaddr     Set stack address
pthread_self     Unique thread id created by the OS
pthread_equal     True if two threads have the same id
pthread_once     Executes the "init_routine" exactly once in a process
pthread_mutex_init     Initialize a mutex object
pthread_mutex_destroy     Uninitialize a mutex object
pthread_mutexattr_init     Initialize a mutex attribute object
pthread_mutexattr_destroy     Uninitialize a mutex attribute object
pthread_mutex_lock     Acquire a lock on a specified mutex variable
pthread_mutex_trylock     Attempt to acquire a lock on a specified mutex variable
pthread_mutex_unlock     Give up a lock on a specified mutex variable
pthread_cond_init     Initialize a condition variable
pthread_cond_destroy     Uninitialize a condition variable
pthread_cond_wait     Uninitialize a condition variable
pthread_cond_signal     Uninitialize a condition variable
pthread_cond_broadcast     Uninitialize a condition variable
pthread_condattr_init     Initialize a condition attribute variable
pthread_condattr_destroy     Uninitialize a condition attribute variable