本次实验是实现文件锁。
废话不多说,上代码。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
int main()
{
int fd;
fd = open("test2.txt",O_CREAT |O_RDWR | O_TRUNC,0644);
if(fd == -1)
{
ERR_EXIT("open error");
}
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if(fcntl(fd,F_SETLK,&lock) == 0)
{
printf("lock success\n");
printf("press any key to unlock\n");
getchar();
lock.l_type = F_UNLCK;
if(fcntl(fd,F_SETLK,&lock) == 0)
{
printf("unlock success\n");
}
else
{
ERR_EXIT("unlock fail");
}
}
else
{
ERR_EXIT("lock fail");
}
return 0;
}