#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
pid_t pid1;
int file1;
char *path1="/home/wy/whatfuck";
pid1=fork();
while(1)
{
file1=open(path1,O_CREAT|O_RDWR|O_EXCL,666);
if(file1==-1)
{
printf("%d - locked!\n",getpid());
sleep(3);
}
else
{
printf("%d - opened!\n",getpid());
sleep(1);
close(file1);
unlink(path1);
sleep(2);
}
}
return 0;
}
使用open方式的文件锁
最新推荐文章于 2022-12-29 17:45:48 发布