Question
Please help with errors #include #include #include #include struct threadData { /*structure to pass data to thread function*/ int id, INC; }; struct returnData {
Please help with errors
#include
struct threadData { /*structure to pass data to thread function*/ int id, INC; };
struct returnData { /*structure to return data from the thread function*/ int count; double timeElapsed; };
int charArrayToInt(char *c) { /*function to convert char array to integers*/ int i, n=0; for(i=0; c[i]!='\0'; i++) { n = n*10 + (c[i]-'0'); } return n; }
int isPrime(int n) { /*function that returns 0 for non-prime numbers and 1 for prime numbers*/ int i; if(n
if(n == 2) // 2 is a prime number return 1;
for(i=2; i
return 1; //prime if not divisible by any number }
void* countPrimes(void *args) { /*the thread function*/ clock_t t; //to measure elapsed time t = clock(); //get time at start
struct threadData *data = (struct threadData*)args; //data passed to function int i, l, r, c=0; l = data->id * data->INC; //calculate range of number to work on r = (data->id + 1) * data->INC;
for(i=l; i t = clock() - t; //get time at end struct returnData *result = malloc(sizeof(struct returnData)); result->count = c; result->timeElapsed = ((double)t)/CLOCKS_PER_SEC; return (void*)result; //return the data using struct } int main(int argc, char **argv) { int i, t, INC; //read the command line arguments t = charArrayToInt(argv[1]); INC = charArrayToInt(argv[2]); pthread_t *threads = malloc(t * sizeof(pthread_t)); //create t pthreads void *threadResult; //to store return from threads for(i=0; i pthread_join(threads[i], &threadResult); //join thread i struct returnData *result = (struct returnData*)threadResult; //convert form void* to returnData* printf("id = %d, count = %d, elapsed time = %lf seconds ", i, result->count, result->timeElapsed); } return 0; }
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