Lab 2
Lab 2
In this laboratory students will implement concurrent and parallel programs using threads.
1 Threads
man pthreads(7) - Linux manual page – man7.org
man pthread_create(3) - Linux manual page – man7.org
man pthread_join(3) - Linux manual page - man7.org
In order to compile a program that uses threads, it is necessary to use the -lpthread gcc option.
Students should read the provided PDF to further understand how threads work and are
programmed:
The Linux Programming Interface – Sections 29.1 29.2 29.3 29.4 29.6
1.1 Exercise 1 – launching threads
Observe the test-threads_1.c program supplied, compile it and execute it with different input
values.
After running a few times with different input values, answer the following questions:
• What happens to the threads if the user presses enter and exits main?
• Read the pthread_create() man page and take note of the various arguments.
Although threads run concurrently at the same time inside the same program, it is possible to
wait for the termination of a specific thread.
This wait is accomplished calling the pthread_join() function. This function receives as first
argument the pthread_t value assigned by the pthread_create() and (for now) NULL as second
argument. This function will be blocked and wait until the corresponding thread terminates.
The fgets should be removed, but the program only terminates after the last threads
terminates.
• store each thread_id in a array of pthread_t and initialize this array as pthread_create()
is called.
• create a new loop at the end of the program that performs one pthread_join() for each
thread
• in each pthread_join() use the pthread_t value retrieved when creating the thread
It is possible to encapsulate in it pointers to data allocated (malloc) inside the thread or even
other 32/64 bit values (such as integers, floats, long doubles).
In test-threads_2.c the thread returns a random number, but there is way to retrieve it in main.
void * thread_ret;
pthread_join(thread_id, &thread_ret);
printf(“%d”, (int) thread_ret);
The following examples show how threads can return values of various types: int, float, string,
structures.
Hints:
• Pass such variable by reference (using the &) as the second argument of each
pthread_join()
Before calling the pthread_create() the data should be allocated and sent into the thread using
the last argument of the pthread_create(). Inside the thread it then becomes possible to
access the allocated data.
The next examples show how to send an integer or a string (array of characters) into a thread.
Integer string
long i = 0;
while( i < n_threads) {
pthread_create(&thread_id, NULL,thread_function, &i);
i++;
};
Each thread should receive a different value of i, but this does not happen.
Execute the program various times with various numbers of threads and try to explain
what is happening.
Hints:
• modify the pthread_create() so that the last argument receives the pointer previously
allocated and initialized;
• inside the thread declare an integer and copy there the value pointed by the thread
argument;
Parallelize the program creating 5 threads. Each thread receives one of the previous
numbers (2, 3, 5, 7 or 11 ), calculates how many multiples of the argument there are in
the array, and returns such value.