gpt4 book ai didi

c - pthread_mutex_lock() 处的段错误

转载 作者:行者123 更新时间:2023-11-30 14:35:18 28 4
gpt4 key购买 nike

我有一个可以打印一些数据结构的代码。

void
print_wheel_timer(wheel_timer_t *wt){

<code to print data structure>
}

Then i sandwiched the code in-between lock and unlock mutex calls.

void
print_wheel_timer(wheel_timer_t *wt){
pthread_mutex_lock(&wt->global_lock);
<code to print data structure>
pthread_mutex_unlock(&wt->global_lock);
}

现在,出现了段错误。我已经禁用了代码中的所有线程,现在程序是单线程的,仍然是段错误。我使用的是 gcc,ubuntu 19.04。

gdb 显示 pthread_mutex_lock() 调用正在搞乱数据结构!这很奇怪!我将观察点放在由任意值写入的地址上,它显示对 pthread_mutex_lock() 的调用正在修改它。当我删除这些锁定/解锁调用时,没有看到段错误。

(gdb) watch *(glthread_t *)&(wt->slotlist[1].slots.right)
Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)
(gdb) c
Continuing.
Printing Wheel Timer DS

Thread 1 "test.exe" hit Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)

Old value = {left = 0x0, right = 0x0}
New value = {left = 0x1, right = 0x0} <<< corruption !!
0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
80 ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
#1 0x000055555555f3a2 in print_wheel_timer (wt=0x555555577180)
at WheelTimer/WheelTimer.c:276

Code :
========
void
print_wheel_timer(wheel_timer_t *wt){

int i = 0, j = 0;
glthread_t *curr;
glthread_t *slot_list_head = NULL;
wheel_timer_elem_t *wt_elem = NULL;

printf("Printing Wheel Timer DS\n");
pthread_mutex_lock(&wt->global_lock);
printf("wt->current_clock_tic = %d\n", wt->current_clock_tic);
printf("wt->clock_tic_interval = %d\n", wt->clock_tic_interval);
printf("wt->wheel_size = %d\n", wt->wheel_size);
printf("wt->current_cycle_no = %d\n", wt->current_cycle_no);
printf("wt->wheel_thread = %p\n", &wt->wheel_thread);
printf("WT uptime = %s\n", hrs_min_sec_format(WT_UPTIME(wt)));
printf("wt->no_of_wt_elem = %u\n", wt->no_of_wt_elem);
printf("printing slots : \n");

for(; i < wt->wheel_size; i++){
slot_list_head = WT_SLOTLIST_HEAD(wt, i);
ITERATE_GLTHREAD_BEGIN(slot_list_head, curr){ << segfaulting here for i = 1
wt_elem = glthread_to_wt_elem(curr);

如果某个神圣的灵魂想在他的机器上尝试一下。 请在这里下载代码: https://github.com/sachinites/tcpip_stack

After downloading :
switch to branch "Hellos"
git checkout Hellos
Compile :
make all
run
./test.exe
to reproduce , run the cmd :
debug show node H1 timer

现在它将以函数为核心:在WheelTimer/WheelTimer.c中实现的print_wheel_timer()

最佳答案

问题出在我的数据结构上,其中零长度数组是 C 结构的非最后一个成员,在读/写到此类数组时会损坏内存。可拉伸(stretch)数组应该是结构的最后一个成员。

typedef struct _wheel_timer_t {
int current_clock_tic;
int clock_tic_interval;
int wheel_size;
int current_cycle_no;
pthread_t wheel_thread;
slotlist_t slotlist[0]; << problem, make it last member
slotlist_t reschd_list;
unsigned int no_of_wt_elem;
pthread_mutex_t global_lock;
} wheel_timer_t;

关于c - pthread_mutex_lock() 处的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58596496/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com