ISU E4 - Posix Thread

Posix Threads

Goal

When you have completed this exercise, you will
_ have learned how to create Posix threads
_ have experienced the shared data problem for multithreaded programs
This is a rather large exercise, but it serves to exhibit important fundamentals about threading
and shared data in particular. Make sure to complete and understand it.


Exercise 1 Creating Posix Threads

Write a program that creates two threads. When created, the threads must be passed an ID
which they will print to stdout every second along with the number of times the thread has
printed to stdout. When the threads have written to stdout 10 times each, they shall terminate.
The main() function must wait for the two threads to terminate before continuing


Code:





pthread_create arguments:

  • Int pthread_create(thread, attr, start_routine, arg)
    • thread: An opaque, unique identifier for the new thread returned by the subroutine.
    • attr: An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes bject, or NULL for the default values.
    • start_routine: the C routine that the thread will execute once it is created.
    • arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.
  • returns zero if successful.
  • returns an error number if an error occurs.
pthread_join - wait for thread termination.
  • Int pthread(threadID, status)
    • returns zero if successful.
    • returns an error number if an error occurs.


Execution:


It is clear that it doesn’t print correct due to the scheduler, who determines the timeslices.

What happens if function main() returns immediately after creating the threads? Why?

The two threads are terminated because the process is terminated.

The seemingly easy task of passing the ID to the thread may present a challenge; In your chosen solution what have you done? Have you used a pointer or a scalar?

We have used an array for each ID and a pointer is used pass the ID to the thread function.



Exercise 2 Sharing data between threads

Create a program that creates two threads, incrementer and reader. The two threads share an
unsigned integer variable named shared which is initially 0. incrementer increments shared
every second while reader reads it every second and outputs it to stdout.

Code:


Are there any problems in this program? Do you see any? Why (not)?

There is no problems in this code. When we execute the code it counts to 10. We see no problem because only one of the threads is writing to the shared variable. Additionally we only write or read every one second.


Exercise 3 Sharing a Vector class between threads

The supplied class Vector1 holds 10.000 elements per default, which at all times must have the same value. Vector::setAndTest() sets the value of the elements and then immediately checks that the Vector object is consistent (all elements hold the expected value).
Create a thread function writer that uses Vector::setAndTest() to set and test the value of
a shared Vector object. Then create a main() function that creates a user-defined number of
writer threads (between 1 and 100), each with their own unique ID. Let each writer set and
test the shared Vector object to its ID every second. If a writer detects an inconsistency in the
shared Vector object (i.e. setAndTest() returns false), it should write an error message.
Run the program with a number of threads.

Code:



Execution:


Do your writers detect any problems? Are there any problems in this program? Do you see them? Why do (not) see them?

When we run the program, we get an error. This error occurs because the thread function is non-atomic. This means that we do not have any idea how long one thread gets before it is interrupted. For example, if thread 1 has started to write "1" to the vector and is interrupted when it is half way. Then thread 2 starts to write "2" in the vector. After that thread 1 continues from where it came, and writes "1" to the rest of the vector and thinks there is "1" in all of the vectors pitches. After a test, it finds out that this is not true.


Exercise 4 Tweaking parameters

Modify your program from exercise 3 so that the writers loop time is no longer one second but a user-defined number of microseconds. Experiment with the number of writers created and shorter loop time as well as the number of elements in the Vector - do you see any problems?
Explain when and why they start to occur, and why you did not see them in exercise 3.

If we lower the time more errors start to occur. This is a logic response to the shorter looping time because there is no protection. Because the threads have a shorter run time they will start to collide more and more often.


Exercise 5 Testing on target

Recompile the solution from exercise 3 and test it on target following the same line of thinking
as in exercise 4. Compare your findings with those in that of exercise 4.
Are the parameters that you found to present problems on the host the same that yield problems on the target? Why do you experience what you do?

Executing exercise 3 on target gives many more errors than on host, due to less cpu power and only one core.

1 kommentar:

  1. Hi Group 32 (SHJ and RHN)

    As you write, the goal is to learn how to create and handle Posix threads and get some experience with the problem of shared data and multithreaded programs gives.

    In exercise 2, you are right that there is no concurrency problem in relation to modify the shared unsigned integer, but you could be able to see that the scheduler sometimes do a read operation before a write operation and the other way around.

    Instead of copying the questions directly from the given exercise, it would nice if you could use your own words to describe what your job is and eleborate a bit about the goal of the exercise.

    In exercise 4, you are asked to experiment with the number of writers created, shorter loop time and number of elements in the Vector. It seems like you have only tested your code with shorter loop time.

    Overall we think that your answers is very good and that you describe the problem you encounter fine. We have approved it.

    SvarSlet