Chinaunix

标题: Linux文件锁无卵用? [打印本页]

作者: 无形装逼    时间: 2016-05-29 12:25
标题: Linux文件锁无卵用?
在Linux下写了一段小代码测试一下文件锁,创建文件并写入文件锁(强制锁,写入锁),结果无论怎么测试其它线程和自身都可以随意写入数据,文件锁丝毫不起作用,将代码贴出来,各位前辈看看什么情况。
  1. /*file_lock_set.c*/
  2. int lock_set(int fd,int type)
  3. {
  4.     struct flock old_lock,lock;
  5.     lock.l_whence=SEEK_SET;
  6.     lock.l_start=0;
  7.     lock.l_len=25;
  8.     lock.l_type=type;
  9.     lock.l_pid=-1;

  10. /*judge whether the file has been locked*/
  11.     fcntl(fd,F_GETLK,&lock);
  12.         if (lock.l_type!=F_UNLCK)/*there is already a lock*/
  13.         {
  14.             if(lock.l_type==F_RDLCK)
  15.                 printf("read lock is already setted by %d",lock.l_pid);       
  16.                 else if(lock.l_type==F_WRLCK)
  17.                     printf("read lock is already setted by %d",lock.l_pid);       
  18.         }
  19.     lock.l_type=type;/*l_type may be changed by F_GETLK*/
  20.    
  21.     if(fcntl(fd,F_SETLKW,&lock)<0)
  22.         {
  23.             printf("lock failed:type = %d\n",lock.l_type);
  24.         }        
  25.            
  26.     else if(fcntl(fd,F_SETLKW,&lock)==0)
  27.     {
  28.         switch(lock.l_type)       
  29.         {
  30.             case F_RDLCK:
  31.                 printf("read lock set by %d\n",getpid());break;
  32.             case F_WRLCK:
  33.                 printf("write lock set by %d\n",getpid());break;
  34.             case F_UNLCK:
  35.                 printf("release lock by %d\n",getpid());       
  36.         }           
  37.     }       
  38. }           
  39.            
  40.            
  41. #include<sys/types.h>
  42. #include<unistd.h>
  43. #include<fcntl.h>
  44. #include<stdio.h>
  45. #include<string.h>
  46. #include<stdlib.h>
  47. #include<signal.h>
  48. #include<pthread.h>
  49. #include<semaphore.h>
  50. #include"file_lock_set.c"

  51. sem_t mywrite,mylock;
  52. pthread_t thread[2];


  53. /*func******************************************************************/
  54. void func(void)
  55. {   
  56.     int fd;
  57.     printf("thread1 is going to open the locked file\n");
  58.     fd=open("./datafile",O_RDWR);
  59.     if (fd<0)
  60.         perror("open datafile fail");
  61.     if(fd>0)
  62.     {
  63.         while(1)
  64.             {
  65.                 if(write(fd,"thread1 has been here \n",22)>0)
  66.                     printf("thread1 write data sucessfully \n");
  67.                 else if(write(fd,"thread1 has been here \n",22)<=0)
  68.                     printf("thread1 failed to write data \n");
  69.                 sleep(1);
  70.          }
  71.     }        
  72. }

  73. /*locker***************************************************************/
  74. void* locker(void* arg)
  75. {
  76.     int fd;
  77.     sem_wait(&mylock);
  78.     printf("child1 is creating a datafile\n");
  79.     fd=open("./datafile",O_RDWR|O_CREAT,0666);/*创建数据文件*/
  80.     if(fd<0)
  81.         perror("creat datafile fail");
  82.     lock_set(fd,F_WRLCK);/*文件上锁*/
  83.     if(write(fd,"thread0 has been here \n",22)>0)
  84.                     printf("thread0 write data sucessfully \n");
  85.                 else if(write(fd,"thread1 has been here \n",22)<=0)
  86.                     printf("thread2 failed to write data \n");
  87.     sem_post(&mywrite);
  88.     sleep(3);
  89.     lock_set(fd,F_UNLCK);
  90.     sleep(2);
  91.     pthread_cancel(thread[1]);
  92. }
  93. /*writer***************************************************************/
  94. void* writer(void* arg)
  95. {
  96.     sem_wait(&mywrite);
  97.     func();       
  98. }

  99. /*main****************************************************************/
  100. int main (int argc,char* argv[])
  101. {
  102.     int ret;
  103.    
  104. /*创建两个线程*/  
  105.     ret=sem_init(&mylock,0,1);
  106.     ret+=sem_init(&mywrite,0,0);
  107.     if(ret!=0)
  108.         printf("At least one sem init failed");
  109.     ret=pthread_create(&thread[0],NULL,locker,NULL);
  110.     ret+=pthread_create(&thread[1],NULL,writer,NULL);
  111.     if(ret!=0)
  112.         printf("At least one thread created failed");
  113.     pthread_join(thread[0],NULL);
  114.     pthread_join(thread[1],NULL);
  115.     return 0;
  116.     exit(0);
  117. }

复制代码
执行结果在附件的图片里,锁完全没起作用啊

结果结果.png (34.46 KB, 下载次数: 81)

结果结果.png

作者: hellioncu    时间: 2016-05-30 11:35
一般只是advisory锁吧
作者: yaoyefengyun    时间: 2016-05-31 22:55
本帖最后由 yaoyefengyun 于 2016-05-31 22:55 编辑

这只是建议锁并不是强制锁,也就是说,你每次操作之前,应该先获取一次锁,如果得到锁,即进行操作,否则不操作。强制锁我好像在哪儿看到过,但并没有看内容,你可以网上查查,一般场景建议锁也够用了。
作者: 流氓无产者    时间: 2016-06-01 09:45
无形装逼 发表于 2016-05-29 12:25
在Linux下写了一段小代码测试一下文件锁,创建文件并写入文件锁(强制锁,写入锁),结果无论怎么测试其它线 ...

大家都要加锁才有效果啊
可以封装成一个库供使用
但往往出现有不加锁的害群之马
所以,还是希望有内核自动加锁,不知道现在有没有了
作者: lolizeppelin    时间: 2016-06-01 10:31
强制锁要挂载的时候加特别参数才生效吧

只用过协同锁 强制锁没用过




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2