Skip to content

Example

TDacik edited this page Oct 17, 2019 · 1 revision
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;

void function1()
{
	pthread_mutex_lock(&lock1);
	pthread_mutex_unlock(&lock1);
}

void function4()
{
	pthread_mutex_lock(&lock1);
}

void function5()
{
	pthread_mutex_lock(&lock2);
	pthread_mutex_unlock(&lock2);
	pthread_mutex_unlock(&lock1);
}

void function3()
{
	function4();
	function5();
}

void function2()
{
	function3();
}

void *thread1(void *v)
{
	function2();

	return NULL;
}

void *thread2(void *v)
{
	pthread_mutex_lock(&lock2);
	function1();
	pthread_mutex_unlock(&lock2);

	return NULL;
}

int main()
{	
	pthread_t threads[2];

	pthread_create(&threads[0], NULL, thread1, NULL);
	pthread_create(&threads[1], NULL, thread2, NULL);

	pthread_join(threads[0], NULL);
	pthread_join(threads[1], NULL);

	return 0;
}

Output:

[kernel] Parsing simple_dl_trace.c (with preprocessing)
[Deadlock] Deadlock analysis started
[Deadlock] === Assumed threads: ===
[Deadlock] main
[Deadlock] thread1
[Deadlock] thread2
[Deadlock] === Lockgraph: ===
[Deadlock] lock1 -> lock2
[Deadlock] lock2 -> lock1
[Deadlock] ==== Results: ====
[Deadlock] Deadlock between threads thread1 and thread2:
  
  Trace of dependency (lock2 -> lock1):
  In thread thread2:
      Lock of lock2 (simple_dl_trace.c:46)
  
      Call of function1 (simple_dl_trace.c:47)
      Lock of lock1 (simple_dl_trace.c:10)
  
  Trace of dependency (lock1 -> lock2):
  In thread thread1:
    Call of function2 (simple_dl_trace.c:39)
    Call of function3 (simple_dl_trace.c:34)
      Call of function4 (simple_dl_trace.c:28)
      Lock of lock1 (simple_dl_trace.c:16)
  
      Call of function5 (simple_dl_trace.c:29)
      Lock of lock2 (simple_dl_trace.c:21)
Clone this wiki locally