int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd ,struct flock* lock);
struct flock
{
short l_type; /*锁的类型*/
off_t l_start; /*偏移量的起始位置:SEEK_SET,SEEK_CUR,SEEK_END*/
short l_whence; /*加锁的起始偏移*/
off_t l_len; /*上锁字节*/
pid_t l_pid; /*锁的属主进程ID */
}
/* lock_set.c */
int lock_set(int fd, int type)
{
struct flock old_lock, lock;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_type = type;
lock.l_pid = -1;
/* 判断文件是否可以上锁 */
fcntl(fd, F_GETLK, &lock);
//文件已有锁
if (lock.l_type != F_UNLCK)
{
/* 判断文件不能上锁的原因 */
if (lock.l_type == F_RDLCK) /* 该文件已有读取锁 */
{
printf("Read lock already set by %d\n", lock.l_pid);
}
else if (lock.l_type == F_WRLCK) /* 该文件已有写入锁 */
{
printf("Write lock already set by %d\n", lock.l_pid);
}
}
/* l_type 可能已被F_GETLK修改过 */
lock.l_type = type;
/* 根据不同的type值进行阻塞式上锁或解锁 */
if ((fcntl(fd, F_SETLKW, &lock)) < 0)
{
printf("Lock failed:type = %d\n", lock.l_type);
return 1;
}
switch(lock.l_type)
{
case F_RDLCK:
{
printf("Read lock set by %d\n", getpid());
}
break;
case F_WRLCK:
{
printf("Write lock set by %d\n", getpid());
}
break;
case F_UNLCK:
{
printf("Release lock by %d\n", getpid());
return 1;
}
break;
default:
break;
}/* end of switch */
return 0;
}
创建一个hello文件,之后对其上写入锁,键盘输入任意一个字符后解除写入锁。
/* write_lock.c */
#include
#include
#include
#include
#include
#include
#include "lock_set.c"
int main(void)
{
int fd;
/* 首先打开文件 */
fd = open("hello",O_RDWR | O_CREAT, 0644);
if(fd < 0)
{
printf("Open file error\n");
exit(1);
}
/* 给文件上写入锁 */
//调用lock_set函数
lock_set(fd,F_WRLCK);
getchar(); //输入任意字符
/* 给文件解锁 */
lock_set(fd,F_UNLCK);
getchar(); //
close(fd);
exit(0);
}
补充笔记:关于Linux权限
Linux 系统中采用三位十进制数表示权限,如0755, 0644.
ABCD
A- 0, 表示十进制
B-用户
C-组用户
D-其他用户
--- -> 0 (no excute , no write ,no read)
--x -> 1 excute, (no write, no read)
-w- -> 2 write
-wx -> 3 write, excute
r-- -> 4 read
r-x -> 5 read, excute
rw- -> 6 read, write ,
rwx -> 7 read, write , excute
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
/* read_lock.c */
#include
#include
#include
#include
#include
#include
#include "lock_set.c"
int main(void)
{
int fd;
fd = open("hello",O_RDWR | O_CREAT, 0644);
if(fd < 0)
{
printf("Open file error\n");
exit(1);
}
/* 给文件上读取锁 */
lock_set(fd,F_RDLCK);
getchar(); //输入任意字符
/* 给文件解锁 */
lock_set(fd,F_UNLCK);
getchar(); //输入任意字符
close(fd);
exit(0);
}