#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";
int main()
{
int file_desc;
int byte_count;
char *byte_to_write = "A";
struct flock region1;
struct flock region2;
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);
region1.l_type = F_RDLCK;
region1.l_whence = SEEK_SET;
region1.l_start = 10;
region1.l_len = 20;
region2.l_type = F_WRLCK;
region2.l_whence = SEEK_SET;
region2.l_start = 40;
region2.l_len = 10;
printf("Process %d locking file\n", getpid());
res = fcntl(file_desc, F_SETLK, ®ion1);
if(res == -1)
fprintf(stderr, "Failed to lock region1\n");
res = fcntl(file_desc, F_SETLK, ®ion2);
if(res == -1)
fprintf(stderr, "Failed to lock region2\n");
sleep(60);
printf("Princess %d closing file\n", getpid());
close(file_desc);
printf("SUCCESS=%d", EXIT_SUCCESS);
exit(EXIT_SUCCESS);
}
//文件锁的竞争
const char *test_file="/home/ndj/filelock/fcntl/test_lock.in";//文件对象
//const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";
int main()
{
int file_desc;
struct flock region_to_lock;
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);
}
//指定文件区域,并尝试在他们之上执行不同的锁定操作
region_to_lock.l_type = F_RDLCK;//共享锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 10;
region_to_lock.l_len = 5;
printf("Process %d, trying F_RDLCK, region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - failed to lock region\n", getpid());
// exit(EXIT_FAILURE);
}
else
{
printf("Process %d - obtained lock region\n", getpid());
}
region_to_lock.l_type = F_UNLCK;//解锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 10;
region_to_lock.l_len = 5;
printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - fail to unlock region\n", getpid());
}
else
{
printf("Process %d - unlocked region\n", getpid());
}
region_to_lock.l_type = F_UNLCK;//解锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 0;
region_to_lock.l_len = 50;
printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - fail to unlock region\n", getpid());
}
else
{
printf("Process %d - unlocked region\n", getpid());
}
region_to_lock.l_type = F_WRLCK;//独占锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 16;
region_to_lock.l_len = 5;
printf("Process %d, trying F_WRLCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - fail to lock region\n", getpid());
}
else
{
printf("Process %d - locked region\n", getpid());
}
region_to_lock.l_type = F_RDLCK;//共享锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 40;
region_to_lock.l_len = 10;
printf("Process %d, trying F_RDLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - fail to lock region\n", getpid());
}
else
{
printf("Process %d - locked region\n", getpid());
}
region_to_lock.l_type = F_WRLCK;//独占锁
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 16;
region_to_lock.l_len = 5;
printf("Process %d, trying F_WELCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
res = fcntl(file_desc, F_SETLK, ®ion_to_lock);
if(res == -1)
{
fprintf(stderr, "Process %d - fail to lock region\n", getpid());
}
else
{
printf("Process %d - locked region\n", getpid());
}
printf("Process %d ending\n", getpid());
close(file_desc);
exit(EXIT_SUCCESS);
}