Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57430
  • 博文数量: 8
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 88
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-13 16:18
个人简介

不积跬步无以至千里,不积小流无以成江河。

文章分类
文章存档

2014年(8)

我的朋友

分类: 其他UNIX

2014-11-17 14:28:17

1.用F_GETLK调用fcntl,当调用进程是持有记录锁的进程时,F_GETLK命令不会报告调用进程自己持有的锁
2.多次对文件加锁,那么按照加锁的最大范围计算,比如先加锁全部文件,再加锁一个文件的0-2字节,那么加锁的范围是全部文件。

进程1加记录锁
1>锁住1,2,3字节
2>锁住全部字节
进程2用F_GETLK获取记录锁情况(只锁定1>情况下):
iret=0
+========child=======+
0-1 byte is unlock.
2-1 byte is WR lock.
2-2 byte is WR lock.
2-3 byte is WR lock.
3-1 byte is WR lock.
3-2 byte is WR lock.
4-1 byte is unlock.
5-1 byte is unlock.
6-1 byte is unlock.
7-1 byte is unlock.
8-1 byte is unlock.
9-1 byte is unlock.
10-1 byte is unlock.
+======parent========+
0-1 byte is unlock.
2-1 byte is unlock.
2-2 byte is unlock.
2-3 byte is unlock.
2-4 byte is unlock.
3-2 byte is unlock.
6-1 byte is unlock.

进程2用F_GETLK获取记录锁情况(1>,2>都执行情况下):
iret=0
+========child=======+
0-1 byte is WR lock.
2-1 byte is WR lock.
2-2 byte is WR lock.
2-3 byte is WR lock.
3-1 byte is WR lock.
3-2 byte is WR lock.
4-1 byte is WR lock.
5-1 byte is WR lock.
6-1 byte is WR lock.
7-1 byte is WR lock.
8-1 byte is WR lock.
9-1 byte is WR lock.
10-1 byte is WR lock.
+======parent========+
0-1 byte is unlock.
2-1 byte is unlock.
2-2 byte is unlock.
2-3 byte is unlock.
2-4 byte is unlock.
3-2 byte is unlock.
6-1 byte is unlock.


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <errno.h>
  7. #include <sys/wait.h>
  8. #include <stdlib.h>


  9. #define lockfullwrfile(fd) (lockfile(fd,F_WRLCK,0,0,SEEK_SET))
  10. #define lockapartofwrfile(fd,start,len) (lockfile(fd,F_WRLCK,start,len,SEEK_SET))

  11. int lockfile(int fd, int type , off_t start , off_t len , int whence) {
  12.     
  13.     struct flock l;
  14.     
  15.     l.l_pid = getpid();
  16.     l.l_start = start;
  17.     l.l_type = type;
  18.     l.l_whence = whence;
  19.     l.l_len = len;
  20.     
  21.     return fcntl(fd, F_SETLK,&l);
  22. }
  23. #define unlockfullfile(fd) (unlockfile(fd,F_WRLCK,0,0,SEEK_SET))
  24. #define unlockapartoffile(fd,start,len) (unlockfile(fd,F_WRLCK,start,len,SEEK_SET))

  25. int unlockfile(int fd, int type , off_t start , off_t len , int whence) {
  26.     
  27.     struct flock l;
  28.     
  29.     l.l_pid = getpid();
  30.     l.l_start = start;
  31.     l.l_type = type;
  32.     l.l_whence = whence;
  33.     l.l_len = len;
  34.     
  35.     return fcntl(fd, F_UNLCK,&l);
  36. }

  37. #define unlockfullfile(fd) (unlockfile(fd,F_WRLCK,0,0,SEEK_SET))
  38. #define unlockapartoffile(fd,start,len) (unlockfile(fd,F_WRLCK,start,len,SEEK_SET))

  39. int getlockfile(int fd, int type , off_t start , off_t len , int whence) {
  40.     
  41.     struct flock l;
  42.     int ret = 0;
  43.     l.l_pid = getpid();
  44.     l.l_start = start;
  45.     l.l_type = type;
  46.     l.l_whence = whence;
  47.     l.l_len = len;
  48.     
  49.     ret = fcntl(fd, F_GETLK,&l);
  50.     if (ret < 0) {
  51.         perror("GETLK error\n");
  52.         return l.l_pid;
  53.     }
  54.     return l.l_type;
  55. }

  56. void printistype(int type,char * region){
  57.     if (!region) {
  58.         printf("region is empty.\n");
  59.         return;
  60.     }
  61.     switch (type) {
  62.         case F_WRLCK:
  63.             printf("%s byte is WR lock.\n",region);
  64.             break;
  65.         case F_RDLCK:
  66.             printf("%s byte is RD lock.\n",region);
  67.             break;
  68.         case F_UNLCK:
  69.             printf("%s byte is unlock.\n",region);
  70.             break;
  71.         default:
  72.             printf("%s byte is error.\n",region);
  73.             break;
  74.     }

  75. }
  76. /**
  77.  * 测试:在同一个进程中对一个文件全体加锁,然后在对一个文件一部分加锁,看看文件哪个锁起作用
  78.  * 结果:对全体加锁起作用,
  79.  */
  80. int main(){
  81.     
  82.     ssize_t ret = 0;
  83.     int iret = 0 ;
  84.     int fd = 0;
  85.     pid_t pid = 0 , pret = 0;
  86.     fd = open("/Users/panghongjiang/lock2file.txt",O_CREAT|O_RDWR, \
  87.               S_IRUSR|S_IWUSR);
  88.     
  89.     if (fd < 0) {
  90.         perror("fd error\n");
  91.         return -1;
  92.     }
  93.     
  94.     ret = write(fd, "abcdefg", 8);
  95.     
  96.     if (ret != 8) {
  97.         perror("write is not full.\n");
  98.         close(fd);
  99.         return -1;
  100.     }
  101.     
  102.     lockapartofwrfile(fd, 1, 3); //1>
  103.     iret = lockfullwrfile(fd); //2>
  104.     printf("iret=%d\n",iret);
  105.     
  106.     if ((pid = fork()) < 0) {
  107.         printf("fork error\n");
  108.     } else if (0 == pid) {
  109.         if ((iret = getlockfile(fd,F_WRLCK,0,1,SEEK_SET) ) < 0)
  110.             printf("getlockfile error\n");
  111.         printistype(iret,"0-1");
  112.         
  113.         if ((iret = getlockfile(fd,F_WRLCK,2,1,SEEK_SET) ) < 0)
  114.             printf("getlockfile error\n");
  115.         printistype(iret,"2-1");
  116.         
  117.         if ((iret = getlockfile(fd,F_WRLCK,2,2,SEEK_SET) ) < 0)
  118.             printf("getlockfile error\n");
  119.         printistype(iret,"2-2");
  120.         
  121.         if ((iret = getlockfile(fd,F_WRLCK,2,3,SEEK_SET) ) < 0)
  122.             printf("getlockfile error\n");
  123.         printistype(iret,"2-3");
  124.         
  125.         
  126.         if ((iret = getlockfile(fd,F_WRLCK,3,1,SEEK_SET) ) < 0)
  127.             printf("getlockfile error\n");
  128.         printistype(iret,"3-1");
  129.         
  130.         
  131.         if ((iret = getlockfile(fd,F_WRLCK,3,2,SEEK_SET) ) < 0)
  132.             printf("getlockfile error\n");
  133.         printistype(iret,"3-2");
  134.         
  135.         
  136.         if ((iret = getlockfile(fd,F_WRLCK,4,1,SEEK_SET) ) < 0)
  137.             printf("getlockfile error\n");
  138.         printistype(iret,"4-1");
  139.         
  140.         if ((iret = getlockfile(fd,F_WRLCK,5,1,SEEK_SET) ) < 0)
  141.             printf("getlockfile error\n");
  142.         printistype(iret,"5-1");
  143.         
  144.         if ((iret = getlockfile(fd,F_WRLCK,6,1,SEEK_SET) ) < 0)
  145.             printf("getlockfile error\n");
  146.         printistype(iret,"6-1");
  147.         
  148.         
  149.         if ((iret = getlockfile(fd,F_WRLCK,7,1,SEEK_SET) ) < 0)
  150.             printf("getlockfile error\n");
  151.         printistype(iret,"7-1");
  152.         
  153.         
  154.         if ((iret = getlockfile(fd,F_WRLCK,8,1,SEEK_SET) ) < 0)
  155.             printf("getlockfile error\n");
  156.         printistype(iret,"8-1");
  157.         
  158.         if ((iret = getlockfile(fd,F_WRLCK,9,1,SEEK_SET) ) < 0)
  159.             printf("getlockfile error\n");
  160.         printistype(iret,"9-1");
  161.         
  162.         if ((iret = getlockfile(fd,F_WRLCK,10,1,SEEK_SET) ) < 0)
  163.             printf("getlockfile error\n");
  164.         printistype(iret,"10-1");
  165.         exit(0);
  166.     }else {
  167.         while ( pid != (pret = waitpid(pid, NULL, 0)));
  168.     }
  169.     
  170.     
  171.     if ((iret = getlockfile(fd,F_WRLCK,0,1,SEEK_SET) ) < 0)
  172.         printf("getlockfile error\n");
  173.     printistype(iret,"0-1");
  174.     
  175.     if ((iret = getlockfile(fd,F_WRLCK,2,1,SEEK_SET) ) < 0)
  176.         printf("getlockfile error\n");
  177.     printistype(iret,"2-1");
  178.     
  179.     if ((iret = getlockfile(fd,F_WRLCK,2,2,SEEK_SET) ) < 0)
  180.         printf("getlockfile error\n");
  181.     printistype(iret,"2-2");
  182.     
  183.     if ((iret = getlockfile(fd,F_WRLCK,2,3,SEEK_SET) ) < 0)
  184.         printf("getlockfile error\n");
  185.     printistype(iret,"2-3");
  186.     
  187.     
  188.     if ((iret = getlockfile(fd,F_WRLCK,2,3,SEEK_SET) ) < 0)
  189.         printf("getlockfile error\n");
  190.     printistype(iret,"2-4");
  191.     
  192.     
  193.     if ((iret = getlockfile(fd,F_WRLCK,3,2,SEEK_SET) ) < 0)
  194.         printf("getlockfile error\n");
  195.     printistype(iret,"3-2");
  196.     
  197.     
  198.     if ((iret = getlockfile(fd,F_WRLCK,6,1,SEEK_SET) ) < 0)
  199.         printf("getlockfile error\n");
  200.     printistype(iret,"6-1");

  201.     close(fd);
  202.     
  203.     return 0;
  204. }

阅读(2799) | 评论(0) | 转发(0) |
0

上一篇:守护进程的创建

下一篇:Xcode删除一行

给主人留下些什么吧!~~