Skip to content

Commit 93b127d

Browse files
optiklaboptiklab
optiklab
authored and
optiklab
committed
Improved logic of flock.
1 parent 28c5dfe commit 93b127d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

multithreading/02 - Flock.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ int lock_write()
2525
return 0;
2626
}
2727

28-
flock(fd, LOCK_EX | LOCK_NB);
28+
if (flock(fd, LOCK_EX | LOCK_NB) < 0)
29+
{
30+
printf("Access to LOCK error.\n");
31+
close(fd);
32+
return 0;
33+
}
2934

3035
const char buf[] = "ABRACADABRA";
3136
write(fd, &buf, strlen(buf) + 1);
@@ -47,7 +52,12 @@ int lock_read(pid_t pid)
4752
return 0;
4853
}
4954

50-
flock(fd, LOCK_SH | LOCK_NB);
55+
if (flock(fd, LOCK_SH | LOCK_NB) < 0)
56+
{
57+
printf("Access to LOCK error.\n");
58+
close(fd);
59+
return 0;
60+
}
5161

5262
char buf[12];
5363
read(fd, &buf, 12);

0 commit comments

Comments
 (0)