'Segmentation fault (core dumped) in SCHED_OTHER

I am running this program but there is an issue which i cannot find.When i put dimensions 50x50x50 the program works fine but when i put for example 50x50x20 i show the error Segmentation fault (core dumped).I dont get it why its doing that.Anyone knows? Here is the code.I am trying to mul array A and B and i use the part to seperate the rows of array A to work equally to threads.Thanks in advance

    //SCHED_OTHER
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sched.h>
int part,move=-1;
int **A,**B,**C;
int rows;
int cols;
int cols2;

//AxB arrays
//part used for seperate the work equally for threads
void *mul(){
    int s=++move;
    struct timeval start, end;
    //printf("deadline thread started [%d]\n", gettid());
        struct sched_param params;
        //params.sched_priority =sched_get_priority_min(SCHED_RR);
        sched_setscheduler(0,SCHED_OTHER,&params);
        gettimeofday(&start,NULL);
        for(int k=s*part; k<(s+1)*part; k++){
            for(int j=0; j<cols; j++){
                C[k][j]=0;
                for(int f=0; f<cols2; f++){
                    C[k][j]+=A[k][f]*B[f][j];
                    //sched_yield();
                }
            }
        }
        gettimeofday(&end,NULL);
        printf("Time taken for thread_other %d in microsec %ld\n",s+1,((end.tv_sec*1000000 +end.tv_usec)-(start.tv_sec *1000000 + start.tv_usec)));
        //if rows/5!=0 then the last threads doing the rest rows of array A
        if(s==4 && rows/5!=0){
           for(int k=5*part; k<rows; k++){
            for(int j=0; j<cols; j++){
                C[k][j]=0;
                for(int f=0; f<cols2; f++){
                    C[k][j]+=A[k][f]*B[f][j];
                }
            }
        }
        }
        
}

int main(int argc,char *argv[]){
    
    pthread_t t[5];
    srand(time(NULL));
    printf("Give rows of array A\n");
    scanf("%d",&rows);
    printf("Give cols of array A\n");
    scanf("%d",&cols);
    printf("Give cols of array B\n");
    scanf("%d",&cols2); 

    A=malloc(rows*(sizeof(int*)));
    for(int i=0; i<rows; i++){
        A[i]=malloc(cols*(sizeof(int)));
    }
    B=malloc(cols*(sizeof(int*)));
    for(int i=0; i<cols; i++){
        B[i]=malloc(cols2*(sizeof(int)));
    }

    C=malloc(rows*(sizeof(int*)));
    for(int i=0; i<rows; i++){
        C[i]=malloc(cols2*(sizeof(int)));
    }
    for(int i=0; i<rows; i++){
        for(int l=0; l<cols; l++){
            A[i][l]= (rand()%2)+1;
            //A[i][l]=1;
           // printf("%5d",A[i][l]);
        }
        //printf("\n");
    }
    printf("\n");
    for(int i=0; i<cols; i++){
        for(int l=0; l<cols2; l++){
            B[i][l]= (rand()%2)+1;
            //B[i][l]=1;
            //printf("%5d",B[i][l]);
        }
        //printf("\n");
    }
    printf("\n");
    part=rows/5;

    
        int res;
        for(int i=0; i<5; i++){
        res=pthread_create(&t[i],NULL,mul,NULL);
        }
    

    for(int i=0; i<5; i++){
        res=pthread_join(t[i],NULL);
    }

    for(int i=0; i<rows; i++){
        for(int l=0; l<cols2; l++){
            printf("%8d",C[i][l]);

        }
        printf("\n");
    }
    free(A);
    free(B);
    free(C);
    return 0;

}
c


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source