Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C program Goal: Multithreaded sieve of eratosthenes I need help modifying this code to make it work. It keeps seg faulting #include #include #include #include

C program

Goal: Multithreaded sieve of eratosthenes

I need help modifying this code to make it work. It keeps seg faulting

#include #include #include #include #include

typedef struct BitBlock { uint32_t bits; } BitBlock_t;

static uint32_t val = 10240; static BitBlock_t b[4]; static int num_threads = 1; static unsigned short isVerbose; static pthread_mutex_t* mutexes; static pthread_key_t key;

void setbit(BitBlock_t* a, uint32_t k) { a[k / 32].bits |= 1 << (k % 32); }

void clearbit(BitBlock_t* a, uint32_t k) { a[k / 32].bits &= ~(1 << (k % 32)); }

int testbit(BitBlock_t* a, uint32_t k) { return (a[k / 32].bits & (1 << (k % 32))) != 0; }

void* sieveoferatosthenes(void* vid) { long tid = (long)vid; uint32_t* own_b = pthread_getspecific(key); if (own_b == NULL) { own_b = calloc((val + 31) / 32, sizeof(uint32_t)); pthread_setspecific(key, own_b); }

for (uint32_t j = 2; j <= val; j++) { if (!testbit((BitBlock_t *) own_b, j)) { for (uint32_t i = j * j; i <= val; i += j) { setbit((BitBlock_t *) own_b, i); } } } pthread_exit(EXIT_SUCCESS); }

int getnext() { static int next = 0; pthread_mutex_lock(&mutexes[0]); int cur = next++; pthread_mutex_unlock(&mutexes[0]); return cur; }

int main(int argc, char* argv[]) { int opt = -1; pthread_t* threads = NULL; long tid = 0; for (int i = 0; i < 4; i++) {

b[i].bits = 0;

} pthread_mutex_init(&mutexes[0], NULL);

while ((opt = getopt(argc, argv, "t:u:hv")) != -1) {

switch (opt) {

case 't':

num_threads = atoi(optarg);

break;

case 'u':

val = atoi(optarg);

break;

case 'h':

printf("%s: -t # -u # -h -v ", argv[0]);

printf("\t-t # for number of threads ");

printf("\t-u # for the upper bound of largest number "); exit(0); break; case 'v': isVerbose = 1; break; default: /* '?' */ exit(EXIT_FAILURE); break; }

}

threads = malloc(num_threads * sizeof(pthread_t)); if(isVerbose) { fprintf(stderr, "number of threads: %d", num_threads); fprintf(stderr, "upper bound: %d", val); }

for (tid = 0; tid < num_threads; tid++) { pthread_create(&threads[tid], NULL, sieveoferatosthenes, (void *) tid); }

for (tid = 0; tid < num_threads; tid++) { pthread_join(threads[tid], NULL); }

for (uint32_t p = 2; p <= val; p++) { if (testbit(b, p) != 1) printf("%u ", p); }

free(threads); pthread_mutex_destroy(&mutexes[0]);

return EXIT_SUCCESS; }

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

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

What are the characteristics of useful information?

Answered: 1 week ago

Question

Describe and discuss the different NFTS Security Attributes.

Answered: 1 week ago