亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1561 | 回復: 2
打印 上一主題 下一主題

[Linux] 跪求指點記錄鎖問題 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 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 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2014-02-18 15:52 |只看該作者
timespace 發(fā)表于 2014-02-18 09:57
回復 1# qq247739187
明白了。謝了。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP