linux 记录锁的应用
2013-09-26 11:28
405 查看
//文件加读写锁
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
void my_err(const char *err_string, int line)
{
fprintf(stderr, "line: %d ", line);
perror(err_string);
exit(1);
}
int lock_set(int fd, struct flock *lock)
{
if(fcntl(fd, F_SETLK, lock) == 0)
{
if (lock->l_type == F_RDLCK)
{
printf("set read lock, pid:%d\n", getpid());
}
else if (lock->l_type == F_WRLCK)
{
printf("set write lock, pid:%d\n", getpid());
}
else if (lock->l_type == F_UNLCK)
{
printf("release lock, pid:%d\n", getpid());
}
}
else
{
perror("lock operation fail\n");
return -1;
}
return 0;
}
//测试文件是否枷锁,没有加锁时返回0
int lock_test(int fd, struct flock *lock)
{
if (fcntl(fd, F_GETLK, lock) == 0)
{
if (lock->l_type == F_UNLCK)
{
printf("lock can be set in fd\n");
return 0;
}
else
{
if (lock->l_type == F_RDLCK)
{
printf("read lock has been set by:%d\n", lock->l_pid);
}
else if (lock->l_type == F_WRLCK)
{
printf("write lock has been set by:%d\n", lock->l_pid);
}
return -2;
}
}
else
{
perror("get incompatible locks fail");
return -1;
}
}
int main(int argc, char *argv[])
{
int fd;
int ret;
struct flock lock;
char buf[32];
if ((fd = open("/home/lhl/test/testa/lhl.cpp", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1)
{
my_err("open", __LINE__);
}
if(write(fd, "lihailong111", strlen("lihailong111")) != strlen("lihailong111"))
{
my_err("write", __LINE__);
}
memset(&lock, 0, sizeof(struct flock));
memset(buf, 0, sizeof(buf));
//对整个文件加锁
lock.l_start = SEEK_SET;
lock.l_whence = 0;
lock.l_len = 0;
//测试文件是否加了读锁
lock.l_type = F_RDLCK;
if (lock_test(fd, &lock) == 0)
{
//给文件加读锁
lock.l_type = F_RDLCK;
lock_set(fd, &lock);
}
lseek(fd, 0, SEEK_SET);
if ((ret = read(fd, buf, sizeof(buf))) < 0)
{
my_err("read", __LINE__);
}
printf("%s\n", buf);
getchar();
//测试文件是否加写锁
lock.l_type = F_WRLCK;
if (lock_test(fd, &lock) == 0)
{
//给文件加写锁
lock.l_type = F_WRLCK;
lock_set(fd, &lock);
}
//释放锁
lock.l_type = F_UNLCK;
lock_set(fd, &lock);
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
void my_err(const char *err_string, int line)
{
fprintf(stderr, "line: %d ", line);
perror(err_string);
exit(1);
}
int lock_set(int fd, struct flock *lock)
{
if(fcntl(fd, F_SETLK, lock) == 0)
{
if (lock->l_type == F_RDLCK)
{
printf("set read lock, pid:%d\n", getpid());
}
else if (lock->l_type == F_WRLCK)
{
printf("set write lock, pid:%d\n", getpid());
}
else if (lock->l_type == F_UNLCK)
{
printf("release lock, pid:%d\n", getpid());
}
}
else
{
perror("lock operation fail\n");
return -1;
}
return 0;
}
//测试文件是否枷锁,没有加锁时返回0
int lock_test(int fd, struct flock *lock)
{
if (fcntl(fd, F_GETLK, lock) == 0)
{
if (lock->l_type == F_UNLCK)
{
printf("lock can be set in fd\n");
return 0;
}
else
{
if (lock->l_type == F_RDLCK)
{
printf("read lock has been set by:%d\n", lock->l_pid);
}
else if (lock->l_type == F_WRLCK)
{
printf("write lock has been set by:%d\n", lock->l_pid);
}
return -2;
}
}
else
{
perror("get incompatible locks fail");
return -1;
}
}
int main(int argc, char *argv[])
{
int fd;
int ret;
struct flock lock;
char buf[32];
if ((fd = open("/home/lhl/test/testa/lhl.cpp", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1)
{
my_err("open", __LINE__);
}
if(write(fd, "lihailong111", strlen("lihailong111")) != strlen("lihailong111"))
{
my_err("write", __LINE__);
}
memset(&lock, 0, sizeof(struct flock));
memset(buf, 0, sizeof(buf));
//对整个文件加锁
lock.l_start = SEEK_SET;
lock.l_whence = 0;
lock.l_len = 0;
//测试文件是否加了读锁
lock.l_type = F_RDLCK;
if (lock_test(fd, &lock) == 0)
{
//给文件加读锁
lock.l_type = F_RDLCK;
lock_set(fd, &lock);
}
lseek(fd, 0, SEEK_SET);
if ((ret = read(fd, buf, sizeof(buf))) < 0)
{
my_err("read", __LINE__);
}
printf("%s\n", buf);
getchar();
//测试文件是否加写锁
lock.l_type = F_WRLCK;
if (lock_test(fd, &lock) == 0)
{
//给文件加写锁
lock.l_type = F_WRLCK;
lock_set(fd, &lock);
}
//释放锁
lock.l_type = F_UNLCK;
lock_set(fd, &lock);
close(fd);
return 0;
}
相关文章推荐
- Linux学习之CentOS(二十九)--Linux网卡高级命令、IP别名及多网卡绑定
- Linux命令(5):cd命令
- NS-2相关工具大体介绍<2>
- Linux挂载光盘对/etc/fstab的修改
- Linux命令(4):yum命令
- linux 用户(User)和用户组(Group)配置文件详解
- Linux获取线程tid线程名
- Linux下集群技术应用概述
- php 变量 【PHP_EOL】 解决不同环境(win,linux,unix)下的按回车分隔问题
- 系统性学习linux文件系统
- Debug Android and Linux suspend and resume
- Linux命令(3):gcc命令
- 几个检查当前运行的LINUX是在VM还是在实体机中的方法
- Linux统计/监控工具SAR详细介绍
- linux内存管理--进程在内存中的分布
- Redhat Linux网卡配置与绑定
- linux下open和fopen的区别
- Linux网卡绑定探析
- linux cache swap 以及 虚拟内存等
- NS-2相关工具大体介绍<1>