Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #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; } image text in transcribed

main.cpp: In function 'void* countPrimes (void*)': main.cpp:72:35: error: invalid conversion from 'void*' to 'returnData*" [-fpermissive] struct returnData *result = malloc(sizeof(struct returnData)); NNNNNNANNUR main.cpp: In function 'int main(int, char**)': main.cpp:86:28: error: invalid conversion from 'void*' to 'pthread_t* {aka long unsigned int*}' [-fpermissive] pthread_t *threads = malloc(t * sizeof(pthread_t)); //create t pthreads NNNNNN

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

Step: 3

blur-text-image

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

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago

Question

Explain the various techniques of Management Development.

Answered: 1 week ago