Hun73r3006
Hun73r3006

Reputation: 33

Segment Fault using threads

This code runs but keeps hitting a segment fault (core dumped). Not sure where the issue is is, I have tried to run it a different variables for the threads and it creates a large error while trying to run using a 2.

*** thread 21474836484 sees value 32
*** thread 21474836484 sees value 33
*** thread 38654705672 sees value 25
*** thread 34359738375 sees value 29
*** thread 34359738375 sees value 30
Thread 34359738375 sees final value 31
*** thread 21474836484 sees value 31
*** thread 38654705672 sees value 32
*** thread 21474836484 sees value 33
*** thread 38654705672 sees value 34
*** thread 21474836484 sees value 35
Thread 21474836484 sees final value 36
*** Error in `./treadcreator': munmap_chunk(): invalid pointer: 0x00007ffeab9a33bc ***
*** thread 8589934593 sees value 29
*** thread 8589934593 sees value 30
*** thread 8589934593 sees value 31
Segmentation fault (core dumped)

Here is the code:

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

struct my_thread_info {
    long which;
};

int SharedVariable = 0;

void *SimpleThread(void *data) {
    int num, val;
    struct my_thread_info *info = data;

    for (num = 0; num < 20; num++) {
        if (random() > RAND_MAX / 2)
            usleep(10);
        val = SharedVariable;
        printf("*** thread %ld sees value %d\n", info->which, val);
        SharedVariable = val + 1;
    }

    val = SharedVariable;
    printf("Thread %ld sees final value %d\n", info->which, val);

    free(info);
    return NULL;
}

int main(int argc, char **argv) {
    int j = 0, isDigit = 1;
    while (j < strlen(argv[1])) {
        isDigit = isdigit(argv[1][j]);
        if (isDigit == 0) break;
        j++;
    }
    if (isDigit == 1) {
        int num_threads = atoi(argv[1]); // Convert first argument to integer.

        pthread_t threads[num_threads];
        int args[num_threads];
        int i = 0;

        for (i = 0; i < num_threads; i++) {
            args[i] = i + 1;

            if (pthread_create(&threads[i], NULL, SimpleThread, &args[i]) != 0) {
                printf("error: Cannot create thread # %d\n", i + 1);
                break;
            }
        }
        int a = 0;
        for (a = 0; a < num_threads; i++)
            if (pthread_join(threads[a], NULL) != 0)
                printf("error: Cannot join thread # %d\n", a + 1);
    } else
        printf("Enter a valid number for number of threads to be created\n");

    return 0;
}

Upvotes: 0

Views: 378

Answers (2)

Hun73r3006
Hun73r3006

Reputation: 33

Thank You all for your help, i changed my code to the following below, which worked out and did not cause any error codes. I removed the free and added a break which was causing an infinite loop.

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


int SharedVariable = 0;

void* SimpleThread(void *which) 
{
int num, val = 0;

for(num = 0; num < 20; num++) 
{
if (random() > RAND_MAX / 2)
usleep(10);

val = SharedVariable;

printf("*** thread %d sees value %d\n", which, val);             
SharedVariable = val + 1;

}

val = SharedVariable;

printf("Thread %d sees final value %d\n", which, val);

}

int main(int argc, char** argv)
{
int j = 0, isDigit = 1;
while (j < strlen(argv[1])) 
{
    isDigit = isdigit(argv[1][j]);
    if (isDigit == 0) break;
    j++;
}
if (isDigit == 1)
{
    int num_threads = atoi(argv[1]); // Convert first         
argument to integer.

    pthread_t threads[num_threads];
    int args[num_threads];
    int i = 0;

    for (i = 0; i < num_threads; i++)

    {
        args[i] = i + 1;

        if (pthread_create(&threads[i], NULL, 
  SimpleThread, &args[i]) != 0)

        {
            printf("error: Cannot create thread # 
   %d\n", i + 1);
            break;

        }
    }
    int a = 0;
    for (a = 0; a < num_threads; i++)

        if (pthread_join(threads[a], NULL) != 0)
        {
            printf("error: Cannot join thread # %d\n", 
    a + 1);
            break;
        }


  }
  else
    printf("Enter a valid number for number of threads 
 to be created\n");
 return 0;
}

Upvotes: 0

Employed Russian
Employed Russian

Reputation: 213526

In general, any crash inside malloc or free (in this case, munmap_chunk is called by free) is very likely to be the result of heap corruption.

In this particular case, you free(info), but you didn't malloc it (info points to stack variable args[i]).

Freeing un-allocated memory is one specific instance of heap corruption. Other causes: freeing something twice, overflowing malloced buffer, etc. etc.

A nice way to find this (and similar) bugs is by running the program under Valgrind or (better) Address Sanitizer.

Upvotes: 1

Related Questions