Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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
C pthread_连接分段错误_C_Pthreads_Posix - Fatal编程技术网

C pthread_连接分段错误

C pthread_连接分段错误,c,pthreads,posix,C,Pthreads,Posix,我正在尝试编写一个C程序,该程序使用分配的线程计算目录树的大小 当只有一个子目录时,我的代码可以正常工作,但是每当我有2个或更多子目录时,就会出现分段错误。我读了很多关于这方面的文章,无法找到代码失败的原因 在我的全球范围内: pthread_mutex_t mutex; int total_size = 0; // Global, to accumulate the size main(): int main(int argc, char *argv[]) { pthread_t t

我正在尝试编写一个C程序,该程序使用分配的线程计算目录树的大小

当只有一个子目录时,我的代码可以正常工作,但是每当我有2个或更多子目录时,就会出现分段错误。我读了很多关于这方面的文章,无法找到代码失败的原因

在我的全球范围内:

pthread_mutex_t mutex;
int total_size = 0; // Global, to accumulate the size
main():

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

    ...

    if (pthread_mutex_init(&mutex, NULL) < 0) 
    {
        perror("pthread_mutex_init");
        exit(1);
    }

    pthread_create(&thread, NULL, dirsize, (void*)dirpath);
    pthread_join(thread, NULL);

    printf("\nTotal size: %d\n\n", total_size);

...
}
intmain(intargc,char*argv[])
{
pthread\u t线程;
...
if(pthread\u mutex\u init(&mutex,NULL)<0)
{
perror(“pthread_mutex_init”);
出口(1);
}
pthread_创建(&thread,NULL,dirsize,(void*)dirpath);
pthread_join(线程,NULL);
printf(“\n总大小:%d\n\n”,总大小);
...
}
Mydirsize()函数:

void* dirsize(void* dir)
{
    ...

    pthread_t tid[100];
    int threads_created = 0;

    dp=opendir(dir);
    chdir(dir);

    // Loop over files in directory
    while ((entry = readdir(dp)) != NULL)
    {
        ...

        // Check if directory
        if (S_ISDIR(statbuf.st_mode))
        {
            // Create new thread & call itself recursively
            pthread_create(&tid[threads_created], NULL, dirsize, (void*)entry->d_name);
            threads_created++;
        }
        else
        {
            // Add filesize
            pthread_mutex_lock(&mutex);
            total_size += statbuf.st_size;
            pthread_mutex_unlock(&mutex);
        }
    }

    for (i = 0; i < threads_created; i++)
    {
        pthread_join(tid[i], NULL);
    }
}
void*dirsize(void*dir)
{
...
pthread_t tid[100];
int threads_created=0;
dp=opendir(dir);
chdir(dir);
//循环目录中的文件
while((entry=readdir(dp))!=NULL)
{
...
//检查目录是否正确
if(S_ISDIR(statbuf.st_模式))
{
//创建新线程&递归地调用自身
pthread_create(&tid[threads_created],NULL,dirsize,(void*)条目->d_名称);
线程创建++;
}
其他的
{
//添加文件大小
pthread_mutex_lock(&mutex);
总尺寸+=statbuf.st尺寸;
pthread_mutex_unlock(&mutex);
}
}
对于(i=0;i
我做错了什么?如果你能给我指出正确的方向,我将不胜感激

以下是我通过gdb得到的信息:


提前谢谢你

线程数的值是多少

    // Check if directory
    if (S_ISDIR(statbuf.st_mode))
    {
        // Create new thread & call itself recursively
        pthread_create(&tid[threads_created], NULL, dirsize, (void*)entry->d_name);
        threads_created++;
    }
在这里,您应该检查创建的线程数是否等于NUM\u线程数,如果是,则增加tid数组的大小(顺便说一句,我会在函数开始时malloc,在函数结束时free


此外,在将目录名(malloc+strcpy)作为参数传递给线程之前,您应该分配一个目录名副本,并在函数末尾释放该副本,而不是
entry->d_name

NUM_THREADS的值是多少

    // Check if directory
    if (S_ISDIR(statbuf.st_mode))
    {
        // Create new thread & call itself recursively
        pthread_create(&tid[threads_created], NULL, dirsize, (void*)entry->d_name);
        threads_created++;
    }
在这里,您应该检查创建的线程数是否等于NUM\u线程数,如果是,则增加tid数组的大小(顺便说一句,我会在函数开始时malloc,在函数结束时free


此外,在将目录名作为参数传递给线程之前,您应该分配目录名的副本(malloc+strcpy),并在函数末尾释放此类副本,而不是
entry->d_name

我认为您应该提供完整的dirsize源代码。我更新了代码,谢谢。什么是
entry->d_name
?它是否与
dirpath
的类型相同?我建议在SEGFULT之后检查stacktrace。您可以在gdb中加载coredump并键入bt。如果您没有coredump,请在gdb中加载可执行文件,然后在获得segfault时运行和bt。然后您可以看到SEGFULT发生的确切位置。始终测试系统调用错误。我认为您应该提供完整的dirsize源代码。我更新了代码,谢谢。什么是
entry->d_name
?它是否与
dirpath
的类型相同?我建议在SEGFULT之后检查stacktrace。您可以在gdb中加载coredump并键入bt。如果您没有coredump,请在gdb中加载可执行文件,然后在获得segfault时运行和bt。然后您可以看到segfault发生的确切位置。始终测试系统调用错误。NUM_THREADS的值足够大(1000),并且我使用的目录结构非常小。谢谢,我尝试了,但它仍然会导致segfault。我将尝试使用gdb来回溯它。如果我发现问题的原因,我会将其发布到这里。NUM_THREADS的值足够大(1000),并且我使用的目录结构非常小。谢谢,我尝试过,但它仍然会导致segfault。我将尝试使用gdb来回溯它。如果我发现问题的原因,我会把它贴在这里。