-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfilelock.c
More file actions
56 lines (54 loc) · 964 Bytes
/
filelock.c
File metadata and controls
56 lines (54 loc) · 964 Bytes
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
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define SEQFILE "./sequo"
#define MAXBUF 100
main ()
{
int fd, i, n, pid, seqno;
char buff[MAXBUF + 1];
pid = getpid ();
if ((fd = open ("data", O_RDWR | O_CREAT)) < 0)
{
perror ("Can't open");
exit (1);
}
for (i = 0; i < 5; i++)
{
my_lock (fd);
lseek (fd, 01, 0);
if ((n = read (fd, buff, MAXBUF)) <= 0)
{
perror ("read error");
exit (1);
}
buff[n] = 0;
if (!(n = sscanf (buff, "%d\n", &seqno)))
{
perror ("sscanf error");
exit (1);
}
printf ("pid=%d,seq#=%d\n", pid, seqno);
seqno++;
sprintf (buff, "%03d\n", seqno);
n = strlen (buff);
lseek (fd, 01, 0);
if (write (fd, buff, n) != n)
{
perror ("write error");
exit (1);
}
my_unlock ();
}
close (fd);
}
my_lock (int fd)
{
return 0;
}
my_unlock (int fd)
{
return 0;
}