使用线程段故障

时间:2018-07-08 14:49:14

标签: c segmentation-fault

此代码运行,但一直出现段故障(核心已转储)。不确定问题出在哪里,我尝试为线程运行一个不同的变量,并且在尝试使用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)

代码如下:

#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;
}

2 个答案:

答案 0 :(得分:0)

通常,mallocfree内部的任何崩溃(在这种情况下,munmap_chunkfree调用)很可能是堆损坏的结果。

在这种情况下,您free(info),但没有mallocinfo指向堆栈变量args[i])。

释放未分配的内存是堆损坏的一种特定实例。其他原因:free进行两次操作,malloc版缓冲区溢出等。

查找此(和类似)错误的一种好方法是在Valgrind或(更好)Address Sanitizer下运行程序。

答案 1 :(得分:0)

谢谢大家的帮助,我将我的代码更改为以下代码,这些代码可以正常工作,并且没有引起任何错误代码。我删除了自由,并添加了一个导致无限循环的中断。

#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;
}