Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mactavish96
ALP4-Tutorials
Commits
7e028c8c
Commit
7e028c8c
authored
2 years ago
by
Mactavish
Browse files
Options
Downloads
Patches
Plain Diff
add deadlock exercise
parent
61d303dc
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
exercises/deadlock/Makefile
+19
-0
19 additions, 0 deletions
exercises/deadlock/Makefile
exercises/deadlock/README.md
+20
-0
20 additions, 0 deletions
exercises/deadlock/README.md
exercises/deadlock/simple_deadlock.c
+75
-0
75 additions, 0 deletions
exercises/deadlock/simple_deadlock.c
with
114 additions
and
0 deletions
exercises/deadlock/Makefile
0 → 100644
+
19
−
0
View file @
7e028c8c
PROG
=
simple_deadlock
OBJECTS
=
simple_deadlock.o
CC
=
gcc
CFLAGS
=
-Wall
-std
=
c11
-pthread
CFLAGS
+=
-I
.
# add the current directory to the include path
.Phone
:
all
all
:
clean $(PROG)
#
build and run the program
./
$(
PROG
)
$(PROG)
:
$(OBJECTS)
#
link the object files into a binary
$(
CC
)
$(
CFLAGS
)
$^
-o
$@
$(OBJECTS)
:
%.o: %.c
#
compile the source files into object files
$(
CC
)
$(
CFLAGS
)
-c
$<
.PHONY
:
clean
clean
:
#
remove the object files and the binary
rm
-f
$(
OBJECTS
)
$(
PROG
)
This diff is collapsed.
Click to expand it.
exercises/deadlock/README.md
0 → 100644
+
20
−
0
View file @
7e028c8c
# Simple deadlock
Here is a small exercise that helps you understand common deadlock situation in C.
## Instructions
Read through the source code in
`simple_deadlock.c`
, try to run it multiple times.
Then fix the code that causes the deadlock. Recompile it and run it again.
## Build
```
bash
# build and run the executable
make
# clean all compilation files
make clean
```
This diff is collapsed.
Click to expand it.
exercises/deadlock/simple_deadlock.c
0 → 100644
+
75
−
0
View file @
7e028c8c
#include
<pthread.h>
#include
<stdio.h>
const
int
SIZE
=
10
;
pthread_mutex_t
g_mutex
,
even_mutex
,
odd_mutex
;
void
shared_print_thread_even
(
int
i
)
{
pthread_mutex_lock
(
&
even_mutex
);
pthread_mutex_lock
(
&
odd_mutex
);
printf
(
" %d "
,
i
);
pthread_mutex_unlock
(
&
odd_mutex
);
pthread_mutex_unlock
(
&
even_mutex
);
}
void
shared_print_thread_odd
(
int
i
)
{
pthread_mutex_lock
(
&
odd_mutex
);
pthread_mutex_lock
(
&
even_mutex
);
printf
(
" %d "
,
i
);
pthread_mutex_unlock
(
&
even_mutex
);
pthread_mutex_unlock
(
&
odd_mutex
);
}
void
shared_print_main
(
int
i
)
{
pthread_mutex_lock
(
&
g_mutex
);
printf
(
" %d "
,
i
);
pthread_mutex_unlock
(
&
g_mutex
);
}
void
*
f
(
void
*
arg
)
{
int
n
=
*
(
int
*
)
arg
;
for
(
int
i
=
SIZE
*
(
n
-
1
);
i
<
SIZE
*
n
;
i
++
)
{
if
(
n
%
2
==
0
)
shared_print_thread_even
(
i
);
else
shared_print_thread_odd
(
i
);
}
return
NULL
;
}
int
main
()
{
pthread_t
t1
,
t2
,
t3
,
t4
,
t5
;
// Initialize the mutexes
pthread_mutex_init
(
&
g_mutex
,
NULL
);
pthread_mutex_init
(
&
even_mutex
,
NULL
);
pthread_mutex_init
(
&
odd_mutex
,
NULL
);
int
t1_arg
=
1
,
t2_arg
=
2
,
t3_arg
=
3
,
t4_arg
=
4
,
t5_arg
=
5
;
// Create the threads
pthread_create
(
&
t1
,
NULL
,
f
,
&
t1_arg
);
pthread_create
(
&
t2
,
NULL
,
f
,
&
t2_arg
);
pthread_create
(
&
t3
,
NULL
,
f
,
&
t3_arg
);
pthread_create
(
&
t4
,
NULL
,
f
,
&
t4_arg
);
pthread_create
(
&
t5
,
NULL
,
f
,
&
t5_arg
);
for
(
int
i
=
-
1
;
i
>
-
SIZE
;
i
--
)
shared_print_main
(
i
);
// Wait for the threads to finish
pthread_join
(
t1
,
NULL
);
pthread_join
(
t2
,
NULL
);
pthread_join
(
t3
,
NULL
);
pthread_join
(
t4
,
NULL
);
pthread_join
(
t5
,
NULL
);
// Destroy the mutexes
pthread_mutex_destroy
(
&
g_mutex
);
pthread_mutex_destroy
(
&
even_mutex
);
pthread_mutex_destroy
(
&
odd_mutex
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment