Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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] 马洛克问题_C_Multithreading_Segmentation Fault_Malloc - Fatal编程技术网

[C] 马洛克问题

[C] 马洛克问题,c,multithreading,segmentation-fault,malloc,C,Multithreading,Segmentation Fault,Malloc,我正试图编写一个程序,使用一些基本的线程来完成作业。下面是我认为导致问题的相关片段 程序在25个或更少线程时运行正常,但在使用26个或更多线程时通常会导致segfault。这让我觉得我的malloc声明是错误的。有人能把我推向正确的方向吗?如果你需要更多的代码发布,我很乐意提供 而且,这些问题只有在我在学校的学生机器上运行程序时才会出现。它在我的本地机器上运行得很好。有人知道为什么吗 谢谢你的时间 ... struct thread_args { struct bitmap *bm;

我正试图编写一个程序,使用一些基本的线程来完成作业。下面是我认为导致问题的相关片段

程序在25个或更少线程时运行正常,但在使用26个或更多线程时通常会导致segfault。这让我觉得我的malloc声明是错误的。有人能把我推向正确的方向吗?如果你需要更多的代码发布,我很乐意提供

而且,这些问题只有在我在学校的学生机器上运行程序时才会出现。它在我的本地机器上运行得很好。有人知道为什么吗

谢谢你的时间

...

struct thread_args {
    struct bitmap *bm;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    int max;
    int start;
    int end;
};

...

int num_threads; //Given by user input
struct bitmap *bm = bitmap_create(500, 500); //All threads share the same bitmap
int i;
pthread_t *thread_id = malloc(num_threads * sizeof(*thread_id));
struct thread_args *args = malloc(num_threads * sizeof(*args));
for (i = 0; i < num_threads; i++) { 
    args[i].bm = bm;
    args[i].xmin = xcenter-scale;
    args[i].xmax = xcenter+scale;
    args[i].ymin = ycenter-scale;
    args[i].ymax = ycenter+scale;
    args[i].max = max;
    args[i].start = bitmap_height(bm) * i / num_threads;
    args[i].end = bitmap_height(bm) * (i + 1) / num_threads;

    pthread_create(&thread_id[i], NULL, compute_image, &args[i]);
}

for (i = 0; i < num_threads; i++) {
    pthread_join(thread_id[i], NULL);
}

...

void* compute_image(void *arg)
{
    struct thread_args* args = (struct thread_args*) arg;
    int i,j;
    int width = bitmap_width(args->bm);
    int height = bitmap_height(args->bm);

    // For every pixel in the image...

    for(j=args->start;j<args->end;j++) {
        for(i=0;i<width;i++) {
            // Determine the point in x,y space for that pixel.
            double x = args->xmin + i*(args->xmax-args->xmin)/width;
            double y = args->ymin + j*(args->ymax-args->ymin)/height;

            // Compute the iterations at that point.
            int iters = iterations_at_point(x,y,args->max);
            // Set the pixel in the bitmap.
            bitmap_set(args->bm,i,j,iters);
        }
    }
    return 0;
}

...

valgrind log

==24919== 
==24919== HEAP SUMMARY:
==24919==     in use at exit: 1,000,000 bytes in 1 blocks
==24919==   total heap usage: 56 allocs, 55 frees, 1,018,884 bytes allocated
==24919== 
==24919== Searching for pointers to 1 not-freed blocks
==24919== Checked 87,112 bytes
==24919== 
==24919== 1,000,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==24919==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==24919==    by 0x401256: bitmap_create (bitmap.c:21)
==24919==    by 0x400CEC: main (mandel.c:103)
==24919== 
==24919== LEAK SUMMARY:
==24919==    definitely lost: 1,000,000 bytes in 1 blocks
==24919==    indirectly lost: 0 bytes in 0 blocks
==24919==      possibly lost: 0 bytes in 0 blocks
==24919==    still reachable: 0 bytes in 0 blocks
==24919==         suppressed: 0 bytes in 0 blocks
==24919== 
==24919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
--24919-- 
--24919-- used_suppression:      4 U1004-ARM-_dl_relocate_object
--24919-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==24919== 
==24919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
。。。
结构线程参数{
结构位图*bm;
双xmin;
双xmax;
双ymin;
双ymax;
int max;
int启动;
内端;
};
...
int num_线程//由用户输入
结构位图*bm=bitmap\u创建(500500)//所有线程共享同一位图
int i;
pthread_t*thread_id=malloc(num_threads*sizeof(*thread_id));
struct thread_args*args=malloc(num_threads*sizeof(*args));
对于(i=0;ibm);
整数高度=位图高度(args->bm);
//对于图像中的每个像素。。。
对于(j=args->start;jend;j++){
对于(i=0;ixmin+i*(args->xmax args->xmin)/width;
双y=args->ymin+j*(args->ymax args->ymin)/高度;
//计算该点的迭代次数。
int iters=在某个点的迭代次数(x,y,args->max);
//设置位图中的像素。
位图设置(args->bm,i,j,iters);
}
}
返回0;
}
...
伐木
==24919== 
==24919==堆摘要:
==24919==在出口处使用:1个块中有1000000字节
==24919==总堆使用率:56个alloc,55个free,分配了1018884字节
==24919== 
==24919==搜索指向1个未释放块的指针
==24919==已检查87112字节
==24919== 
==24919==1块中的1000000字节肯定会在丢失记录1(共1个)中丢失
==24919==at 0x4A069EE:malloc(vg_替换_malloc.c:270)
==24919==by 0x401256:bitmap\u创建(bitmap.c:21)
==24919==0x400CEC:main(mandel.c:103)
==24919== 
==24919==泄漏汇总:
==24919==肯定丢失:1个块中有1000000字节
==24919==间接丢失:0个块中有0个字节
==24919==可能丢失:0个块中有0个字节
==24919==仍然可访问:0个块中有0个字节
==24919==抑制:0个块中有0个字节
==24919== 
==24919==错误摘要:1个上下文中的1个错误(已抑制:6个上下文中的6个)
--24919-- 
--24919--已使用\u抑制:4 U1004-ARM-\u dl\u重新定位\u对象
--24919——用于抑制:2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==24919== 
==24919==错误摘要:1个上下文中的1个错误(已抑制:6个上下文中的6个)

编辑:添加了compute_图像代码和valgrind日志,尽管日志缺少今天早些时候显示的错误消息。我知道丢失了1000000字节。

请确保
pthread_create
没有返回错误。计算机对可以生成的线程数量有全局限制,并且可能是错误的波动接近这个极限。如果你不能生成一个线程,你将有一个垃圾
pthread\u t
,这可能会导致
pthread\u join
爆炸。

有几个调试工具可以告诉你具体的失败位置。只需谷歌“c调试工具”.调试是毕业后您需要的一项技能。我们是否假设您的所有线程都应该共享同一位图?因为它们共享。如果您认为实际的线程进程与此相关,那么您是对的。stdlib调用可能会失败。您应该检查malloc()的返回值在使用它们之前调用NULL。@redFIVE我尝试使用GDB和valgrind,但我认为他们告诉我malloc没有分配适当的空间。@WhozCraig啊,是的,所有线程共享相同的位图。它们处理单独的数据集和图像部分,因此没有竞争条件。我替换了
pthread\u create(&thread\u id[i],NULL,compute\u image,&args[i]);
带有
while(pthread\u create(&thread\u id[i],NULL,compute\u image,&args[i])!=0)
而且它现在似乎正在工作。这就是你的意思吗?当然,我计划在某个时候使用errno和strerror。我只是建议你检查错误。我会打印一条错误消息并退出,而不是旋转直到成功。我猜每个人都在处理同一个作业,并且创建了很多错误线程。通过旋转,你的程序将等待其他人的程序完成并释放资源。但是,如果你运气不好,你的程序可能会无限旋转。