Your assignment is to write a program ‘hw3.c’, which uses multiple threads to improve the performance (save time). An easy approach is splitting the array into pieces and each piece is handled by different thread. Following is the example of how to compile and run ‘hw3.c’. Don’t mind the warning message. Note that each thread is working on different parts of the array.
#define BUFFER_SIZE 4000000
int countPrime=0;
int numbers[BUFFER_SIZE];
int isPrime(int n)
{
int i;
for(i=2;i<n;i++)
if (n%i==0)
return 0;
return 1;
}
int main()
{
int i;
// fill the buffer
for(i=0;i<BUFFER_SIZE;i++)
numbers[i] = (i+100)%100000;
// Computation
for(i=0;i<BUFFER_SIZE;i++)
if (isPrime(numbers[i]))
countPrime++;
printf("count of prime numbers =
%d\n",countPrime);
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define MIN_USER_INPUT 2
#define BUFFER_SIZE 4000000
void *getUserInput(){
int *number_from_user=(int
*)malloc(sizeof(int));
puts("Enter a number to print all prime numbers in range [0 - number]");
while(1){
printf("Input :
");
scanf("%d",number_from_user);
if(*number_from_user<MIN_USER_INPUT ||
*number_from_user>BUFFER_SIZE){
printf("Please Enter a positive number from %d to
%d\n",MIN_USER_INPUT,BUFFER_SIZE);
///rewinds the input buffer (if user gives character type input it
prevents infinite while loop
rewind(stdin);
}else break;
}
puts("Input thread received user input.");
///writing number_from_user to the second
argument (a generic pointer) passed in thread join call
pthread_exit(number_from_user);
}
int checkPrime(int number){
if(number==1) return 0; ///1 is not prime
int i;
///(int) casting on the next line is not
necessary.
///just making sure that it does not cause
a bug with other compilers
/// for a different architecture in which
int division might return floating point number
for(i=2; i<=(int)(number/2); i++)
if(!(number%i)) return
0; ///Given number is not prime.
///Control reached here which means given number
is prime
return 1;
}
void *printPrimeUntil(void *argument){
int number = *((int *)argument),i,count=0;
printf("Thread two received : %d\n",number);
for(i=2; i<=number; i++){
if(checkPrime(i)){
printf("%5d",i);
if(!(++count%5)) ///print 5
prime number on a single line then move to next line
printf("\n");
}
}
pthread_exit(NULL);
}
int main(){
pthread_t input_thread, output_thread;
void *ret_number;
//first thread
if(pthread_create(&input_thread,NULL,getUserInput,NULL))
return -1*printf("Error
in thread creation!!!\n");///return a negative integer
pthread_join(input_thread,&ret_number);
int *number = (int *)ret_number;
//second thread
if(pthread_create(&output_thread,NULL,&printPrimeUntil,(void
*)number))
return -1*printf("Error in
thread creation!!!\n");///return a negative integer
return pthread_join(output_thread,NULL);
///wait for thread to finish
}
Your assignment is to write a program ‘hw3.c’, which uses multiple threads to improve the performance...
In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...
I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...
Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...
//In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j; //the staring and ending index unsigned long freq[DIGITS]; // results }; //initialize the array void init_array(unsigned...
Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...
How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex). Replace the codes (for mutex) by the codes (for reader-writer lock). To initialize reader-writer lock, pthread_rwlock_initializer. At the end,...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
Please explain your answers thoroughly for the following question. 1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually differs. a) Why is the variable myglobal usually not 40? Be specific, be precise. b) In what special case would myglobal be 40? Be specific, be precise. #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Purpose: Use 2 threads to increment myglobal exactly 40 times in total. Compile: using -pthread option */...
Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...