-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlockf.c
68 lines (55 loc) · 1.2 KB
/
lockf.c
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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
off_t filesize(char *file)
{
struct stat s;
int ret;
ret = stat(file, &s);
if (ret < 0) {
return -1;
}
return s.st_size;
}
int main(int argc, char **argv)
{
pid_t pid;
int fd;
off_t size;
int ret;
if (argc != 2) {
fprintf(stderr, "<%s> filename\n", argv[0]);
return -1;
}
size = filesize(argv[1]);
fd = open(argv[1], O_RDWR);
if (fd < 0) {
return -1;
}
pid = fork();
if (pid == 0) {
ret = lockf(fd, F_LOCK, size);
if (ret < 0) {
printf("cannot lock.. %s\n", strerror(errno));
return -1;
}
printf("lock acquired.. now set to sleep\n");
sleep(4);
printf("unlock..\n");
ret = lockf(fd, F_ULOCK, size);
} else {
sleep(1);
ret = lockf(fd, F_LOCK, size);
if (ret < 0) {
printf("cannto loock.. %s\n", strerror(errno));
return -1;
}
printf("lock acquired by parent..\n");
lockf(fd, F_ULOCK, size);
}
return 0;
}