Put all of the code for this assignment in a fresh directory. For each part, perform the following tests from Assignment 4: tests 9, 10, 17, 18, 19. Since our starting point is Part 2 of Assignment 4, incorrect results may be obtained for some of the tests on some of the parts of this assignment.
Part 0
We will have to handle errors differently if we are not going to use the
return value of findFactors1 or findFactors2.
Have saveValue set a global error
number (not errno) in case an error occurs. Check this value each time
findFactors1 or findFactors2 returns.
Terminate the program with an appropriate error message if an error occurs.
Answer the following questions:
In this case we have two concurrent threads looking for primes and
storing the results using saveValue.
Answer the following questions:
Part 2
Part 3
We cannot do this using pthread_cancel since findFactors2
does not have any cancellation points. Consider using the following
method. Have a global variable which is initially false and set to
true when findFactors1 finds a factor. Have findFactors2
examine this value in its loop and terminate if the value it true.
Part 4
Part 5
Fix the problem by having saveValue reject multiple factors.
Now when all threads have completed, check each factor to see how many times
it divides the original value.
Part 6
Part 7
Handing in your assignment
In addition, run the tests listed above (with timing) for each part of the
assignment. Do all of the tests on the same type of machine, all at home,
or all in the lab. If you do the tests on UTSA machines, try to do all
of them on main2xx machines which are mostly identical.
Check the load average before running your tests.
(You can do this with the w command.) If the load average is high,
note this with your output.
Answer the questions for each part of the assignment. Please type your answers
and make sure each is labeled with the appropriate number.
Use this cover sheet.
Start with your program from Part 2 of Assignment 4.
Modify findFactors1 and findFactors2 so that they can be called
as threads. This means that each needs to have one pointer parameter and
return a pointer. We will not use the return value, so they can return
NULL. Instead of taking a parameter that is a long long,
have them each take one parameter that is a pointer to a
Part 1
Hint: Look at USP: Program 8.9, page 280.
Does this implementation have the same problem?
What were your results for test number 10?
Modify the program in Part 0 so that it creates threads for findFactors1
and findFactors2 and then joins the threads before printing the results.
Where this place is will
depend on your implementation, but there will always be such a place.
Now the other thread calls saveValue.
What can go wrong?
If not, why do you think it did not
occur?
Add appropriate synchronization to saveValue so that the program does
not have any synchronization issues. The simplest method is to use a
pthread_mutex_lock as in USP Program 13.2, on page 454, but other
methods are available. Answer the following question:
If findFactors1 finds a factor, findFactors2 will not
find a new prime factor.
Have findFactors1 kill off the findFactors2 thread
if it finds a factor.
How can this occur even if the second thread is killed?
In this part we will fix the problem of factors that are not prime.
You may use any method you like to do this. Here are two suggestions.
It may contain at most one number
that is not a prime factor (since findFactors2 cannot find more than
one factor).
For each number in the list, see if it is divisible by one of the others.
One problem still remains. It is possible for findFactors2 to find
a factor that findFactors1 has already found.
It is possible for your program to give the correct results even if
the program itself is correct. This will happen if the second thread is
killed before it finds a factor. Your program is not correct if it must
assume that one thread will execute faster than another.
Hint: you can find an example with three small distinct prime factors.
In Parts 3 and 4, if findFactors1 finds a factor and kills the
findFactors2 thread, only one CPU will be working on this
problem. Find a way to do what we did in Part 5 of Assignment 4 by having
the program use 2 threads on the reduced problem whenever a factor is
found.
This is a question about whether your program is correct, not about
whether it gave the right answers for the tests you tried.
What if we were to utilize 3 threads by breaking the problem into 3 parts:
    2 to sqrt(n)/3
    sqrt(n)/3 to 2*sqrt(n)/3
    2*sqrt(n)/3 to sqrt(n)
If the first one found a factor, would it be possible for one of the
others to find a prime factor? In other words,
Give an example involving small numbers.
Note: you do not need to implement this.
You will hand in the source code for each of the programming parts of the
assignment.