A simple deadlock example using pthread

I'm trying to figure out a dead end with a simple example, using two resources rs1 and rs2, both have their own mutex locks, so proc1 is blocking resource1 and trying to get resource2, while proc2 is blocking resource2 and trying to get resource1, so both are stuck. The following program shows a deadlock scenario, but the problem is that both "p1 trying to get rs2" and "p2 ting to get rs1" do not print proc1 and proc2 respectively ...

#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);
}

      

+3


source to share


2 answers


It might have something to do with how you type your messages. stdout

is a buffered string , which means that buffers are flushed with a newline. Try adding a newline last to the lines you are printing, or explicitly flushing the buffers with fflush

. - some programmer dude



0


source


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;

      

}

// This program is executed in order, looks like

0


source







All Articles