当多个进程都需要对同一个文件进行读写操作的时候,为了确保在进程读写完成前不被其他进程修改,可以使用文件锁机制。
通过函数实现:
int fcntl(int fd, int cmd, struct flock *lock);
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
注意:如果len==0, 表示锁定从起点开始到文件尾的区域
锁的兼容性
当前文件区域 加读锁 加写锁
无锁 可以 可以
有读锁 可以 不可以
有写锁 不可以 不可以
例子:
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
/*define a function pid_printf*/
void pid_printf(char *format, ...)
{
va_list ap;
va_start(ap, format);
printf("[%d]%d:", getpid(),__LINE__);
vprintf(format, ap);
}
int set_lock(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);
}
/*readlock*/
#define read_lock(fd, offset, whence, len) \
set_lock(fd, F_SETLK, F_RDLCK, offset, whence, len)
/*readlock wouldblock*/
#define readw_lock(fd, offset, whence, len) \
set_lock(fd, F_SETLKW, F_RDLCK, offset, whence, len)
/*writelock*/
#define write_lock(fd, offset, whence, len) \
set_lock(fd, F_SETLK, F_WRLCK, offset, whence, len)
/*writelock wouldblock*/
#define writew_lock(fd, offset, whence, len) \
set_lock(fd, F_SETLKW, F_WRLCK, offset, whence, len)
/*unlock*/
#define un_lock(fd, offset, whence, len) \
set_lock(fd, F_SETLK, F_UNLCK, offset, whence, len)
pid_t test_lock(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) == -1)
{
perror("fcntl");
return -1;
}
if(lock.l_type == F_UNLCK)
{
return 0;
}
return lock.l_pid;
}
int main(int argc, char *argv[])
{
int fd;
pid_t pid;
pid_t lpid;
/*create a file*/
fd=creat("/tmp/flock_test", 0660);
if(fd == -1)
{
perror("create flock_test error!");
return -1;
}
pid_printf("/tmp/flock_test created, fd=[%d]\n", fd);
/*write lock*/
pid_printf("add writelock now\n");
write_lock(fd, 0, SEEK_SET, 0);
/*creat process*/
pid = fork();
if(pid==0)
{
/*子进程会继承父进程的环境和上下文中的大部分内容,包括文件描述符。此时父子进程享有相同的文件偏移量,执行相同的程序读取文件中的字符。程序执行结果是随机的,可能是父进程先读,产生一个偏移,再由子进程读其相邻字符*/
/*while(1)
{
lpid = test_lock(fd, F_RDLCK, 0,SEEK_SET, 0);
if(lpid > 0)
{
pid_printf("process [%d] locked /tmp/flock_test\n", lpid);
}
else if(lpid ==0)
{
pid_printf("/tmp/flock_test unlock\n");
break;
}
else
{
break;
}
sleep(1);
}*/
writew_lock(fd, 0, SEEK_SET, 0);
pid_printf("subprocess get writelock\n");
}
else if(pid != -1)
{
sleep(3);
pid_printf("unlock write lock of /tmp/flock_test now!\n");
/*unlock*/
un_lock(fd, 0, SEEK_SET, 0);
/*wait subprocess*/
wait(pid, NULL);
}
else
{
printf("there was an error with fork\n");
}
close(fd);
return 0;
}
/*涉及到的函数主要是fcntl()的应用*/