To illustrate how to implement a manager/workers formation using threads, we used a very simple prime testing routine, implemented in the program isprime.c. We use copper to run the concurrent version as follows:
cu12:~/Notes/L27221% cc_r -o workers workers.c cu12:~/Notes/L27222% workers Give the number of workers : 3 Give a natural number : 1233 Give a natural number : 1234 Give a natural number : 1235 The number 1233 is divisible by 3. The number 1234 is divisible by 2. The number 1235 is divisible by 5.Observe the similarity in the program workers.c with the MPI_Send/MPI_Recv paradigm we have used. Of course, for some numbers one may very quickly find a divisor, while the run time for the prime numbers will be the largest.
To illustrate mutual exclusion for shared variables, the program bank.c uses a so-called monitor procedure, locking and unlocking the critical section where the withdrawal operations happens.
At the of the lecture, we returned to the the "dining philosophers" example, indicating how binary semaphores may not suffice to prevent deadlock. Instead, one should use a counting semaphore to keep track of the number of eating philosophers. The number of locks should never be maximized in case no one eats.
Bibliography