#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
const char *test_file = "/tmp/test_lock";
/×为文件上锁程序×/
int main(int argc, char **argv)
{
int file_desc;
int byte_count;
char *byte_to_write = "A";
struct flock region_1; //锁结构体
struct flock region_2;
int res;
file_desc = open(test_file, O_RDWR | O_CREAT, 0666);
if(!file_desc)
{
fprintf(stderr, "unable to open %s for read/write\n", test_file);
exit(EXIT_FAILURE);
}
for(byte_count = 0; byte_count < 100; byte_count++)
{
(void)write(file_desc, byte_to_write, 1); //写入100个字符
}
/×填充锁结构体×/
region_1.l_type = F_RDLCK; //读锁
region_1.l_whence = SEEK_SET;//设置文件开始位置
region_1.l_start = 10;//距开始位置的偏移量
region_1.l_len = 20;//锁的区域大小
region_2.l_type = F_WRLCK;//写锁
region_2.l_whence = SEEK_SET;
region_2.l_start = 40;
region_2.l_len = 10;
printf("Process %d locking file\n", getpid());
//为文件上锁
res = fcntl(file_desc, F_SETLK, ®ion_1);
if(res == -1)
fprintf(stderr, "Failed to lock region_1\n");
res = fcntl(file_desc, F_SETLK, ®ion_2);
if(res == -1)
fprintf(stderr, "Failed to lock region_2\n");
sleep(60);
printf("Process %d closing file\n", getpid());
close(file_desc);
exit(EXIT_SUCCESS);
}
/×测试文件锁程序×/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
const char *test_file = "/tmp/test_lock";
#define SIZE_TO_TRY 5
void show_lock_info(struct flock *to_show);
int main(int argc, char **argv)
{
int file_desc;
int res;
struct flock region_to_test;
int start_byte;
file_desc = open(test_file, O_RDWR | O_CREAT, 0666);
if(!file_desc)
{
fprintf(stderr, "Unable to open %s for read/write\n", test_file);
return -1;
}
for(start_byte = 0; start_byte < 99; start_byte += SIZE_TO_TRY)
{
region_to_test.l_type = F_WRLCK;
region_to_test.l_whence = SEEK_SET;
region_to_test.l_start = start_byte;
region_to_test.l_len = SIZE_TO_TRY;
region_to_test.l_pid = -1;
printf("Testing F_WRLCK on region from %d to %d\n", start_byte, start_byte + SIZE_TO_TRY);
/×获取定锁信息×/
res = fcntl(file_desc, F_GETLK, ®ion_to_test); /获取锁信息失败,返回-1
if(res == -1)
{
fprintf(stderr, "F_GETLK failed\n");
return -1;
}
if(region_to_test.l_pid != -1) //如果pid==-1,则该区域没有上写锁,否则已上锁
{
printf("Lock would fail. F_GETLK returned\n");
show_lock_info(®ion_to_test);
}else{
printf("F_WRLCK -- lock would succeed\n");
}
region_to_test.l_type = F_RDLCK;
region_to_test.l_whence = SEEK_SET;
region_to_test.l_start = start_byte;
region_to_test.l_len = SIZE_TO_TRY;
region_to_test.l_pid = -1;
printf("Testing F_RDLCK on region from %d to %d\n", start_byte, start_byte + SIZE_TO_TRY);
res = fcntl(file_desc, F_GETLK, ®ion_to_test);
if(res == -1)
{
fprintf(stderr, "F_GETLK failed\n");
return -1;
}
if(region_to_test.l_pid != -1)
{
printf("Lock would fail. F_GETLK returned\n");
show_lock_info(®ion_to_test);
}else{
printf("F_RDLCK -- lock would succeed\n");
}
}
close(file_desc);
exit(EXIT_SUCCESS);
}
void show_lock_info(struct flock *to_show)//显示锁信息
{
printf("l_type: %d, ", to_show->l_type);
printf("l_whence: %d, ", to_show->l_whence);
printf("l_start: %lu, ", to_show->l_start);
printf("l_len: %lu, ", to_show->l_len);
printf("l_pid: %d\n", to_show->l_pid);
}