-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflock_ex.c
75 lines (66 loc) · 2.85 KB
/
flock_ex.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
68
69
70
71
72
73
74
75
/* -*- Mode:C; Coding:us-ascii-unix; fill-column:132 -*- */
/**********************************************************************************************************************************/
/**
@file flock_ex.c
@author Mitch Richling <https://www.mitchr.me/>
@Copyright Copyright 1998 by Mitch Richling. All rights reserved.
@brief How to use flock()
@Keywords UNIX file lock flock
@Std ISOC POSIX UNIX98 BSD4.3 SYSV3
@Tested
- MacOS X.3
This C program is intended to illustrate how to use flock(). flock() provides an interface for advisory locking that is less
complex to use than other locking systems. flock is widely available on across different UNIX systems. Test this program by
running two copies at the same time, one started slightly after the first one.
***********************************************************************************************************************************/
#include <stdio.h> /* I/O lib C89 */
#include <sys/types.h> /* UNIX types POSIX */
#include <errno.h> /* error stf POSIX */
#include <stdlib.h> /* Standard Lib C89 */
#include <sys/stat.h> /* UNIX stat POSIX */
#include <sys/file.h> /* Locks UNIX */
/**********************************************************************************************************************************/
int main() {
int FD;
char buf[] = "hello\n";
int i;
if((FD = open("foo", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
perror("ERROR: File open");
exit(10);
} /* end if */
/*
LOCK_SH shared lock
LOCK_EX exclusive lock
LOCK_NB don't block when locking
LOCK_UN unlock
*/
printf("Attempting to get lock.\n");
/* NOTE: Use LOCK_SH for shared lock. The LOCK_NB part keeps the call from blocking, and gives us the chance to make intelligent
decisions in the face of a previously locked resource. */
while(flock(FD, LOCK_EX | LOCK_NB) < 0) {
if(errno == EWOULDBLOCK) {
printf("Could not get lock. Sleeping...\n");
sleep(1);
} else if(errno == EBADF) {
printf("ERROR: The descriptor was not valid.\n");
exit(1);
} else if(errno == EINVAL) {
printf("ERROR: The descriptor was not a file.\n");
exit(1);
} else if(errno == EOPNOTSUPP) {
printf("ERROR: The referenced descriptor is not of the correct type.\n");
exit(1);
} /* end else/if */
} /* end while */
printf("Got the lock.\n");
/* We ignore possible I/O errors as our goal here is to demo locking, not how to do I/O in a safe way... */
for(i=0; i<10; i++) {
printf("Write..");
write(FD, buf, strlen(buf));
printf("Sleep..\n");
sleep(1);
} /* end for */
close(FD);
printf("Free up lock.\n");
flock(FD, LOCK_UN);
} /* end func main */