//flock文件上锁,是能够让多个进程安全,合理并按预料同时访问同一文件的方法。
//GNU/Linux====p175
#include<unistd.h>
#include<sys/file.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
void setlock(int fd,int type){
struct flock lock;
char msg[80];
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 1;
while(1){
lock.l_type = type;
if((fcntl(fd,F_SETLK,&lock))==0)
return;
fcntl(fd,F_GETLK,&lock);
if(lock.l_type != F_UNLCK){
switch(lock.l_type){
case(F_RDLCK):
sprintf(msg,"read lock already set by %d\n",lock.l_pid);
break;
case(F_WRLCK):
sprintf(msg,"write lock already set by %d\n",lock.l_pid);
break;
}
puts(msg);
getchar();
}
}
}
int main(int argc,char *argv[]){
int fd;
if((fd=open(argv[1],O_RDWR|O_CREAT,0666))<0){
perror("open ");
exit(-1);
}
setlock(fd,F_RDLCK);
printf("PID %d read %s\n",getpid(),argv[1]);
getchar();
setlock(fd,F_UNLCK);
printf("PID %d unlocked %s\n",getpid(),argv[1]);
getchar();
setlock(fd,F_WRLCK);
printf("PID %d write %s\n",getpid(),argv[1]);
getchar();
close(fd);
exit(0);
}