Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gcc 覆盆子PI4分割错误_Gcc_Pthreads_Raspberry Pi4 - Fatal编程技术网

Gcc 覆盆子PI4分割错误

Gcc 覆盆子PI4分割错误,gcc,pthreads,raspberry-pi4,Gcc,Pthreads,Raspberry Pi4,我有一个短程序,在运行多次后(例如:循环中运行10次),导致RPi4出现分段错误。 我使用的是RaspbianGNU/Linux10(buster)和默认的gcc编译器(SudoAPTInstallBuildEssential) 您认为这是gcc编译器的问题吗?也许我缺少一些RPi4的特殊设置。 我用它来构建: gcc threads.c -o threads -l pthread 输出有时(并非总是)如下所示: ... in thread_dummy, loop: 003 Segmentat

我有一个短程序,在运行多次后(例如:循环中运行10次),导致RPi4出现分段错误。 我使用的是RaspbianGNU/Linux10(buster)和默认的gcc编译器(SudoAPTInstallBuildEssential)

您认为这是gcc编译器的问题吗?也许我缺少一些RPi4的特殊设置。 我用它来构建:

gcc threads.c -o threads -l pthread
输出有时(并非总是)如下所示:

...
in thread_dummy, loop: 003
Segmentation fault
代码如下:

#include <stdio.h>  /* for puts() */
#include <unistd.h> /* for sleep() */
#include <stdlib.h> /* for EXIT_SUCCESS */
#include <pthread.h>

#define PTR_SIZE (0xFFFFFF)
#define PTR_CNT (10)

void* thread_dummy(void* param)
{
    void* ptr = malloc(PTR_SIZE);

    //fprintf(stderr, "thread num: %03i, stack: %08X, heap: %08X - %08X\n", (int)param, (unsigned int)&param, (unsigned int)ptr, (unsigned int)((unsigned char*)ptr + PTR_SIZE));
    fprintf(stderr, "in thread_dummy, loop: %03i\n", (int)param);

    sleep(1);

    free(ptr);

    pthread_detach(pthread_self());
    return NULL;
}

int main(void)
{
    void* ptrs[PTR_CNT];
    pthread_t threads[PTR_CNT];
    for(int i=0; i<PTR_CNT; ++i)
    {
        ptrs[i] = malloc(PTR_SIZE);
        //fprintf(stderr, "main   num: %03i, stack: %08X, heap: %08X - %08X\n", i, (unsigned int)&ptrs, (unsigned int)ptrs[i], (unsigned int)((unsigned char*)ptrs[i] + PTR_SIZE));
        fprintf(stderr, "in main, loop: %03i\n", i);
    }

    fprintf(stderr, "-----------------------------------------------------------\n");

    for(int i=0; i<PTR_CNT; ++i)
        pthread_create(&threads[i], 0, thread_dummy, (void*)i);

    for(int i=0; i<PTR_CNT; ++i)
        pthread_join(threads[i], NULL);

    for(int i=0; i<PTR_CNT; ++i)
        free(ptrs[i]);

    return EXIT_SUCCESS;
}

pthread\u create
类似于
malloc
pthread\u detach
pthread\u join
类似于
free
。你基本上是在做一些类似“双重自由”的事情——你拆下一条线,同时连接它。拆下或连接螺纹

您可以从
main
中删除
pthread\u join
。但是您应该在逻辑上从线程内部删除
pthread\u detach(…)
,这实际上是无用的,因为无论如何线程都会在之后立即终止

#include <stdio.h>  /* for puts() */
#include <unistd.h> /* for sleep() */
#include <stdlib.h> /* for EXIT_SUCCESS */
#include <pthread.h>

#define PTR_SIZE (0xFFFFFF)
#define PTR_CNT (10)

void* thread_dummy(void* param)
{
    void* ptr = malloc(PTR_SIZE);

    //fprintf(stderr, "thread num: %03i, stack: %08X, heap: %08X - %08X\n", (int)param, (unsigned int)&param, (unsigned int)ptr, (unsigned int)((unsigned char*)ptr + PTR_SIZE));
    fprintf(stderr, "in thread_dummy, loop: %03i\n", (int)param);

    sleep(1);

    free(ptr);

    pthread_detach(pthread_self());
    return NULL;
}

int main(void)
{
    void* ptrs[PTR_CNT];
    pthread_t threads[PTR_CNT];
    for(int i=0; i<PTR_CNT; ++i)
    {
        ptrs[i] = malloc(PTR_SIZE);
        //fprintf(stderr, "main   num: %03i, stack: %08X, heap: %08X - %08X\n", i, (unsigned int)&ptrs, (unsigned int)ptrs[i], (unsigned int)((unsigned char*)ptrs[i] + PTR_SIZE));
        fprintf(stderr, "in main, loop: %03i\n", i);
    }

    fprintf(stderr, "-----------------------------------------------------------\n");

    for(int i=0; i<PTR_CNT; ++i)
        pthread_create(&threads[i], 0, thread_dummy, (void*)i);

    for(int i=0; i<PTR_CNT; ++i)
        pthread_join(threads[i], NULL);

    for(int i=0; i<PTR_CNT; ++i)
        free(ptrs[i]);

    return EXIT_SUCCESS;
}
gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/arm-linux-gnueabihf/11.1.0/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../configure --enable-languages=c,c++,fortran --with-cpu=cortex-a72 --with-fpu=neon-fp-armv8 --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.1.0 (GCC)