实验 5.2.8 解锁/锁定文件
1. 实现对指定文件的锁定和解锁。
1. 实现对指定文件的锁定和解锁。
源代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd,i;
char path[]="/root/txt1.txt";
fd=open(path,O_WRONLY|O_CREAT);
if(fd!=-1)
{
printf("opened file %s .\n",path);
if(flock(fd,LOCK_EX)==0)
{
printf("the file was locked.\n");
}
else
{
printf("the file was not locked.\n");
}
if(flock(fd,LOCK_UN)==0)
{
printf("the file was unlocked.\n");
}
else
{
printf("the file was not unlocked.\n");
}
close(fd);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
}
实验 5.2.8 解锁/锁定文件 1. 实现对指定文件的锁定和解锁。 1. 实现对指定文件的锁定和解锁。
最新推荐文章于 2021-04-28 16:08:49 发布