I've written a code which, at the final product is suppose to take in a file and use multi-threading to help locate all the prime values. Unfortunately that isn't working as planned, my code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <pthread.h>
#define maxIntArraySize 10000
struct sum_struct{
long long answer;
int * takeNum;
};
/// primality test, if n is prime, return 1, else return 0
int isPrime(int64_t n)
{
if( n <= 1) return 0; // small numbers are not primes
if( n <= 3) return 1; // 2 and 3 are prime
if( n % 2 == 0 || n % 3 == 0) return 0; // multiples of 2 and 3 are not primes
int64_t i = 5;
int64_t max = sqrt(n);
while( i <= max) {
if (n % i == 0 || n % (i+2) == 0) return 0;
i += 6;
}
return 1;
}
//Thread function to generate the # of primes in said thread
void* sum_runner(void* arg)
{
struct sum_struct *arg_struct = (struct sum_struct*) arg;
long long sum = 0;
int countTakeNum = sizeof(arg_struct->takeNum)/sizeof(int);
for(int p = 0; p <= countTakeNum; p++) {
if( isPrime(arg_struct->takeNum[p]) == 1) {sum = sum + 1;}
}
arg_struct->answer = sum;
pthread_exit(0);
}
// Takes out the first requires values
int *takeOut(int * list, int div)
{
static int r[100];
for (int h = 0; h < div; h++){
r[h] = list[h];
}
return r;
}
// Main Function
int main( int argc, char ** argv)
{
int nThreads = atoi(argv[1]);
int listOfNums[maxIntArraySize];
int currentSize = 0;
int listCounter = 0;
/// count the primes
printf("Counting primes using %d thread%s.\n", nThreads, nThreads == 1 ? "s" : "");
int64_t count = 0;
while( 1) {
int64_t num;
if( 1 != scanf("%ld", & num)) break;
//printf("%d\n",num);
listOfNums[listCounter] = num;
currentSize += 1;
listCounter += 1;
//if( isPrime(num)) count ++;
}
struct sum_struct args[nThreads];
int * removeNums = listOfNums;
int divider = currentSize/nThreads;
int lastThread = nThreads -1;
int takeOutSize = 0;
//first initialize the takeOuts
for (int j = 0; j < nThreads; j++){
if(j == lastThread){
args[j].takeNum = removeNums;
}else{
args[j].takeNum = takeOut(removeNums,divider);
removeNums = &removeNums[divider];
}
}
// Launcher Thread
pthread_t tids[nThreads];
for (int i = 0; i < nThreads; i++){
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, sum_runner, &args[i]);
}
for (int i = 0; i < nThreads; i++) {
pthread_join(tids[i], NULL);
printf("\nSum for thread %d is %11d\n", i, args[i].answer);
count += args[i].answer;
}
//int test = 5%3;
//printf("%d",test);
//return 0;
printf("Found %ld primes.\n", count);
return 0;
}
To elaborate on what is does, basically my code countPrimes.c
will take in a file and accept 1 int argument. So the the code would be like so: compile: gcc countPrimes.c -o2 -o count -lm -lpthread
run: ./count < test.txt 5
where test.txt contains the contents: 1 2 3 4 5 6 7 8 9 10 100 101 102 103 104 105 106 107
. Back on track, what my code is desired to do is use multi threading such that each thread will take total amount of nums / # of threads
. So in this case it would be 18 / 5
, so all threads will take in 3 indexes of the array, (them removing them from the OG array) except for the last array which would take whatever is remaining in the array. After that's all done it runs launches the threading at p_thread tids
each thread will be sent to sum_runner
function and scan through all the values in the given takeNum
and it will use the isPrime
to figure out which one's are prime. In the above case, I should expect 7 prime numbers, only I don't and I've tried just about everything from debugging, to putting print statement between lines to see what errors I'm generating but I can't seem to find the problem, I could really use some help.
转载于:https://stackoverflow.com/questions/53111140/designing-a-multi-thread-pthread-which-takes-in-a-file-and-sets-up-n-number-of