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 #include #include #include #include double time1() { struct timeval tv;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!