简体   繁体   中英

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!!!!!!

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

The answer to the source of your core dump problem has already been addressed, but to address the other things you asked, or stated:

1st:
Regarding your statement: i have them as pointers because i don not know the size,the user gives it as argument .

Often times, in C, you can avoid using calloc / malloc in your code by using VLAs instead. Available in C99 and beyond. (see links)
VLA a
VLA b

2nd:
Regarding your statement: I know it gets a little bit messy ...
Its really not that messy, but you could consider cleaning up the memory allocation/freeing steps by moving most of the work into a function:

long int **A,**B,**C;
int N;
... 
//in main
N = atoi(argv[2]); 
A = Create2D(N, N);
B = Create2D(N, N);
B = Create2D(N, N);
...
free2D(A, N);
free2D(B, N);
free2D(C, N);


long ** Create2D(int c, int r)
{   
    long **arr;
    int    y = 0;

    arr   = calloc(c, sizeof(long *));
    for(y=0;y<c;y++)
    {
        arr[y] = calloc((2*y)+1, sizeof(long)); 
    }
    return arr;
}

void free2D(long **arr, int c)
{
    int i;
    if(!arr) return;
    for(i=0;i<c;i++)
    {
        if(arr[i]) 
        {
            free(arr[i]);
            arr[i] = NULL;
        }
    }
    free(arr);
    arr = NULL;
}

Side note:
There is nothing absolutely wrong with your memory statements as they are:

A = (long int**) malloc(N*sizeof(long int*));  

However, although C++ requires it, there is no reason to cast the return of malloc , calloc or realloc when using C. ( See discussion here ) The following is sufficient (in C):

A = malloc(N*sizeof(long int*));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM