The objective of this program is to implement a thread application that uses the
ID: 3774225 • Letter: T
Question
The objective of this program is to implement a thread application that uses the worker thread model. The specific application that you need to implement is a prime number program that finds the prime numbers less than 500,000. The program should use up to 5 threads to perform the task. A sixth thread should be used to print the final summary. You should implement the Sieve of Eratosthenes algorithm to identify the primes. Some thought should go into making the program efficient although the main objective is the correct implementation and synchronization of the threads. Depending upon your prime number method, each thread should 'select' the next number to either check for primeness or to eliminate the multiples of the number. Access to this common 'counter' should be protected so that only one thread at a time can access and update the counter. You should execute the program five times, once for each number of threads between 1 and 5. This will give you a chance to compare the outputs and processing times when using a different number of threads. Special consideration should be given to the data structures that must be global and shared by the threads. Be sure to protect access to the shared structures by mutexes and by condition variables where applicable. The thread examples on Blackboard should provide a framework that can be modified. Your output should be clearly labeled and understandable. The output should include the following elements: A title and description for the user of the program. A message that each thread has started. Each number that each thread checks for primeness or that it uses to mark off multiples of the number should be listed for those primesExplanation / Answer
# include <stdio.h>
# include <pthread.h>
void * primesum(void *);
int count=0;
int pno;
pthread_mutex_t pmt=PTHREAD_MUTEX_INITIALIZER;
int main()
{
int cnt1,cnt2;
int p;
int flag=1;
pthread_t thr;
printf("Enter Number to Count Prime Numbers (number<500000): ");
scanf("%d",&pno);
p=pno/2 + 1;
pthread_create(&thr,NULL,primesum,(void *)&p);
for(cnt1=2;cnt1<=pno/2;cnt1=cnt1++)
{
for(cnt2=2;cnt2<cnt1;cnt2++)
{
if(cnt1%cnt2==0)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("Thread processing prime number [ %d ] ",cnt1);
pthread_mutex_lock(&pmt);
count=count++;
pthread_mutex_unlock(&pmt);
}
else
{
flag=1;
}
}
pthread_join(thr,NULL);
printf("Total prime numbers are : %d ",count);
return 0;
}
void *primesum(void *no)
{
int *fr,cnt1,cnt2;
int flag=1;
fr=(int*)no;
for(cnt1=*fr;cnt1<=pno;cnt1=cnt1++)
{
for(cnt2=2;cnt2<cnt1;cnt2++)
{
if(cnt1%cnt2==0)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("Thread processing prime number [ %d ] ",cnt1);
pthread_mutex_lock(&pmt);
count++;
pthread_mutex_unlock(&pmt);
}
flag=1;
}
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.