C 使用线程的段故障

C 使用线程的段故障,c,segmentation-fault,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

此代码运行,但不断遇到段错误(内核转储)。不确定问题在哪里,我尝试为线程运行一个不同的变量,它在尝试使用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;
}
#包括
#包括
#包括
#包括
结构我的线程信息{
长哪;
};
int SharedVariable=0;
void*SimpleThread(void*data){
int num,val;
struct my_thread_info*info=数据;
对于(num=0;num<20;num++){
如果(随机()>RAND_MAX/2)
usleep(10);
val=共享变量;
printf(“***线程%ld看到值%d\n”,信息->哪个,val);
SharedVariable=val+1;
}
val=共享变量;
printf(“线程%ld看到最终值%d\n”,信息->哪个,val);
免费(信息);
返回NULL;
}
int main(int argc,字符**argv){
int j=0,isDigit=1;
而(j
一般来说,
malloc
free
内部的任何崩溃(在这种情况下,
munmap\u chunk
free
调用)都很可能是堆损坏的结果

在这种特殊情况下,您可以
free(info)
,但没有
malloc
它(
info
指向堆栈变量
args[i]

释放未分配的内存是堆损坏的一个特定实例。其他原因:
free
ing某物两次,溢出
malloc
ed缓冲区等


找到这个(和类似的)bug的一个好方法是在或(更好)下运行该程序。

谢谢大家的帮助,我将代码更改为以下内容,这很有效,并且没有导致任何错误代码。我删除了自由,并添加了一个中断,这导致了一个无限循环

#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;
}
#包括
#包括
#包括
#包括
int SharedVariable=0;
void*SimpleThread(void*which)
{
int num,val=0;
对于(num=0;num<20;num++)
{
如果(随机()>RAND_MAX/2)
usleep(10);
val=共享变量;
printf(“***线程%d看到值%d\n”,其中,val);
SharedVariable=val+1;
}
val=共享变量;
printf(“线程%d看到最终值%d\n”,它是val);
}
int main(int argc,字符**argv)
{
int j=0,isDigit=1;
而(j
记住只
免费
你的
malloc
。你没有
malloc
任何东西,因此你也不应该
free
任何东西。哦,你使用
SharedVariable
valgrind
gdb
时会遇到一些令人讨厌的数据竞争。我试图删除free命令,但遇到了一个无限循环:(.在使用gdb并运行程序后,我收到以下消息:(gdb)运行启动程序:/root/threadcreator[使用libthread\u db启用线程调试]使用主机libthread\u db library”/lib64/libthread\u db.so.1”.Program从/lib64/libc.so.6接收到信号SIGSEGV,分段错误。0x00007ffff7960ff1 in_u_ustrlen_sse2_pminub()来自/lib64/libc.so.6缺少单独的调试信息,请使用:debuginfo安装glibc-2.17-222.el7.x86_64(gdb)以调试信息开始构建(在构建时添加
-g
标志),然后GDB将能够准确地告诉您崩溃发生在代码的哪一行。