c - Segfault resulting from pthread? -


i have been beating head on wall several hours trying find causing segfault.

i have found segfault occurs consistantly on pthread_mutex_lock(lock) line (38). have placed 2 print statements surrounding lock however, 1 of them prints, justification concluding segfault occurs @ instruction.

am using mutex lock correctly, or making mistake arrays (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 1 of primes in buffer.  offers safe way manage next value placed*/ void placevalue(int value) {     buffer[placeposition] = value;     placeposition++; }  /*this method pulls next prime , increments next prime in 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 highest value check \n"); scanf("%d", &ceiling);      /* hold 1's , 0's.         1 = number has been checked or             confirmed not prime         0 = number possible prime      idea behind these values next     prime can identified 0     lowest index*/ numbermarker = (int*)malloc(sizeof(int)*(ceiling));      /*this hold primes 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 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); 

}

lock uninitialised pointer. need allocate memory initialise mutex before locking it. simplest fix change

pthread_mutex_t *lock; 

to

pthread_mutex_t lock = pthread_mutex_initializer; 

and (since lock no longer pointer)

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

to

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

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -