Search code examples
cstructpthreadsargvargc

Attempting to understand multithreading which involves struct. Getting output "Segmentation fault (core dumped)"


I created this program to understand multithreading and have tested this program with single thread and works. Basically you enter 3 digits. First one as an initiale number, Second one is how many squence it will be run and last number is used for the number of threads required. Program will add the first 2 numbers in a struct that has: start, iteration and result. The algorithm will start multiplying the first number by 2 for the number of times you entered in the second number. example: 1 3 2.

I've done the program in normally which works. but once i introduce pthread i'm getting Segmentation core dump error. I've spend hours trying to identify what is causing it, but no luck.

//The program will do: 1 * 2 = 2, 2 * 2 = 4, 4 * 2 = 8 
//The results will be stored in a the struct result which is a pointer.

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

struct Params
{
    int start;
    int iteration;
    int *result;
};

void *double_number(void *vFirststruct)
{
    struct Params *Firststruct = (struct Params *)vFirststruct;
    int iter = 0;
    Firststruct->result = (int *)malloc(sizeof(int) * Firststruct->iteration);
    for (iter = 0; iter < Firststruct->iteration; iter++)
    {
        // printf("%d\n", Firststruct->start);
        Firststruct->start = Firststruct->start * 2;
        Firststruct->result[iter] = Firststruct->start;
    }
}

void double_number_Single_Thread(struct Params *Firststruct)
{
    int iter = 0;
    Firststruct->result = (int *)malloc(sizeof(int) * Firststruct->iteration);
    for (iter = 0; iter < Firststruct->iteration; iter++)
    {
        printf("%d\n", Firststruct->start);
        Firststruct->start = Firststruct->start * 2;
        Firststruct->result[iter] = Firststruct->start;
    }
}

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

    struct Params *Firststruct = (struct Params *)malloc(sizeof(struct Params));
    Firststruct->start = atoi(argv[1]);
    Firststruct->iteration = atoi(argv[2]);
    int threads = atoi(argv[3]);

    //For Single Thread
    // double_number_Single_Thread(Firststruct); // <-- testing on single thread

    // for (int i = 0; i < Firststruct->iteration; i++)
    // {
    //     printf("%d %d\n", i, Firststruct->result[i]);
    // }

    //End for Single Thread

    //Start of Single thread using pthread-Thread
    pthread_t *t = (pthread_t *)malloc(threads * sizeof(pthread_t));

    pthread_create(&t[0], NULL, &double_number, (void *)&Firststruct);
    pthread_join(t[0], NULL);

    //End for Single Thread
    
    //Start of Multi thread

    // for (int i = 0; i < threads; i++)
    // {
    //     pthread_create(&t[i], NULL, &double_number, (void *)&Firststruct);
    // }

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

    free(Firststruct);
    return 0;
}

Solution

  • The main problem you have (ignoring the fact that different thread will modify the same data) is your pthread_create call.

    pthread_create(&t[0], NULL, &double_number, (void *) & Firststruct);
    

    Should be

    pthread_create(&t[0], NULL, &double_number, (void *) Firststruct);
    

    Indeed Firststruct is already a pointer on struct Params, the extra & causes the mess.