2013-04-05 66 views
1

我一直在牆上敲打我的頭幾個小時,現在試圖找出是什麼導致此段錯誤。由pthread產生的Segfault?

我發現段錯誤在pthread_mutex_lock(lock)行(38)上一直出現。我已經放置了兩條圍繞鎖的打印語句,但是隻打印了其中的一條,這是我斷定段錯誤發生在該指令處的理由。

我是否正確使用互斥鎖,或者我是否與陣列出錯(buffer[]numbermarker[]

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 

int* numbermarker = NULL; 
int* buffer = NULL; 
int pullposition = 0; 
int placeposition = 1; 
pthread_mutex_t *lock; 
int ceiling; 

/*This method places one of the primes in the buffer. It 
offers a safe way to manage where the next value will be placed*/ 
void placevalue(int value) 
{ 
    buffer[placeposition] = value; 
    placeposition++; 
} 

/*This method pulls the next prime and increments to the next prime in the list*/ 
int takevalue() 
{ 
    pullposition++; 
    return buffer[pullposition-1]; 
} 


void* threadmethod() 
{ 
    int k; 
    int l; 
    int firstval; 
    while(1) 
    { 
     while(numbermarker[buffer[pullposition]-1]==0) 
     { 
      printf("flag1 \n"); 
      pthread_mutex_lock(lock); 
      printf("flag2 \n"); 
       numbermarker[buffer[pullposition]-1] = 1; 
       l = takevalue(); 
      pthread_mutex_unlock(lock); 
      firstval = 1; 
      for(k=l+1;k<=ceiling;k++) 
      { 
       if(k%l != 0) 
       { 
        if(firstval) 
        { 
         placevalue(k); 
         firstval = 0; 
        } 
       } 
       else 
       { 
        numbermarker[k-1] = 1; 
       } 
      } 
     } 
    } 
} 


int main() 
{ 
int numthreads; 
int i; 

printf("Enter number of threads: \n"); 
scanf("%d", &numthreads); 

printf("Enter the highest value to check \n"); 
scanf("%d", &ceiling); 

    /* This will hold 1's and 0's. 
     1 = number has been checked or is 
      confirmed not to be a prime 
     0 = number is a possible prime 

    The idea behind these values is that the next 
    prime can always be identified by the 0 with 
    the lowest index*/ 
numbermarker = (int*)malloc(sizeof(int)*(ceiling)); 

    /*This will hold the primes as they are found*/ 
buffer = (int*)malloc(sizeof(int)*(ceiling)); 

for(i=0; i<ceiling; i++) 
{ 
    if(i<1) 
    { 
     numbermarker[i] = 1; 
    } 
    else 
    { 
     numbermarker[i] = 0; 
    } 

    buffer[i]=0; 
    printf("%d \n",numbermarker[i]); 
} 

placevalue(2); 

pthread_t **tid = (pthread_t **) malloc(sizeof(pthread_t *) * numthreads); 


for(i=0;i<numthreads;i++) 
{ 

    tid[i] = (pthread_t *) malloc(sizeof(pthread_t)); 
} 

for(i=0;i<numthreads;i++) 
{ 

    if(pthread_create(tid[i], 
       NULL, 
       threadmethod, 
       NULL)) 
    { 

     printf("Could not create thread \n"); 
     exit(-1); 
    } 
}  


int not_done = 1; 
int sum; 
while(not_done) 
{ 
    sum = 0;  
    for(i=0; i<ceiling; i++) 
    { 
     sum += numbermarker[i]; 
    } 
    if(sum == ceiling) 
     not_done = 0; 
} 
for(i=0;i<numthreads;i++) 
{ 
    if(pthread_join(*tid[i], NULL)) 
    { 
     printf("Error Joining with thread \n"); 
    } 
    free(tid[i]); 
} 
free(tid); 




for(i=0;i<ceiling;i++) 
{ 
    if(buffer[i] != 0); 
     printf("%d \n", i); 
} 
free(buffer); 
free(numbermarker); 
buffer=NULL; 
numbermarker=NULL; 


return(0); 

}

+0

請[唐」 t在C](http:// stack)中輸入'malloc()'的返回值overflow.com/a/605858/28169)。謝謝。 – unwind 2013-04-05 07:54:40

回答

5

lock是一個未初始化的指針。你需要分配內存然後在鎖定之前初始化一個互斥鎖。最簡單的解決將是改變

pthread_mutex_t *lock; 

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 

和(因爲lock不再是一個指針)

pthread_mutex_lock(lock); 
.... 
pthread_mutex_unlock(lock); 

pthread_mutex_lock(&lock); 
.... 
pthread_mutex_unlock(&lock); 
+0

並調用pthread_mutex_lock(鎖定) – 2013-04-05 07:53:48

+0

@jimmcnamara你剛纔打我吧,現在編輯爲包括該 – simonc 2013-04-05 07:54:52

+0

我做了一些進一步的研究,發現了不同的修復,保持鎖爲指針: 我需要的鎖分配空間,然後使用pthread初始值設定項方法: 'lock =(pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));' 'pthread_mutex_init(lock,NULL);' – 2013-04-05 08:01:10