memcpy() 在迭代次数过多后会产生分段错误
我正在尝试用 c 创建一个多线程库。这是整个项目的链接(因为在此处粘贴代码会导致文本过多)。
在文件 tests/MultithreadingTests.c
中,我正在测试 lib/systems/multithreading/src/ThreadPool.c
的功能。函数add_work
将任何例程函数添加到工作队列中,该函数利用lib/sds/lists/src/Queue.c
和lib/sds/lists的功能/src/LinkedList.c
。在 MultithreadingTests.c
中,NUM_TESTS
定义了我添加到工作队列中由 NUM_THREADS
执行的作业数量
我遇到了一个奇怪的问题与代码。如果 NUM_TESTS
任何数字小于 349,261
,则代码工作得很好,但任何高于或等于 349,261
的数字都会导致分段错误。我试图检查分段错误到底发生在哪里,发现它发生在 lib/sds/lists/src/Node.c 的第 29 行的 memcpy(node->; data, data, size);
错误的代码流是
tests/MultiThreadingTests.c
第 95 行,位于pool->add_work(pool, new_thread_job(routine, &arguments[i]));
lib/systems/multithreading/src/ThreadPool.c
第 150 行thread_pool->work.push(& thread_pool->work, &job, sizeof(job));
lib/sds/lists/src/Queue.c
行54return q->list.insert(&q->list, q->list.length, data, size);
lib/sds/lists/src/LinkedLists .c
第107行Node *node_to_insert = new_node(data, size);
lib/sds/lists/src/Node.c
line 29memcpy(node->data, data, size);
我不确定为什么会发生这个问题当工作岗位数量高于或等于 349,261 时,但当其较小时则不然。
I am trying to create a multithreading library in c. Here is the link to whole project (because pasting the code here would be too much text).
In the file tests/MultithreadingTests.c
I am testing to the functionality of lib/systems/multithreading/src/ThreadPool.c
. The function add_work
adds any routine function the the work queue which utilises the functionality of lib/sds/lists/src/Queue.c
and lib/sds/lists/src/LinkedList.c
. In MultithreadingTests.c
, NUM_TESTS
defines the number of jobs I am adding to the work queue to be performed by NUM_THREADS
I am facing a weird issue with the code. If NUM_TESTS
any number is less than 349,261
, the code works perfectly fine but any number higher than or equal to 349,261
results in segmentation fault. I tried to check where exactly the segmentation fault is happening and found that it happens in the lib/sds/lists/src/Node.c
at line number 29 at memcpy(node->data, data, size);
The flow of code for the error is
tests/MultiThreadingTests.c
line 95 atpool->add_work(pool, new_thread_job(routine, &arguments[i]));
lib/systems/multithreading/src/ThreadPool.c
line 150thread_pool->work.push(&thread_pool->work, &job, sizeof(job));
lib/sds/lists/src/Queue.c
line 54return q->list.insert(&q->list, q->list.length, data, size);
lib/sds/lists/src/LinkedLists.c
line 107Node *node_to_insert = new_node(data, size);
lib/sds/lists/src/Node.c
line 29memcpy(node->data, data, size);
I am not sure why this issue is happening only when the number of jobs is higher than or equal to 349,261 but not when its smaller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在函数
new_thread_pool()
中,既不thread_pool.pool = malloc(sizeof(pthread_t) * num_threads);
中的分配失败,也不中的线程创建失败>pthread_create(&thread_pool.pool[i], NULL, generic_thread_function, &thread_pool);
尝试在任意线程上创建 349261 个或更多线程系统看起来更像是压力测试,而不是现实生活中的目的。测试错误并以可用的方式报告它们。
new_node
也不检查分配失败。除非您为此检测代码,否则应该使用malloc()
调用的包装器来检测分配失败并使用错误消息中止程序。代码中的问题出在函数
mt_test_add_work()
中:您定义了一个具有自动存储功能的参数数组:该对象在堆栈上分配,使用 8382264 字节的堆栈空间。这对于您的系统来说太过分了,会导致调用链中未定义的行为,进一步使用堆栈实际上会导致分段错误:堆栈溢出的典型情况。
您应该在退出函数之前从堆中分配该对象并释放它:
In function
new_thread_pool()
, you neitherthread_pool.pool = malloc(sizeof(pthread_t) * num_threads);
norpthread_create(&thread_pool.pool[i], NULL, generic_thread_function, &thread_pool);
Trying to create 349261 or more threads on any system looks more like a stress test than a real life purpose. Test for errors and report them in a usable way.
new_node
does not check for allocation failure either. Unless you instrument your code for this, you should use a wrapper aroundmalloc()
calls to detect allocation failure and abort the program with an error message.The issue in your code is in the function
mt_test_add_work()
: you define an array of arguments with automatic storage:This object is allocated on the stack, using 8382264 bytes of stack space. This is too much for your system and causes undefined behavior down the call chain where further stack usage actually cause a segmentation fault: a typical case of Stack Overflow.
You should allocate this object from the heap and free it before exiting the function: