Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

DTS202TC Foundation of Parallel Computing

Lab 8: Shared-Memory Programming with Pthreads

Task 1: Monte-Carlo Estimation with Pthreads (Semaphores)

Figure 1: Estimate the shaded area

Write a Pthreads program that uses semaphores and the Monte Carlo method for estimating the shaded area in Figure 1.  The main thread should read in the total number of tosses and print the estimate.  You may want to use long long ints for the number of hits and the number of tosses, since both may have to be very large to get a reasonable estimation.

Task 2

“Send a message” task has been introduced in the lecture. More specifically, suppose that we have t threads, thread 0 sends a message to thread 1, thread 1 sends a message to thread 2, ..., thread t-2 sends a message to thread t-1 and thread t-1 sends a message to thread 0..

After a thread receives” a message, it can print the message and terminate. Now please write a Pthreads program to achieve this task and make sure the receiving and printing messages are in sequence. For example, thread 0 should receive and print the message first. You can use the code pth msg sem.c on the virtual machine as a reference. The ”send message” function as shown in Figure 2:

Figure 2: Using semaphores so that threads can send messages

Task 3 (Optional)

Ask AI for more Pthreads practices if you have spare time.