Skip to content

Commit

Permalink
deadlock simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
remzi-arpacidusseau committed May 19, 2019
1 parent 8935486 commit 9bff66d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion threads-bugs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ endif
SRCS := atomicity.c \
atomicity_fixed.c \
ordering.c \
ordering_fixed.c
ordering_fixed.c \
deadlock.c

OBJS := ${SRCS:c=o}
PROGS := ${SRCS:.c=}
Expand Down
5 changes: 5 additions & 0 deletions threads-bugs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Type `make` to build all examples.
- `ordering.c`: Shows the ordering problem from the book chapter
- `ordering_fixed.c`: Shows how to fix the problem with a condition variable

## Deadlock

- `deadlock.c`: Shows simple two-cycle deadlock
- `deadlock_run.sh`: Script to run the above program many times, until you hit a deadlock and are convinced deadlock can occur




54 changes: 54 additions & 0 deletions threads-bugs/deadlock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#include "common.h"
#include "common_threads.h"

pthread_mutex_t L1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t L2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *arg) {
printf("t1: begin\n");
printf("t1: try to acquire L1...\n");
Pthread_mutex_lock(&L1);
printf("t1: L1 acquired\n");
printf("t1: try to acquire L2...\n");
Pthread_mutex_lock(&L2);
printf("t1: L2 acquired\n");
Pthread_mutex_unlock(&L1);
Pthread_mutex_unlock(&L2);
return NULL;
}

void *thread2(void *arg) {
printf(" t2: begin\n");
printf(" t2: try to acquire L2...\n");
Pthread_mutex_lock(&L2);
printf(" t2: L2 acquired\n");
printf(" t2: try to acquire L1...\n");
Pthread_mutex_lock(&L1);
printf(" t2: L1 acquired\n");
Pthread_mutex_unlock(&L1);
Pthread_mutex_unlock(&L2);

return NULL;
}

int main(int argc, char *argv[]) {
if (argc != 1) {
fprintf(stderr, "usage: main\n");
exit(1);
}
pthread_t p1, p2;
printf("main: begin\n");
Pthread_create(&p1, NULL, thread1, NULL);
Pthread_create(&p2, NULL, thread2, NULL);
// join waits for the threads to finish
Pthread_join(p1, NULL);
Pthread_join(p2, NULL);
printf("main: end\n");
return 0;
}

7 changes: 7 additions & 0 deletions threads-bugs/deadlock_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /bin/bash

while true; do
./deadlock
done


0 comments on commit 9bff66d

Please sign in to comment.