Myn
Myn

Reputation: 691

C: Segmentation Fault on pthread_create

I am getting a Segmentation Fault error when running my program in GCC. This is quite a long program so I am only posting the parts which I believe are relevant; please let me know if more is required.

void *RelaxThread(void *para) {
    printf("Entering RelaxThread...\n");
    struct Parameter *parameters;
    parameters = (struct Parameter *) para;
    /*Some code here*/
    pthread_exit(NULL);
}

int relax(double **matrix, int size, int threads, double precision) {
    keepRunning = 0;
    int rowAllocation = (size-2) / threads;     
    struct Parameter para[threads];
    pthread_t threadcount[threads];
    int current;
    int startRow = 1;
    long t;    
    for(t=0; t<threads; t++){       
        para[t].matrix = matrix;
        para[t].size = size;
        para[t].threads = threads;
        para[t].precision = precision;
        para[t].allocation = rowAllocation;
        para[t].threadId = t;
        para[t].startRow = startRow;
        startRow += rowAllocation;
        printf("DebugMsg1\n");
        current = pthread_create(&threadcount[t], NULL, RelaxThread, (void *) &para[t]);
        printf("DebugMsg2\n");
    }
    void *status;
    int rc;
    for(t=0; t<threads; t++) {
        rc = pthread_join(threadcount[t], &status);
        if (rc) {
            exit(-1);
        }
    }
    if (keepRunning) {
        printf("Iterating...\n");
        relax(matrix, size, threads, precision); 
    }
}

int main(int argc, char *argv[])
{
    int const size = 8;
    int const threads = 2;
    double const precision = 1e-6;
    double **matrix = (double **) malloc(size * sizeof(double));
    populateMatrix(matrix, size);
    relax(matrix, size, threads, precision);
    free(matrix);
    pthread_exit(NULL);
    return 0;
}

The code runs fine when size is set to 8 or below. However anything larger than this (it only supports even numbers for now) gives a segmentation fault on the second iteration of relax(). The debug messages therefore run as:

Debug1
Debug2
Debug1
Debug2
Entering RelaxThread...
Entering RelaxThread...
Iterating...
Debug1
Debug2
Debug1
Segmentation Fault

The fact that this runs where size is 8 or less but crashes above this number has completely confused me, and I have spent a long time trying to work out why this is happening. I completely acknowledge that my code is neither the neatest nor most efficient, but would greatly appreciate some advice as to why this is failing.

Edit 1: attaching GDB backlog as requested.

Debug1
[New Thread 0x40040940 (LWP 23270)]
Debug2
Debug1
Entering RelaxThread...
Entering RelaxThread 0
[New Thread 0x40081940 (LWP 23271)]
Debug2
Entering RelaxThread...
Entering RelaxThread 1
[Thread 0x40040940 (LWP 23270) exited]
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 0
Iterating...
Debug1
[Thread 0x40081940 (LWP 23271) exited]
[New Thread 0x40081940 (LWP 23272)]
Debug2
Debug1

Edit2: updated the backtrace log following changes.

Edit3: the below is the full RelaxThread

    void *RelaxThread(void *para) {
        struct Parameter *parameters;
        parameters = (struct Parameter *) para;
        double **matrix = parameters->matrix;
        int size = parameters->size;
        double precision = parameters->precision;
        int threads = parameters->threads;
        int threadId = parameters->threadId;
        int startRow = parameters->startRow;
        int allocation = parameters->allocation;
        int finishRow = (startRow + allocation);
        int i;
        int j;
        double oldValue;
        double newValue;
        double difference;
        for(i=startRow;i<finishRow;i++) {
            for (j=1;j<size-1;j++) {
                pthread_mutex_lock (&mutexmatrix); //Tested with and without the mutex, which is globally defined and initialised elsewhere
                oldValue = matrix[i][j];
                newValue = ((matrix[i+1][j] + matrix[i-1][j] + matrix[i][j+1] + matrix[i][j-1]) / 4);
                matrix[i][j] = newValue;
                pthread_mutex_unlock (&mutexmatrix);
                difference = oldValue - newValue;
                if(difference < 0) {
                    difference = -difference; 
                }
                //printf("Precision on thread%d: %f , aiming for %f\n",threadId, difference,precision);
                if (difference > precision) {
                    keepRunning = 1;
                }
            }
        }
}

Upvotes: 3

Views: 1658

Answers (1)

felipemaia
felipemaia

Reputation: 3571

The proper initialization, assuming a squared matrix would be:

double **matrix = malloc(size * sizeof(double *));
for(int i=0; i<size; i++)
{
    matrix[i] = malloc(size * sizeof(double));
    for(int j=0; j<size; j++)
    {
        matrix[i][j] = 0; // removing the junk contents
    }
}

EDIT: I also realized you never actually initialize void *status. Since you're not using it, change it to rc = pthread_join(threadcount[t], NULL);, that's surely another problem.

EDIT3: Another problem I found is int startRow = 1;, I do not see how you're accessing the matrix, but it's index should start as 0.

EDIT4: Also, when you free a two dimensional array you need to do it this way:

for(int i=0; i<size; i++)
{
    free(matrix[i]);    //Free each row pointer
}
free(matrix);

Upvotes: 3

Related Questions