Question
Write a C program that uses pthreads to compute the number of prime numbers between multiple ranges of specified low and high values, inclusive. Here
Write a C program that uses pthreads to compute the number of prime numbers between multiple ranges of specified low and high values, inclusive. Here is an example execution:
./p3 3 103 1000 2000 3000 4000
The first range is 3 through 103, the second is 1000 through 2000, and the third is 3000 through 4000, all inclusive. You can assume that there will be no more than 4 ranges of values, implying that you will need to create no more than 4 threads.
Note that you only count the total number of primes found in each range, and not the actual prime numbers themselves.
The mainline program should NOT perform any of the computation. Instead, it should create one pthread to examine each range.
BE SURE TO HAVE THIS AS THE FIRST EXECUTABLE LINE IN YOUR PROGRAM:
alarm(90)
Use time1 from timedemo.c in main to determine the amount of time between starting the threads and termination of all threads.
The print should contain EXACTLY 1 LINE OF OUTPUT containing these values: the total time the total number of primes the number of primes in range 1 the number of primes in range 2 etc
An example print for the above test might be:
0.014 281 26 135 120
where I just made up the 0.014 time.
timedemo.c :
#include
gettimeofday( &tv, ( struct timezone * ) 0 ); return ( (double) (tv.tv_sec + (tv.tv_usec / 1000000.0)) ); }
double time2() { return ( (double) time(NULL) ); }
void main(int argc,char *argv[]) { double t1, t2, t3, t4;
t1 = time1(); t2 = time2(); printf("%lf %lf ",t1,t2); sleep(2); t3 = time1(); t4 = time2(); printf("%lf %lf ",t3,t4);
printf("%lf %lf ",t3-t1,t4-t2); }
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