cubuntupthreadsdouble-pointerpthread-barriers

Segmentation fault (core dumped) in C - While Using PTHREADS


Hello guys, I have a problem in my code and i do not know how to fix it(A Segmentation fault(core dumped))!

So my teacher wants me to write a program that creates N treads and makes them do some calculations.I have 3 global 2d arrays A,B,C (i have them as pointers because i don not know the size,the user gives it as argument).I try to allocate memory to them in the main function.

So the problem is i get a Segmentation fault when i try to create the treads in "pthread_create(&tid[id],NULL,add,(void *)(long) i);" :(. I can't figure out why this is happening.I tried using the gdb command but the result was that the problem is in pthread_create.

However when i put in comment the arrays(A,B,C) and the malloc they are using is runs(but the final result is 0).

I am using a virtual box(with Ubuntu inside if that helps :D).

The following code is what i wrote so far:

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

long int p,N,Total_Sum;
long int **A,**B,**C;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_barrier_t bar;

void * add(void *arg){

    long int i,j,Local_Sum=0;
    long int lines,start,end,id;

    id = (long int)arg;
    lines = N/p;
    start = id*lines;
    end = start+lines;

    for(i=start;i<end;i++){
        for(j=0;j<N;j++){
            A[i][j] = 1;
            B[i][j] = 1;
        }
    }
    for(i=start;i<end;i++){
        for(j=0;j<N;j++){
            C[i][j] = A[i][j] * B[i][j];
            Local_Sum += C[i][j];
            printf("C[%ld][%ld] = %ld\n",i,j,C[i][j]);
        }
    }
    pthread_mutex_lock(&mutex);
    Total_Sum += Local_Sum;
    pthread_mutex_unlock(&mutex);
    pthread_barrier_wait(&bar);
    pthread_exit(0);
}

int main(int argc, char *argv[]){

    long int i,j,id;
    pthread_t *tid;
    if(argc!=3){
        printf("Provide Number Of Threads And Size\n");
        exit(1);
    }
    p = atoi(argv[1]);
    tid = (pthread_t *) malloc(p*sizeof(pthread_t));
    if(tid == NULL){
        printf("Could Not Allocate Memory\n");
        exit(1);
    }

    pthread_barrier_init(&bar,NULL,p);

    N = atoi(argv[2]);
    A = (long int**) malloc(N*sizeof(long int*));
    B = (long int**) malloc(N*sizeof(long int*));
    C = (long int**) malloc(N*sizeof(long int*));

    for(i=0;i<N;i++){
        A[i] = (long int*) malloc(N*sizeof(long int));
        B[i] = (long int*) malloc(N*sizeof(long int));
        C[i] = (long int*) malloc(N*sizeof(long int));
    }

    if((A==NULL) || (B == NULL) || (C == NULL)){
        printf("Count Not Allocate Memory\n");
        exit(1);
    }

    for(i=0;i<p;i++){
        pthread_create(&tid[id],NULL,add,(void *)(long) i);
    }
    for(i=0;i<p;i++){
        pthread_join(tid[id],NULL);
    }

    for(i=0;i<N;i++){
        free(A[i]);
        free(B[i]);
        free(C[i]);
    }
    free(A);
    free(B);
    free(C);

    printf("Final Result Is Equal To: %ld\n",Total_Sum);

    return 0;
}

******I know it gets a little bit messy because of the mutex and the the barriers but ask me for further specifications :D.******

Thanks!!!!!!


Solution

  • I think the only problem are the indexes in the following lines:

    for(i=0;i<p;i++){
        pthread_create(&tid[id],NULL,add,(void *)(long) i);
    }
    for(i=0;i<p;i++){
        pthread_join(tid[id],NULL);
    }
    

    id has only been declared, but never initialized! Maybe it's just a typo and you wanted to use i as index for the tid

    The solutions should be:

    for(i=0;i<p;i++){
        pthread_create(&tid[i],NULL,add,(void *)(long) i);
    }
    for(i=0;i<p;i++){
        pthread_join(tid[i],NULL);
    }