Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will write a threaded program in which threads write data to a shared buffer. You must ensure that a race condition

In this assignment, you will write a threaded program in which threads write data to a shared buffer. You must ensure that a race condition does not occur. You will write two versions of the program. The first version will use Pthreads (POSIX threads) and the second version will use C++ threads (which are built on top of Pthreads).

Implement a multithreaded solution to find if a given number is a perfect number. N is a perfect number if the sum of all its factors, excluding itself, is N.

You should ask the user for a number and the number of threads to use. The main program will create the required number of threads. The numbers 1 to N will be partitioned among these threads so that two threads do not work on the same number. For each number in this set, the thread will determine if the number is a factor of N. The parent thread should wait until all the threads complete. The parent will then determine if the input number is perfect and report the result.

Implementation Notes I would suggest that you write a non-threaded version of the program first to make sure you understand the algorithm. You do not need to demonstrate the non-threaded version in class. For both threaded versions you will need to write a function that can be used as the starting routine for the thread. This function will need to be given three arguments: the value of the number it is checking, the starting value for potential factors, and the ending value for potential factors. o You really only need to check factors between 1 and number / 2. Any number greater than number / 2 will not be a factor. o For example, if the number I am checking is 28 and I have requested 2 threads, one thread might check factors from 1 to 7 and the second thread might check factors from 8 to 14. For both implementations (Pthreads and C++ threads), you will need to link in the Pthread library. In CodeLite, you can do this by right-clicking on the project and selecting Settings. Choose the Linker tab and enter -lpthread in Linker Options. You may assume that the number we are checking can be stored in an uint64_t. Your threads should write the factors that they find to a shared buffer. Implement this buffer as an array of uint64_t. 2 o A buffer size of 100 should be sufficient for this project.

Sample Input and Output

(Printing the factors is optional but will help you to verify that your program works correctly.)

Enter number: 8589869056

Number of threads: 7

Number of factors is: 33

1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131071 262142 524284 1048568 2097136 4194272 8388544 16777088 33554176 67108352 134216704 268433408 2147467264 1073733632 3 536866816 4294934528

8589869056 is a perfect number

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Enhance the basic quality of your voice.

Answered: 1 week ago

Question

Describe the features of and process used by a writing team.

Answered: 1 week ago