In previous readings, we were introduced to the concept of concurrency. In this reading we will look at two ways to achieve concurrency in our programs: processes and threads. For the latter, we’ll be introduced to several functions in the Pthreads library that will allow us to create and control threads.
Sections 12.1, 12.3 (excluding 12.3.8), and 12.4 (pp. 971-976, 985-995) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
pthread
.wait
and pthread_join
functions.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
In your own words, explain why processes having isolated address spaces can be both helpful and detrimental to achieving concurrency.
List the state that is private to a thread.
For each of the following functions, list whether it may be used as a thread routine for a pthread
, without making any modifications. Briefly explain your answers. If it is not possible to tell based on the given code, explain why.
void hello() {
printf("Hello world!");
}
void doSomething(void *meow) {
// We don't know the function body.
}
int addStuff(void *val) {
int x = *(int*)val;
return x + 7;
}
Write a C program that creates two pthreads. The first thread should run a routine that prints out the message “Pass the Tofurky!”, while the other should print out a contents of a global variable named “x
”. (You should declare x
in your program and set it to a value in main
before creating the threads.) Your main thread should join with both of the threads.
Briefly describe the four ways in which a thread may be terminated.