简体   繁体   English

使用 pthread 的简单死锁示例

[英]simple deadlock example using pthread

I am trying to understand deadlock with simple example using two resources rs1 and rs2, both has their own mutex locks, so proc1 locks resource1 and trying to get resource2, at the same time proc2 locks resource2 and trying to get resource1, so both are in deadlock.我试图通过使用两个资源 rs1 和 rs2 的简单示例来理解死锁,它们都有自己的互斥锁,因此 proc1 锁定资源 1 并尝试获取资源 2,同时 proc2 锁定资源 2 并尝试获取资源 1,因此两者都在僵局。 Following program shows deadlock scenario, but the problem is why both "p1 trying to get rs2" and "p2 tying to get rs1" are not printing by proc1 and proc2 respectively...以下程序显示了死锁场景,但问题是为什么“p1 试图获取 rs2”和“p2 绑定获取 rs1”都没有分别由 proc1 和 proc2 打印......

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

pthread_mutex_t resource1,resource2;
int test=0;
void *proc1()
{
    printf("\nThis is proc1 using rs1");
    pthread_mutex_lock(&resource1);
        usleep(200);
        printf("\np1 trying to get rs2..."); 
        pthread_mutex_lock(&resource2);
            test++;
        printf("\nproc1 got rs2!!");    
        pthread_mutex_unlock(&resource2);   
     pthread_mutex_unlock(&resource1);  
return 0;
}

void *proc2()
{
    printf("\nThis is proc2 using rs2");
    pthread_mutex_lock(&resource2);
        usleep(200);
        printf("\np2 trying to get rs1..."); 
        pthread_mutex_lock(&resource1);
            test--;
        printf("\nproc2 got rs1!!");    
        pthread_mutex_unlock(&resource1);   
     pthread_mutex_unlock(&resource2);  
return 0;
}

int main(){
    pthread_t t1,t2;
    pthread_mutex_init(&resource1, NULL);
    pthread_mutex_init(&resource2, NULL);

    pthread_create(&t1,NULL, proc1 , NULL);
    pthread_create(&t2,NULL, proc2 , NULL);

    pthread_join(t1,NULL);  
    pthread_join(t2,NULL);
// will never arrive here
    pthread_mutex_destroy(&resource1);
    pthread_mutex_destroy(&resource2);
}
//This program executes in order, have a look

pthread_mutex_t lock1, lock2;

void *resource1(){

    pthread_mutex_lock(&lock1);

    printf("Job started in resource1..\n");
    sleep(2);

    printf("Trying to get resourc2\n");
    pthread_mutex_lock(&lock2); 
    printf("Aquired resourc2\n");
    pthread_mutex_unlock(&lock2);

    printf("Job finished in resource1..\n");

    pthread_mutex_unlock(&lock1);

    pthread_exit(NULL);

}

void *resource2(){

    pthread_mutex_lock(&lock2);

    printf("Job started in resource2..\n");
    sleep(2);

    printf("Trying to get resourc1\n");
    pthread_mutex_lock(&lock1); 
    printf("Aquired resourc1\n");
    pthread_mutex_unlock(&lock1);

    printf("Job finished in resource2..\n");

    pthread_mutex_unlock(&lock2);

    pthread_exit(NULL);

}



int main() {

    pthread_mutex_init(&lock1,NULL);
    pthread_mutex_init(&lock2,NULL);

    pthread_t t1,t2;

    pthread_create(&t1,NULL,resource1,NULL);
    pthread_create(&t2,NULL,resource2,NULL);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);

    return 0;

}

It might have something to do with how you print the messages.这可能与您打印消息的方式有关。 stdout is line buffered , which means the buffers are flushed on newline. stdoutline buffered ,这意味着缓冲区在换行符上刷新。 Try adding a newline last in the strings you print, or to explicitly flush the buffers with fflush .尝试在您打印的字符串的最后添加一个换行符,或者使用fflush显式刷新缓冲区。 – Some programmer dude – 一些程序员哥们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM