怎样实现一个文件互斥的访问程序?
如题,我写的程序如下:
#include <fcntl.h>
#include <unistd.h>
struct flock* file_lock(short type, short whence)
{
static struct flock ret ;
ret.l_type = type ;
ret.l_start = 0 ;
ret.l_whence = whence ;
ret.l_len = 0 ;
ret.l_pid = getpid() ;
return &ret ;
}
int main(){
int fd=open("/home/liougehooa/lo.txt",O_WRONLY|O_CREAT|O_EXCL|O_TRUNC,00777);
long i=fcntl(fd,F_SETLKW,file_lock(F_WRLCK,SEEK_SET));
while(1);
return i;
}
然后编译并后台运行,同时rm lo.txt,lo.txt还是被删掉了。