This program works fine if the user enters only 1 number on the command line. It will factor out the prime factors and output them to the console just fine.
J_10542741@cs3060:~/assn3$ ./assn3 12
12: 2, 2, 3,
My problem is when I test it on these two other cases:
A) Multiple Arguments:
J_10542741@cs3060:~/assn3$ ./assn3 10 8 6
10: 2, 5,
8: 2, 5,
6: 2, 5,
B) Using a Range of Numbers (i.e. {1..5}):
J_10542741@cs3060:~/assn3$ ./assn3 {1..5}
1:
2:
3:
4:
5:
I've looked around this site for anything regarding the return value of pthread_join() as well as searching through Google. I feel that it's a problem with this part of my code:
// FOR loop to join each thread w/ Main Thread 1x1
for(i = 0; i < argc-1; i++){
retCode = pthread_join(t_id[i], &factors);
results = factors;
if (retCode != 0){
// Print Error Message and return -1
fprintf(stderr, "Failure to join threads.");
return -1;
}
else{
printf("%s: ", argv[i+1]);
while(*results != 0){
printf("%d, ", *results);
results++;
}
}
free(factors);
printf("\n");
}
Here is the code in it's entirety:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
// Return a pointer to the pFactors array
void *primeFactors(void* number){
int *pFactors = malloc(sizeof(int)); // Dynamic Array for prime factors
int capacity = 0;
int size = 1;
int num = atoi((char*)number);
int prime = 2;
// If num < 4, then that number is already prime
if(num < 4)
pFactors[capacity] = num;
else{
while(num > 1){
while(num % prime == 0){
if(capacity == size){
size++;
pFactors = realloc(pFactors, size*sizeof(int));
}
num /= prime;
pFactors[capacity] = prime;
capacity++;
}
prime++;
}
}
if(capacity == size){
size++;
pFactors = realloc(pFactors, size*sizeof(int));
}
pFactors[capacity] = 0;
pthread_exit((void*)pFactors);
}
// MAIN FUNCTION
int main(int argc, char* argv[]){
int i, retCode; // retCode holds the value of successful/fail operation for pthread_create/join
int j = 1;
int* results;
void* factors;
//Thread Identifier value is equal to the number of actual int(s) in argv
pthread_t t_id[argc-1];
// Check argc for too few arguments
if(argc < 2){
fprintf(stderr, "Usage: ./assn3 <integer value>...");
return -1;
}
// Loop through argv and check argv[j] value to ensure it's >= 0
while(j <= argc-1){
if(atoi(argv[j]) < 0){
fprintf(stderr, "%d must be >= 0", atoi(argv[j]));
return -1;
}
j++;
}
// Create the thread
for(i = 0; i < argc-1; i++){
retCode = pthread_create(&t_id[i], NULL, primeFactors, *(argv+1));
if (retCode != 0){
// Print Error Message and return -1
printf("Failure to start thread. Error: %d\n", retCode);
return -1;
}
}
// FOR loop to join each thread w/ Main Thread 1x1
for(i = 0; i < argc-1; i++){
retCode = pthread_join(t_id[i], &factors);
results = factors;
if (retCode != 0){
// Print Error Message and return -1
fprintf(stderr, "Failure to join threads.");
return -1;
}
else{
printf("%s: ", argv[i+1]);
while(*results != 0){
printf("%d, ", *results);
results++;
}
}
free(factors);
printf("\n");
}
return 0;
}
To reiterate, this program works fine if I only enter in 1 argument. However, when there are multiple arguments, the program outputs the prime factors of the first argument correctly but the following arguments are printed with the prime factors of the first argument. Secondly, when you input a bash script range (i.e.{1..5}) it only prints out the arguments and not their respective prime factors. If there's something that needs clarification, feel free to ask. Also if there appears to be a duplicate/similar question(s) somewhere that I haven't been able to find, please let me know. Thanks.
pthread_create(&t_id[i], NULL, primeFactors, *(argv+1))
You are passing the same argument, *(argv+1)
, to every thread. Try with *(argv+1+i)
instead.