pthread条件变量和互斥锁,程序有死锁

时间:2015-04-14 03:39:48

标签: c multithreading pthreads deadlock

我的多线程代码有问题,有一个死锁,但我不知道如何修复它,因为我对pthreads很新。

下面的代码显示了一个名为“idle”的函数,它等待模拟进程放在readyqueue上,push函数应该将该进程放在队列上,然后发出信号表明有一个进程准备好了调度。

以下是导致死锁的两个函数:

void push(pcb_t *pcb) {
    pthread_mutex_lock(&queue_mutex);
    if (head == NULL) {
        head = pcb;
        tail = pcb;
    } else {
        pcb_t *old_tail = tail;
        tail = pcb;
        old_tail->next = tail;
    }
    pthread_cond_signal(&proc_ready);
    pthread_mutex_unlock(&queue_mutex);
}
extern void idle(unsigned int cpu_id)
{
    pthread_mutex_lock(&queue_mutex);

    while(head == NULL) {
        pthread_cond_wait(&proc_ready, &queue_mutex);
    }
    pthread_mutex_unlock(&queue_mutex);
    schedule(cpu_id);

    /*

     * idle() must block when the ready queue is empty, or else the CPU threads
     * will spin in a loop.  Until a ready queue is implemented, we'll put the
     * thread to sleep to keep it from consuming 100% of the CPU time.  Once
     * you implement a proper idle() function using a condition variable,
     * remove the call to mt_safe_usleep() below.
     */
}

这是gdb的回溯:

Thread 2 (Thread 0xb7df7b40 (LWP 3443)):
#0  0xb7fdd424 in __kernel_vsyscall ()
No symbol table info available.
#1  0xb7fb59e2 in __lll_lock_wait ()
    at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:144
No locals.
#2  0xb7fb1267 in _L_lock_847 () from /lib/i386-linux-gnu/libpthread.so.0
No symbol table info available.
#3  0xb7fb10a0 in __GI___pthread_mutex_lock (mutex=0x804c75c <queue_mutex>)
    at ../nptl/pthread_mutex_lock.c:79
        __PRETTY_FUNCTION__ = "__pthread_mutex_lock"
        type = 0
        id = <optimized out>
#4  0x080489d9 in idle (cpu_id=0) at student.c:116
No locals.
#5  0x08048f65 in simulator_cpu_thread (cpu_id=0) at os-sim.c:259
        state = CPU_IDLE
#6  0x08049b4a in simulator_cpu_thread_func (data=0x0) at os-sim.c:618
No locals.
#7  0xb7faef70 in start_thread (arg=0xb7df7b40) at pthread_create.c:312
        __res = <optimized out>
        pd = 0xb7df7b40
        now = <optimized out>
        unwind_buf = {cancel_jmp_buf = {{jmp_buf = {-1208217600, -1210090688, 4001536, 
                -1210092504, -193645269, -1085428949}, mask_was_saved = 0}}, priv = {
            pad = {0x0, 0x0, 0x0, 0x0}, data = {prev = 0x0, cleanup = 0x0, 
              canceltype = 0}}}
        not_first_call = <optimized out>
        pagesize_m1 = <optimized out>
        sp = <optimized out>
        freesize = <optimized out>
---Type <return> to continue, or q <return> to quit---
        __PRETTY_FUNCTION__ = "start_thread"
#8  0xb7ee5bee in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129
No locals.

Thread 1 (Thread 0xb7df8700 (LWP 3439)):
#0  0xb7fdd424 in __kernel_vsyscall ()
No symbol table info available.
#1  0xb7fb59e2 in __lll_lock_wait ()
    at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:144
No locals.
#2  0xb7fb1267 in _L_lock_847 () from /lib/i386-linux-gnu/libpthread.so.0
No symbol table info available.
#3  0xb7fb10a0 in __GI___pthread_mutex_lock (mutex=0x804c75c <queue_mutex>)
    at ../nptl/pthread_mutex_lock.c:79
        __PRETTY_FUNCTION__ = "__pthread_mutex_lock"
        type = 0
        id = <optimized out>
#4  0x0804886f in push (pcb=0x804c660 <processes>) at student.c:42
No locals.
#5  0x08048acb in wake_up (process=0x804c660 <processes>) at student.c:205
No locals.
#6  0x08049a14 in simulate_io () at os-sim.c:590
        completed = 0x804d0f8
        pcb = 0x804c660 <processes>
#7  0x08048e0f in simulator_supervisor_thread () at os-sim.c:189
No locals.
#8  0x08048dcc in start_simulator (new_cpu_count=1) at os-sim.c:161
        n = 1
        __PRETTY_FUNCTION__ = "start_simulator"
#9  0x08048ba8 in main (argc=2, argv=0xbffff284) at student.c:240
        cpu_count = 1
        __PRETTY_FUNCTION__ = "main"

编辑:pop函数也使用queue_mutex

pcb_t * pop() {
    pcb_t *pcb = NULL;
    pthread_mutex_lock(&queue_mutex);
    if (head == NULL) {
        return NULL;
    } else if (head == tail) {
        pcb = head;
        head = NULL;
        tail = NULL;
    } else {
        pcb = head;
        head = head->next;
    }
    pthread_mutex_unlock(&queue_mutex);
    pcb->next = NULL;
    return pcb;
}

3 个答案:

答案 0 :(得分:3)

你的pop函数在head == NULL的情况下不会解锁互斥锁。

答案 1 :(得分:0)

null函数中控制头是pop之后,您的互斥对象应该以适当的方式被锁定,否则它将被阻止。而且,pthread_cond_signal函数可以由线程调用,无论它当前是否拥有调用pthread_cond_wait的线程在其等待期间与条件变量相关联的互斥锁;但是,如果需要可预测的调度行为,那么该互斥锁将被调用pthread_cond_signal的线程锁定。

答案 2 :(得分:-2)

空闲功能会锁定互斥锁,然后锁定 pthread_cond_wait 上的锁定。然后当调度程序感觉像这样时,允许执行推送功能,但推送功能的第一件事是尝试抓取互斥锁。不幸的是, mutex 已归空闲主题所有。因此 push 线程阻塞,等待互斥锁被释放,这将永远不会发生,因为空闲函数是 pthread_cond_wait 上的自旋锁定