c - pthread_mutex_trylock 段错误

标签 c locking pthreads segmentation-fault deadlock

我花了一周的时间来修复一个程序,一开始我得到了SIGBUS,但经过多次尝试后程序仍然得到了SIGSEGV段错误,在下面我发布了段错误日志+源代码。如果专家帮助我修复此段错误错误,我将非常感激。任何建议都将受到高度赞赏。 提前致谢

LL_NODE *ll_prepend(LLIST *l, void *obj)
{
    if (l && obj) {
//line bellow is module-datastruct-llist.c:167 
//mentioned in segment-fault log as frame 3

      if (!ll_lock(l)) return NULL;
        LL_NODE *new;
        if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
        new->obj = obj;
        new->nxt = l->initial;
        l->initial = new;
        if (!l->last)
            l->last = l->initial;
        l->count++;
        ll_unlock(l);
        return new;
    }
    return NULL;
}






int32_t ll_lock(LLIST *l)
{
    int32_t res = 1;
   res=cs_trylock(&l->lock);

//line bellow is module-datastruct-llist.c:51  
//mentioned in segment-fault log as frame 2

  while (l && !l->flag && res) {
        cs_debug_mask(D_TRACE, "trylock ll_lock wait");
        cs_sleepms(fast_rnd()%5 + 1);
    }
    return !res;
}







int32_t cs_trylock(pthread_mutex_t *mutex){

    if(!mutex) return -1;
        int32_t result, oldtype;
        /* Make sure that we won't get interrupted while getting the lock */
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

//line bellow is oscam-simples.c:1233  
//mentioned in segment-fault log as frame 1

        if((result=pthread_mutex_trylock(mutex)) == 0){

            struct s_client *cl = cs_preparelock(cur_client(), mutex);
            if(cl)
                cl->mutexstore_used++;
        }
        pthread_setcanceltype(oldtype, NULL);
        pthread_testcancel();
        return result;
    }

// in this function &l->lock is initialized   
LLIST *ll_create()
{
    LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
    pthread_mutex_init(&l->lock, NULL);
    return l;
}



Segment fault log :
Program received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1905]
0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
(gdb) bt
#0  0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
#1  0x00410d98 in cs_trylock (mutex=0x247373a4) at oscam-simples.c:1233
#2  0x0043d4aa in ll_lock (l=0x24737398) at module-datastruct-llist.c:51
#3  0x0043d956 in ll_prepend (l=0x24737398, obj=0x4a2410)
    at module-datastruct-llist.c:167
#4  0x0040a66e in get_cw (client=0x4daa80, er=0x5063a0) at oscam.c:2645
#5  0x00439754 in dvbapi_process_input (demux_id=0, filter_num=0,
    buffer=0x2a98bb60 "\201q=", len=320) at module-dvbapi.c:1634
#6  0x0043c866 in stapi_read_thread (sparam=0x4d1558) at module-dvbapi.c:2441
#7  0x29799486 in ?? () from /lib/libpthread.so.0
Backtrace stopped: frame did not save the PC
(gdb) info args
No symbol table info available.

最佳答案

问题来自于 l->lock,它从未初始化,因此包含非 NULL 不可用的垃圾。

尝试添加l->lock = NULL;或通过创建互斥体正确初始化它。

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

相关文章:

c - 用c编写的listSort函数工作错误

c - 动态分配和指针

linux - 为什么在中断中使用 mutex_trylock 不安全?

c# - 线程和锁定资源

c - 在c中使用usleep作为Timer是否合适

c - 矩阵第 5 列所有数字的逆矩阵

c - LoadLibraryExW 加载 User32.dll 失败

MySQL、Perl DBI、execute() 和锁定(锁定的精确时刻)

pthreads - 我可以在共享的 POSIX 互斥锁被锁定时重新映射它吗?

c - pthread_cond_wait 的空参数