免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1505 | 回复: 2
打印 上一主题 下一主题

[Linux] 跪求指点记录锁问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-17 19:15 |只看该作者 |倒序浏览
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<fcntl.h>
  4. #include<errno.h>

  5. int getlock(struct flock* plock, int type, int whence);


  6. int main(int argc, char* argv[])
  7. {
  8.     int fd = -1;
  9.     if((fd = open("/home/cyc/proc/a.bat", O_RDONLY)) < 0)
  10.     {
  11.         perror("open file failed!!\n");
  12.         return 0;
  13.     }

  14.     struct flock lock1;
  15.     struct flock lock2;
  16.     struct flock lock3;
  17.     getlock(&lock1, F_RDLCK, SEEK_SET);
  18.     if(fcntl(fd, F_SETLK, &lock1) < 0)
  19.     {
  20.         perror("set lock failed!!\n");
  21.         return 0;
  22.     }
  23.     else
  24.     {
  25.         fprintf(stdout, "set lock success!!\n");
  26.     }

  27.     fprintf(stdout, "get lock info\n");
  28.     getlock(&lock2, F_RDLCK, SEEK_SET);
  29.     if(fcntl(fd, F_GETLK, &lock2) < 0)
  30.     {
  31.         fprintf(stderr, "set lock failed!!");
  32.         return 0;
  33.     }
  34.     if(lock2.l_type == F_UNLCK)
  35.     {
  36.         fprintf(stdout, "no lock!!\n");
  37.     }
  38.     else if(lock2.l_type == F_RDLCK)
  39.     {
  40.         fprintf(stdout, "have read lock!!\n");
  41.     }
  42.     else if(lock2.l_type == F_WRLCK)
  43.     {
  44.         fprintf(stdout, "have write lock!!\n");
  45.     }

  46.     getlock(&lock3, F_UNLCK, SEEK_SET);
  47.     if(fcntl(fd, F_SETLK, &lock3) < 0)
  48.     {
  49.         perror("set lock failed!!\n");
  50.         return 0;
  51.     }

  52.     return 0;
  53. }

  54. int getlock(struct flock* plock, int type, int whence)
  55. {
  56.     if(plock == NULL)
  57.     {
  58.         perror("flock pointer is NULL\n");
  59.         return -1;
  60.     }

  61.     plock->l_type   = type;
  62.     plock->l_whence = whence;
  63.     plock->l_start  = 0;
  64.     plock->l_len    = 0;
  65.     plock->l_pid    = getpid();
  66.      
  67.     return 0;
  68. }
复制代码
在加共享锁成功后,我再读取fd对应的锁信息,结果是:
读不到,说没有锁信息:

  1. set lock success!!
  2. get lock info
  3. no lock!!
复制代码
加了锁不是可以获取的到么,为什么获取不到呢。在线等待。

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2014-02-18 09:57 |只看该作者
回复 1# qq247739187
      F_GETLK (struct flock *)
              On  input to this call, lock describes a lock we would like to place on the file.  If the lock could be
              placed, fcntl() does not actually place it, but returns F_UNLCK in the l_type field of lock and  leaves
              the other fields of the structure unchanged.  If one or more incompatible locks would prevent this lock
              being placed, then fcntl() returns details about one of these locks in the l_type,  l_whence,  l_start,
              and l_len fields of lock and sets l_pid to be the PID of the process holding that lock.


看下Linux man的解释,更多示例参考UNPv2
1. 只要当前进程可以加锁(读或写),就返回F_UNLCK
2. 还有很重要的一点,记录锁是基于进程的,同一个进程可以任意加锁或解锁,没有任何限制
上面两个属性,就可以解释你的输出,测试记录锁,起码应该是两个进程之间的事情,同一个进程没有意义。


   

论坛徽章:
0
3 [报告]
发表于 2014-02-18 15:52 |只看该作者
timespace 发表于 2014-02-18 09:57
回复 1# qq247739187
明白了。谢了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP