Lab 5: Introduction to PThreads

This lab introduces basic synchronization concepts through the use of PThreads. We discuss basic usage of PThreads (creating, destroying, waiting on) and also illustrate (and debug) a simple race condition.

Lab Materials
  1. Slides
  2. Lab Files
Assignment
You need to complete the implementation of  ptcount_atomic.c and ptcount_mutex.c. When you are finished, the main process of  ptcount should create three pthreads and wait for these pthreads to complete execution. Each thread should decrement (in a loop) a shared variable named  count as well as a local counter. When the child threads are finished executing, the main thread should print out the value of  count. The value reported by the main process should be consistent with what you would expect with the given loop bound and decrement values. Your program should match the following output:

	bash$make test
	gcc -pedantic -Wall -std=gnu11 -g -o ptcount_mutex ptcount_mutex.c -lpthread
	time ./ptcount_mutex 100000000 1
	Thread: 1 finished. Counted: 0
	Thread: 0 finished. Counted: 0
	Thread: 2 finished. Counted: 0
	Main(): Waited on 3 threads. Final value of count = 0. Done.
	27.94user 28.98system 0:21.13elapsed 269%CPU (0avgtext+0avgdata 1912maxresident)k
	48inputs+0outputs (1major+89minor)pagefaults 0swaps
	time ./ptcount_atomic 100000000 1
	Thread: 2 finished. Counted: 0
	Thread: 0 finished. Counted: 0
	Thread: 1 finished. Counted: 0
	Main(): Waited on 3 threads. Final value of count = 0. Done.
	18.48user 0.01system 0:06.19elapsed 298%CPU (0avgtext+0avgdata 1984maxresident)k
	40inputs+0outputs (1major+91minor)pagefaults 0swaps

You also need to archive your lab for submission. For this step, you should use the 'zip' target included in the lab's Makefile. Change the STUDENT_ID variable in the Makefile to your student ID and type:
    make zip
Please ensure that you have tested your final version of the source code files on the cycle servers, before submission.

 

Evaluation

< Back to the Lab Home Page