1. sample學習代碼
[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define read_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
#define is_read_lockable(fd, offset, whence, len) \
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) \
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
pid_t
lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
if (fcntl(fd, F_GETLK, &lock) < 0) {
printf("fcntl error\n");
exit(1);
}
if (lock.l_type == F_UNLCK)
return (0);
return (lock.l_pid);
}
int
lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
return (fcntl(fd, cmd, &lock));
}
int
main(int argc, char *argv[])
{
int fd;
int flag;
int result;
if (argc != 3) {
printf("usage %s -[rw] <filename>\n", argv[0]);
exit(1);
}
if (argv[1][0] == 'r')
flag = O_RDONLY;
else if (argv[1][0] == 'w')
flag = O_WRONLY;
else {
printf("invalid flag: 'r' or 'w'\n");
exit(1);
}
if ((fd = open(argv[2], flag)) < 0) {
printf("cannot open %s, %s\n", argv[2], strerror(errno));
exit(2);
}
printf("open successfully\n");
// test if write lock work.
if (flag == O_RDONLY) {
printf("read lock, ");
result = read_lock(fd, 0, SEEK_SET, 0);
}
else {
printf("write lock, ");
result = write_lock(fd, 0, SEEK_SET, 0);
}
if (result < 0) {
printf("lock fail, %s\n", strerror(errno));
exit(3);
}
printf("success!\n");
while (1) {
//# loop forever;
}
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define read_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
#define is_read_lockable(fd, offset, whence, len) \
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) \
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
pid_t
lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
if (fcntl(fd, F_GETLK, &lock) < 0) {
printf("fcntl error\n");
exit(1);
}
if (lock.l_type == F_UNLCK)
return (0);
return (lock.l_pid);
}
int
lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
return (fcntl(fd, cmd, &lock));
}
int
main(int argc, char *argv[])
{
int fd;
int flag;
int result;
if (argc != 3) {
printf("usage %s -[rw] <filename>\n", argv[0]);
exit(1);
}
if (argv[1][0] == 'r')
flag = O_RDONLY;
else if (argv[1][0] == 'w')
flag = O_WRONLY;
else {
printf("invalid flag: 'r' or 'w'\n");
exit(1);
}
if ((fd = open(argv[2], flag)) < 0) {
printf("cannot open %s, %s\n", argv[2], strerror(errno));
exit(2);
}
printf("open successfully\n");
// test if write lock work.
if (flag == O_RDONLY) {
printf("read lock, ");
result = read_lock(fd, 0, SEEK_SET, 0);
}
else {
printf("write lock, ");
result = write_lock(fd, 0, SEEK_SET, 0);
}
if (result < 0) {
printf("lock fail, %s\n", strerror(errno));
exit(3);
}
printf("success!\n");
while (1) {
//# loop forever;
}
exit(0);
}