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
76
77
78
79
80
81
82
83
84
85
86
87
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define NUM_FILES 100000
void open_lock_close (char *name);
void delete_file (char *name);
int
main ()
{
int ret = 0;
char filename[256] = {0,};
int i = 0;
for (i = 1; i <= NUM_FILES ; i++) {
snprintf (filename, sizeof (filename), "file-%d", i);
open_lock_close (filename);
}
/* for (i = 1; i <= 100000; i++) { */
/* snprintf (filename, sizeof (filename), "file-%d", i); */
/* delete_file (filename); */
/* } */
return 0;
}
void
open_lock_close (char *name)
{
int ret = 0;
int fd = -1;
struct flock lock;
char *data = "This is a line";
fd = open (name, O_CREAT|O_RDWR, 0644);
if (fd == -1) {
fprintf (stderr, "Error: cannot open the file %s (%s)\n", name,
strerror (errno));
return;
}
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = 0;
ret = fcntl (fd, F_SETLK, &lock);
if (ret == -1)
fprintf (stderr, "Error: cannot lock the file %s (%s)\n", name,
strerror (errno));
ret = flock (fd, LOCK_SH);
if (ret == -1)
fprintf (stderr, "Error: cannot flock the file %s (%s)\n", name,
strerror (errno));
ret = write (fd, data, strlen (data));
if (ret == -1)
fprintf (stderr, "Error: cannot write the file %s (%s)\n", name,
strerror (errno));
close (fd);
return;
}
void
delete_file (char *name)
{
int ret = 0;
ret = unlink (name);
if (ret < 0)
fprintf (stderr, "Error: Cannot unlink the file %s (%s)\n",
name, strerror (errno));
return;
}
|