segmentation fault is gonna kill me since 3 days

Viewed 73

I want to multiply two randomly produced matrices by multi threading. When i compile and enter threading number a i get segmentation fault then i debug it on GDB it shows an error like this:

    [New Thread 0x7ffff7dca700 (LWP 6328)]
    [New Thread 0x7ffff75c9700 (LWP 6329)]
    [New Thread 0x7ffff6dc8700 (LWP 6330)]
    [New Thread 0x7ffff65c7700 (LWP 6331)]
    [New Thread 0x7ffff5dc6700 (LWP 6332)]
    [New Thread 0x7ffff55c5700 (LWP 6333)]
    [New Thread 0x7ffff4dc4700 (LWP 6334)]
    [New Thread 0x7ffff45c3700 (LWP 6335)]
    [New Thread 0x7ffff3dc2700 (LWP 6336)]
    [New Thread 0x7ffff35c1700 (LWP 6337)]
    [New Thread 0x7ffff2dc0700 (LWP 6338)]
    [New Thread 0x7ffff25bf700 (LWP 6339)]
    [New Thread 0x7ffff1dbe700 (LWP 6340)]
    [New Thread 0x7ffff15bd700 (LWP 6341)]
    [New Thread 0x7ffff0dbc700 (LWP 6342)]
    [New Thread 0x7ffff05bb700 (LWP 6343)]
    [New Thread 0x7fffefdba700 (LWP 6344)]
    [New Thread 0x7fffef5b9700 (LWP 6345)]
    [New Thread 0x7fffeedb8700 (LWP 6346)]
    [New Thread 0x7fffee5b7700 (LWP 6347)]
    [New Thread 0x7fffeddb6700 (LWP 6348)]
    [New Thread 0x7fffed5b5700 (LWP 6349)]
    [New Thread 0x7fffecdb4700 (LWP 6350)]
    [New Thread 0x7fffec5b3700 (LWP 6351)]
    [New Thread 0x7fffebdb2700 (LWP 6352)]
    [Thread 0x7fffee5b7700 (LWP 6347) exited]
    [Thread 0x7fffecdb4700 (LWP 6350) exited]
    [Thread 0x7fffed5b5700 (LWP 6349) exited]
    [Thread 0x7fffec5b3700 (LWP 6351) exited]
    [Thread 0x7fffeddb6700 (LWP 6348) exited]
    [Thread 0x7fffeedb8700 (LWP 6346) exited]
    [Thread 0x7fffef5b9700 (LWP 6345) exited]
    [Thread 0x7fffefdba700 (LWP 6344) exited]
    [Thread 0x7ffff05bb700 (LWP 6343) exited]
    [Thread 0x7fffebdb2700 (LWP 6352) exited]
    [Thread 0x7ffff0dbc700 (LWP 6342) exited]
    [Thread 0x7ffff15bd700 (LWP 6341) exited]
    [Thread 0x7ffff1dbe700 (LWP 6340) exited]
    [Thread 0x7ffff25bf700 (LWP 6339) exited]
    [Thread 0x7ffff2dc0700 (LWP 6338) exited]
    [Thread 0x7ffff35c1700 (LWP 6337) exited]
    [Thread 0x7ffff3dc2700 (LWP 6336) exited]
    [Thread 0x7ffff45c3700 (LWP 6335) exited]
    [Thread 0x7ffff4dc4700 (LWP 6334) exited]
    [Thread 0x7ffff55c5700 (LWP 6333) exited]
    [Thread 0x7ffff5dc6700 (LWP 6332) exited]
    [Thread 0x7ffff65c7700 (LWP 6331) exited]
    [Thread 0x7ffff6dc8700 (LWP 6330) exited]
    [Thread 0x7ffff75c9700 (LWP 6329) exited]
    [Thread 0x7ffff7dca700 (LWP 6328) exited]
    
    Thread 1 "a.out" received signal SIGSEGV, Segmentation fault.
    __GI___libc_free (mem=0xf) at malloc.c:3102
    3102    malloc.c: No such file or directory.

I researched about segmentation fault and i found that it occurs when there is allocation problem. So what do you think about this? Should i use malloc for thread number? CODE IS HERE:


    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <time.h>
    
    #define MAT_SIZE 10
    #define MAX_THREADS 100
    
    
    int i,j,k,thread_number;           //Parameters For Rows And Columns
    int matrix1[MAT_SIZE][MAT_SIZE]; //First Matrix
    int matrix2[MAT_SIZE][MAT_SIZE]; //Second Matrix
    int result [MAT_SIZE][MAT_SIZE]; //Multiplied Matrix
    
    //Type Defining For Passing Function Argumnents
    typedef struct parameters {
        int x,y;
    }args;
    
    int random_num() {
        return rand() % 10;
    }
    
    //Function For Calculate Each Element in Result Matrix Used By Threads - - -//
    void* mult(void* arg){
        
        args* p = arg;
        
        //Calculating Each Element in Result Matrix Using Passed Arguments
        for(int a=0;a<j;a++){
            result[p->x][p->y] += matrix1[p->x][a]*matrix2[a][p->y];
        }
        sleep(3);
        
        //End Of Thread
        pthread_exit(0);
    }
    
    
    int main(){
        
        //Initializing All Defined Matrices By Zero - - - - - - - - - - - - - - -//
        for(int x=0;x<10;x++){
            for(int y=0;y<10;y++){
                matrix1[x][y] = 0;
                matrix2[x][y] = 0;
                result[x][y] = 0;
            }
        }
        
        
        //Getting Matrix1 And Matrix2 Info from User - - - - - - - - - - - - - - -//
        
        printf(" --- Defining Matrix 1 ---\n\n");
        
        // Getting Row And Column(Same As Row In Matrix2) Number For Matrix1
        printf("Enter number of rows for matrix 1: ");
        scanf("%d",&i);
        printf("Enter number of columns for matrix 1: ");
        scanf("%d",&j);
        
        printf("\n --- Initializing Matrix 1 ---\n\n");
        for(int x=0;x<i;x++){
            for(int y=0;y<j;y++){
                matrix1[x][y]=random_num();
            }
        }
        
        printf("\n --- Defining Matrix 2 ---\n\n");
    
        // Getting Column Number For Matrix2
        printf("Number of rows for matrix 2 :");
        scanf("%d",&j);
        printf("Enter number of columns for matrix 2: ");
        scanf("%d",&k);
        
        printf("\n --- Initializing Matrix 2 ---\n\n");
        for(int x=0;x<j;x++){
            for(int y=0;y<k;y++){
                matrix2[x][y]=random_num();
            }
        }
        
        
        //Printing Matrices - - - - - - - - - - - - - - - - - - - - - - - - - - -//
        
        printf("\n --- Matrix 1 ---\n\n");
        for(int x=0;x<i;x++){
            for(int y=0;y<j;y++){
                printf("%5d",matrix1[x][y]);
            }
            printf("\n\n");
        }
        
```
1 Answers

Problem 1:

printf("Enter number of rows for matrix 1: ");
scanf("%d",&i);
printf("Enter number of columns for matrix 1: ");
scanf("%d",&j);

printf("Number of rows for matrix 2 :");
scanf("%d",&j); // warning: j is already initialized
printf("Enter number of columns for matrix 2: ");
scanf("%d",&k);

Then you are printing both matrices (using the vars i, j, j, k) but if the 'j' variable is different than the number of cols of matrix 1 -> segfault. The 'j' var is used in the 'mult' function too.

Problem 2:

printf("Enter THREAD NUMBER: ");
scanf("%d",&thread_number);

pthread_t thread[thread_number];
args p[i*k];

//loop; i*k steps; how likely to be the same as thread_number?
for(int x=0;x<i;x++) {
    for(int y=0;y<k;y++) {
        // if thread_number > i*k -> segfault
        p[thread_number].x=x; 
        //...
        pthread_create(&thread[thread_number], ...
        // thread_number specifies the buffer size
        // increasing it means -> segfault
        thread_number++;
    }
}

Problem 3:

The hope that the input for the rows and cols are less than 10.

Solution:

int n=0;
int num_threads = i * k;

pthread_t thread[num_threads];
args p[num_threads];

for (...) {
    for (...) {
        p[n].x=x;
        //...
        pthread_create(&thread[n], ...
        //...
        ++n;
    }
}

Since you want the number of columns of the matrix 1 be equal to the number of rows of the matrix 2, read the value only once or use another variable identifier.

Related