-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdead_flock.c
More file actions
68 lines (56 loc) · 1.47 KB
/
dead_flock.c
File metadata and controls
68 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Does flock() detect deadlock situations and return an error?
**/
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <stdio.h>
#define FILE1 "/tmp/deadflock1.tmp"
#define FILE2 "/tmp/deadflock2.tmp"
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
int fd1, fd2;
unlink(FILE1);
unlink(FILE2);
switch(fork()) {
case -1:
exit_err("fork");
case 0:
// Have to open them in the child process, as lock object is
// associated with the open file description, rather than fd...
if ((fd1 = open(FILE1, O_CREAT | O_RDWR, S_IRWXU)) == -1)
exit_err("open1");
if ((fd2 = open(FILE2, O_CREAT | O_RDWR, S_IRWXU)) == -1)
exit_err("open2");
puts("[Child] Lock fd1");
if (flock(fd1, LOCK_EX) == -1)
exit_err("fd1");
sleep(1);
puts("[Child] @T = 1s: P1 <-> fd1 && P2 <-> fd2");
puts("[Child] We try to flock fd2, and block.");
if (flock(fd2, LOCK_EX) == -1)
exit_err("fd2");
puts("[Child] DONE");
exit(EXIT_SUCCESS);
default:
if ((fd1 = open(FILE1, O_CREAT | O_RDWR, S_IRWXU)) == -1)
exit_err("open1");
if ((fd2 = open(FILE2, O_CREAT | O_RDWR, S_IRWXU)) == -1)
exit_err("open2");
puts("[Parent] Lock fd2");
if (flock(fd2, LOCK_EX) == -1)
exit_err("fd2");
sleep(2);
puts("[Parent] Lock fd1. Deadlock.");
if (flock(fd1, LOCK_EX) == -1)
exit_err("fd1");
}
puts("[Parent] DONE");
unlink(FILE1);
unlink(FILE2);
exit(EXIT_SUCCESS);
}