2016-02-21 82 views
-1

如果用戶在命令行中只輸入1個數字,該程序將正常工作。它會分解主要因素並將它們輸出到控制檯。爲什麼每次調用pthread_join()時pthread_join()的返回值都不會發生變化

[email protected]:~/assn3$ ./assn3 12 
12: 2, 2, 3, 

我的問題是,當我測試了這兩種其他情況:

A)多參數:

[email protected]:~/assn3$ ./assn3 10 8 6 
10: 2, 5, 
8: 2, 5, 
6: 2, 5, 

B)使用數範圍(即{1..5 }):

[email protected]:~/assn3$ ./assn3 {1..5} 
1: 
2: 
3: 
4: 
5: 

我環顧這個網站尋找任何有關pthread_join()的返回值以及搜索t谷歌。我覺得它與我的這部分代碼存在問題:

// 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"); 
    } 

下面是代碼在它的全部:

#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; 
} 

要重申,這個程序工作正常,如果我只在1個參數輸入。但是,當有多個參數時,程序會正確輸出第一個參數的主要因子,但是下面的參數會使用第一個參數的主要因子進行打印。其次,當你輸入一個bash腳本範圍(即{1..5})時,它只打印參數而不打印它們各自的主要因素。如果有什麼需要澄清的話,請隨時詢問。此外,如果在某處我似乎找不到重複/相似的問題,請告訴我。謝謝。

回答

2
pthread_create(&t_id[i], NULL, primeFactors, *(argv+1)) 

您正在向每個線程傳遞相同的參數*(argv+1)。改爲使用*(argv+1+i)

+2

對於'{1..5}'作爲參數的問題,當'(num <4)'時primeFactors()'處理'pFactors'數組時存在一個錯誤。 –

+0

謝謝kaylum!我之前沒有看到。我想我一直盯着屏幕看看它早些時候。 – natekelsey

+0

謝謝Michael Burr。我會看看我能做些什麼來讓它有希望地發揮作用。 – natekelsey

相關問題