Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) Defne a global two-integer array, for storing prime numbers. Defne a global

ID: 3795090 • Letter: #

Question

(a) Defne a global two-integer array, for storing prime numbers. Defne a global variable that stores the index of the next cell available in the array (initiallyzero).

(b) Change the function compute prime() such that it always returns NULL. When it nds the n-th prime number, the number is stored in the next available cell in the array. In other words, the rst thread that completes, stores the result in the rst cell of the array. The second thread that completes, stores the result in the second cell of the array.
(c) The threads must cooperate to assure mutual exclusion when executing statements that access or update the array and index. (Hint: Use a mutex.)
(d) Upon completion of the two threads, the main program prints the computed primes and their product, using the content of the array.

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>
#include <stdbool.h>

typedef struct thread_data {
int n;
int result; } thread_data;

/* Inefficiently, compute successive prime numbers.
Return the nth prime number. */
void* compute_prime (void * arg) {
/* dereference the parameter */
thread_data * tdata = (thread_data *)arg;
int n = tdata->n;
int candidate = 2;

while (true) {
int factor;
int is_prime = true;

/* test primality by successive division. */
for (factor = 2; factor < candidate; ++factor)
if (candidate % factor == 0) {
is_prime = false;
break;
}
/* is prime number we're looking for? */
if (is_prime) {
if (--n == 0) {
/* define the result */
       tdata->result = candidate;
pthread_exit(NULL);
}
}
++candidate;
}
}

int main ()
{
pthread_t tid;
thread_data tdata;
time_t t;

/* intialize random number generator */
srand((unsigned) time(&t));
tdata.n=rand() % 5000;
printf("The %dth prime number", tdata.n);

/* start the thread, up to "tdata.n" */
pthread_create (&tid, NULL, compute_prime, (void *)&tdata);

/* wait for the thread to complete */
pthread_join (tid, NULL);

/* print the computed prime */
printf(" is %d. ", tdata.result);
}

Explanation / Answer

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>
#include <stdbool.h>

typedef struct thread_data {
int n;
int result; } thread_data;

/* Inefficiently, compute successive prime numbers.
Return the nth prime number. */
void* compute_prime (void * arg) {
/* dereference the parameter */
thread_data * tdata = (thread_data *)arg;
int n = tdata->n;
int candidate = 2;

while (true) {
int factor;
int is_prime = true;

/* test primality by successive division. */
for (factor = 2; factor < candidate; ++factor)
if (candidate % factor == 0) {
is_prime = false;
break;
}
/* is prime number we're looking for? */
if (is_prime) {
if (--n == 0) {
/* define the result */
       tdata->result = candidate;
pthread_exit(NULL);
}
}
++candidate;
}
}

int main ()
{
pthread_t tid;
thread_data tdata;
time_t t;

/* intialize random number generator */
srand((unsigned) time(&t));
tdata.n=rand() % 5000;
printf("The %dth prime number", tdata.n);

/* start the thread, up to "tdata.n" */
pthread_create (&tid, NULL, compute_prime, (void *)&tdata);

/* wait for the thread to complete */
pthread_join (tid, NULL);

/* print the computed prime */
printf(" is %d. ", tdata.result);
}