Question
write C code a naive multithreaded parallel prime number generator using pthreads. Basically the goal is this: launch NUM_THREADS individual threads, each engaged in a
write C code a naive multithreaded parallel prime number generator using pthreads. Basically the goal is this: launch NUM_THREADS individual threads, each engaged in a potentially time-consuming brute force algorithm (described below) and nally join on all the threads collecting the generated numbers.
(A followup tweak to this assignment will involve using synchronization primitives to wait until NUM_RESULTS are generated by the threads, and then programatically terminating the program.)
Thread task The task given to your threads is naive and computationally expensive, by design. Each thread should perform the following steps: 1. randomly generate an unsigned long int 2. test for primality by looking for factors with brute force iteration (iteratively divide by numbers until a remainder is zero). 3. repeat 1-2 until a number is found or MAX_TRIES has been reached. Upon completion of this algorithm, the thread should return a value to the spawning thread (via return or pthread_exit) that is either the random prime, or a 0 value indicating that MAX_TRIES has been reached. In my code, Im setting NUM_THREADS to 100 and MAX_TRIES to 10. Make these variable or preprocessor macros so that you can experiment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started