Write a main() program that searches the first five million integers for prime n
ID: 3687087 • Letter: W
Question
Write a main() program that searches the first five million integers for prime numbers by using two threads. A third thread is created to enable the user to check the search status of the two prime search threads while they are running. When the prime number search is completed the main function will combine the individual files "primes 1" and "primcs2" into a single output file "primes". Implement tcst_prime(), primc_scarch(), and mini_shcll() as library functions in firstinitiallastnamc_primc.c/h and main() in firstinitiallastname_lab3.c.Explanation / Answer
Answer for Question:
This below c program will serach the prime numbers
with the help of threads using mute x concept
#include <stdio.h>
#include <stdatomic.h>
#include <stdlib.h> //malloc
#include <stdbool.h>
#include <pthread.h>
typedef struct {
long int *plist;
long int length;
long int max;
} prime_s;
int add_a_prime(_Atomic (prime_s) *pin, long int new_prime){
prime_s p = atomic_load(pin);
p.length++;
p.plist = realloc(p.plist, sizeof(long int) * p.length);
if (!p.plist) return 1;
p.plist[p.length-1] = new_prime;
if (new_prime > p.max) p.max = new_prime;
atomic_store(pin, p);
return 0;
}
typedef struct{
long int i;
_Atomic (prime_s) *prime_list;
pthread_mutex_t *mutex;
} test_s;
void* test_a_number(void *vin){
test_s *in = vin;
long int i = in->i;
prime_s pview;
do {
pview = atomic_load(in->prime_list);
} while (pview.max*2 < i);
bool is_prime = true;
for (int j=0; j < pview.length; j++)
if (!(i % pview.plist[j])){
is_prime = false;
break;
}
if (is_prime){
pthread_mutex_lock(in->mutex);
int retval = add_a_prime(in->prime_list, i);
if (retval) {printf("Too many primes. "); exit(0);}
pthread_mutex_unlock(in->mutex);
}
return NULL;
}
int main(){
prime_s inits = {.plist=NULL, .length=0, .max=0};
_Atomic (prime_s) prime_list = ATOMIC_VAR_INIT(inits);
pthread_mutex_t m;
pthread_mutex_init(&m, NULL);
int thread_ct = 3;
test_s ts[thread_ct];
pthread_t threads[thread_ct];
add_a_prime(&prime_list, 2);
long int max = 1e6;
for (long int i=3; i< max; i+=thread_ct){
for (int t=0; t < thread_ct && t+i < max; t++){
ts[t] = (test_s) {.i = i+t, .prime_list=&prime_list, .mutex=&m};
pthread_create(threads+t, NULL, test_a_number, ts+t);
}
for (int t=0; t< thread_ct && t+i <max; t++)
pthread_join(threads[t], NULL);
}
prime_s pview = atomic_load(&prime_list);
for (int j=0; j < pview.length; j++)
printf("%li ", pview.plist[j]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.