Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1177212
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 16:23:29

flock.c文件

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>

  7. //设置读锁
  8. #define read_lock(fd, offset, whence, len) \
  9.                 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
  10. //设置读锁阻塞
  11. #define readw_lock(fd, offset, whence, len) \
  12.                 lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
  13. //设置写锁
  14. #define write_lock(fd, offset, whence, len) \
  15.                 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
  16. //设置写锁阻塞
  17. #define writew_lock(fd, offset, whence, len) \
  18.                 lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
  19. //解锁
  20. #define un_lock(fd, offset, whence, len) \
  21.                 lock_reg((fd), F_SETLKW, F_UNLCK, (offset), (whence), (len))



  22. //测试能否新建读锁
  23. #define is_read_lockable(fd, offset, whence, len) \
  24.                 (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
  25. //测试能否新建写锁
  26. #define is_write_lockable(fd, offset, whence, len) \
  27.                 (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)



  28. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len);
  29. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len);

  30. int main(int argc, char * * argv)
  31. {
  32.         int ret;
  33.         int fd;
  34.         fd = open("/home/eas/src/API_fun/Trans_910100.xml", O_RDWR, 777);
  35.         if(fd < 0)
  36.         {
  37.                 printf("open err!\n");
  38.                 return -1;
  39.         }
  40.         /*
  41.         如果另外有一个进程对这个文件设置了写锁,表明有进程正在写文件。
  42.         下面的write也是可以写数据进文件的,
  43.         这样就违背了单独写一个文件的规则,不是我们想要的。如果要确保写文件的时候只有一个进程单独在写,
  44.         约定所有进程在写文件之前也尝试设置一把写锁(查看现在能否写该文件)
  45.         如果写锁设置成功,则表示没有其他进程在写该文件,本进程可以写了,
  46.         如果写锁设置失败,则表示有其他进程在写该文件,本进程不必执行write了,非要执行也可以写数据进文件。
  47.         */
  48.         ret = write(fd, "wangxc", strlen("wangxc"));
  49.         if(ret > 0)
  50.         {
  51.                 printf("成功写[%d]个字节数据到文件[/home/eas/src/API_fun/Trans_910100.xml]\n", ret);
  52.         }
  53.         /*
  54.         判断fd文件能否建立一把独占性写锁:如果排斥锁存在:则把现在已有锁的信息写到flockptr指向的结构中去。
  55.         如果排斥锁不存在:则将l_type设置为F_UNLCK。
  56.         只说明:指定的锁不被现有的锁排斥,并不是说该区域没有锁,该区域可能有锁但不排斥新锁,也可能没有锁;
  57.         */
  58.         if(is_read_lockable(fd, 0, SEEK_SET, 0))
  59.         {
  60.                 printf("进程[%d],能新建读锁!\n", getpid());
  61.         }
  62.         else
  63.         {
  64.                 printf("进程[%d],不能新建读锁!\n", getpid());
  65.         }
  66.         
  67.         //设置读锁
  68.         ret = read_lock(fd, 0, SEEK_SET, 0);
  69.         printf("ret = [%d]\n", ret);
  70.         if(ret == 0)
  71.         {
  72.                 printf("进程[%d],对文件Trans_910100.xml,设置读锁成功!\n", getpid());
  73.         }
  74.         else
  75.         {
  76.                 printf("read_lock err!\n");
  77.         }
  78.         
  79.         //while(1);
  80.         
  81.         return 0;
  82. }



  83. /*

  84. */
  85. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  86. {
  87.         struct flock lock;
  88.         
  89.         lock.l_type = type;
  90.         lock.l_start = offset;
  91.         lock.l_whence = whence;
  92.         lock.l_len = len;
  93.         
  94.         return(fcntl(fd, cmd, &lock));
  95. }

  96. //测试锁状态
  97. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
  98. {
  99.         struct flock lock;
  100.         
  101.         lock.l_type = type;
  102.         lock.l_start = offset;
  103.         lock.l_whence = whence;
  104.         lock.l_len = len;
  105.         
  106.         int ret;
  107.         ret = fcntl(fd, F_GETLK, &lock);
  108.         if(ret < 0)
  109.         {
  110.                 printf("fcntl F_GETLK err!\n");
  111.         }
  112.         
  113.         if(lock.l_type == F_UNLCK)
  114.         {
  115.                 printf("排斥锁不存在:则将l_type设置为F_UNLCK!该区域可能有锁但不排斥新锁,也可能没有锁!\n");
  116.                 return 0;
  117.         }
  118.                 
  119.         return(lock.l_pid);
  120. }
flock2.c文件

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>

  7. //设置读锁
  8. #define read_lock(fd, offset, whence, len) \
  9.                 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
  10. //设置读锁阻塞
  11. #define readw_lock(fd, offset, whence, len) \
  12.                 lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
  13. //设置写锁
  14. #define write_lock(fd, offset, whence, len) \
  15.                 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
  16. //设置写锁阻塞
  17. #define writew_lock(fd, offset, whence, len) \
  18.                 lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
  19. //解锁
  20. #define un_lock(fd, offset, whence, len) \
  21.                 lock_reg((fd), F_SETLKW, F_UNLCK, (offset), (whence), (len))

  22. //测试能否新建读锁
  23. #define is_read_lockable(fd, offset, whence, len) \
  24.                 (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
  25. //测试能否新建写锁
  26. #define is_write_lockable(fd, offset, whence, len) \
  27.                 (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)



  28. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len);
  29. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len);

  30. int main(int argc, char * * argv)
  31. {
  32.         int ret;
  33.         int fd;
  34.         fd = open("/home/eas/src/API_fun/Trans_910100.xml", O_RDWR, 777);
  35.         if(fd < 0)
  36.         {
  37.                 printf("open err!\n");
  38.                 return -1;
  39.         }
  40.         //write(fd, "wangxc", strlen("wangxc"));
  41.         //测试能否加写锁
  42.         if(is_write_lockable(fd, 0, SEEK_SET, 0))
  43.         {
  44.                 printf("进程[%d],能新建写锁!\n", getpid());
  45.         }
  46.         else
  47.         {
  48.                 printf("进程[%d],不能新建写锁!\n", getpid());
  49.         }
  50.         
  51.         //设置写锁
  52.         printf("设置写锁!\n");
  53.         ret = write_lock(fd, 0, SEEK_SET, 0);
  54.         printf("ret = [%d]\n", ret);
  55.         if(ret == 0)
  56.         {
  57.                 printf("进程[%d],对文件Trans_910100.xml,设置写锁成功!\n", getpid());
  58.         }
  59.         else
  60.         {
  61.                 printf("write_lock err!\n");
  62.         }
  63.         
  64.         while(1);
  65.         
  66.         int i = 0;
  67.         while(i < 20)
  68.         {
  69.                 printf("sleep(1)!\n");
  70.                 sleep(1);
  71.                 i++;
  72.         }
  73.         
  74.         return 0;
  75. }



  76. /*

  77. */
  78. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  79. {
  80.         struct flock lock;
  81.         
  82.         lock.l_type = type;
  83.         lock.l_start = offset;
  84.         lock.l_whence = whence;
  85.         lock.l_len = len;
  86.         
  87.         return(fcntl(fd, cmd, &lock));
  88. }

  89. //测试锁状态
  90. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
  91. {
  92.         struct flock lock;
  93.         
  94.         lock.l_type = type;
  95.         lock.l_start = offset;
  96.         lock.l_whence = whence;
  97.         lock.l_len = len;
  98.         
  99.         int ret;
  100.         ret = fcntl(fd, F_GETLK, &lock);
  101.         if(ret < 0)
  102.         {
  103.                 printf("fcntl F_GETLK err!\n");
  104.         }
  105.         
  106.         if(lock.l_type == F_UNLCK)
  107.         {
  108.                 printf("排斥锁不存在:则将l_type设置为F_UNLCK!该区域可能有锁但不排斥新锁,也可能没有锁!\n");
  109.                 return 0;
  110.         }
  111.                 
  112.         return(lock.l_pid);
  113. }
flock3.c文件

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>

  7. //设置读锁
  8. #define read_lock(fd, offset, whence, len) \
  9.                 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
  10. //设置读锁阻塞
  11. #define readw_lock(fd, offset, whence, len) \
  12.                 lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
  13. //设置写锁
  14. #define write_lock(fd, offset, whence, len) \
  15.                 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
  16. //设置写锁阻塞
  17. #define writew_lock(fd, offset, whence, len) \
  18.                 lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
  19. //解锁
  20. #define un_lock(fd, offset, whence, len) \
  21.                 lock_reg((fd), F_SETLKW, F_UNLCK, (offset), (whence), (len))

  22. //测试能否新建读锁
  23. #define is_read_lockable(fd, offset, whence, len) \
  24.                 (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
  25. //测试能否新建写锁
  26. #define is_write_lockable(fd, offset, whence, len) \
  27.                 (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)

  28. int lockfile(int fd);

  29. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len);
  30. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len);

  31. int main(int argc, char * * argv)
  32. {
  33.         int ret;
  34.         int fd;
  35.         fd = open("/home/eas/src/API_fun/Trans_910100.xml", O_RDWR, 777);
  36.         if(fd < 0)
  37.         {
  38.                 printf("open err!\n");
  39.                 return -1;
  40.         }
  41.         //write(fd, "wangxc", strlen("wangxc"));
  42.         //测试能否加写锁
  43.         if(is_write_lockable(fd, 0, SEEK_SET, 0))
  44.         {
  45.                 printf("进程[%d],能新建写锁!\n", getpid());
  46.         }
  47.         else
  48.         {
  49.                 printf("进程[%d],不能新建写锁!\n", getpid());
  50.         }
  51.         
  52.         //设置写锁阻塞
  53.         printf("设置写锁阻塞!\n");
  54.         ret = writew_lock(fd, 0, SEEK_SET, 0);
  55.         printf("ret = [%d]\n", ret);
  56.         if(ret == 0)
  57.         {
  58.                 printf("进程[%d],对文件Trans_910100.xml,设置写锁成功!\n", getpid());
  59.         }
  60.         else
  61.         {
  62.                 printf("writew_lock err!\n");
  63.         }
  64.         
  65.         while(1);
  66.         
  67.         return 0;
  68. }

  69. /**
  70. 功能:给文件整体上加一把写锁
  71. 等同于:#define lockfile(fd) writew_lock((fd), 0, SEEK_SET, 0)
  72. **/
  73. int lockfile(int fd)
  74. {
  75.         struct flock lock;
  76.         
  77.         lock.l_type = F_WRLCK;
  78.         lock.l_start = 0;
  79.         lock.l_whence = SEEK_SET;
  80.         lock.l_len = 0;
  81.         
  82.         return(fcntl(fd, F_SETLK, &lock));
  83. }



  84. /*

  85. */
  86. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  87. {
  88.         struct flock lock;
  89.         
  90.         lock.l_type = type;
  91.         lock.l_start = offset;
  92.         lock.l_whence = whence;
  93.         lock.l_len = len;
  94.         
  95.         return(fcntl(fd, cmd, &lock));
  96. }

  97. //测试锁状态
  98. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
  99. {
  100.         struct flock lock;
  101.         
  102.         lock.l_type = type;
  103.         lock.l_start = offset;
  104.         lock.l_whence = whence;
  105.         lock.l_len = len;
  106.         
  107.         int ret;
  108.         ret = fcntl(fd, F_GETLK, &lock);
  109.         if(ret < 0)
  110.         {
  111.                 printf("fcntl F_GETLK err!\n");
  112.         }
  113.         
  114.         if(lock.l_type == F_UNLCK)
  115.         {
  116.                 printf("排斥锁不存在:则将l_type设置为F_UNLCK!该区域可能有锁但不排斥新锁,也可能没有锁!\n");
  117.                 return 0;
  118.         }
  119.                 
  120.         return(lock.l_pid);
  121. }
flock4.c文件

点击(此处)折叠或打开

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

  8. int SetEnforceLock(char * file);
  9. int lockfile(int fd);

  10. int main(int argc, char * * argv)
  11. {
  12.         char filename[128];
  13.         int fd;
  14.         
  15.         memset(filename, 0, sizeof(filename));
  16.         strcpy(filename, "/home/eas/src/API_fun/Trans_910100.xml");
  17.         
  18.         SetEnforceLock(filename);
  19.         
  20.         fd = open(filename, O_RDWR, 0777);
  21.         if(fd < 0)
  22.         {
  23.                 printf("open errr!\n");
  24.                 exit(0);
  25.         }
  26.         lockfile(fd);
  27.         
  28.         printf("errno = [%d]\n", errno);
  29.         
  30.         while(1);
  31.         
  32.         
  33.         return 0;
  34. }


  35. /*
  36. 对指定的文件,开启强制性锁机制
  37. */
  38. int SetEnforceLock(char * file)
  39. {
  40.         int ret = -1;
  41.         struct stat statbuf;
  42.         
  43.         ret = stat(file, &statbuf);
  44.         if(ret < 0)
  45.         {
  46.                 printf("stat err!\n");
  47.                 return -1;
  48.         }
  49.         
  50.         ret = chmod(file, (statbuf.st_mode & ~S_IXGRP) | S_ISGID);
  51.         if(ret < 0)
  52.         {
  53.                 printf("chmod err!\n");
  54.                 return -1;
  55.         }
  56.         
  57.         return 0;
  58. }


  59. /**
  60. 功能:给文件整体上加锁
  61. 等同于:#define lockfile(fd) writew_lock((fd), 0, SEEK_SET, 0)
  62. **/
  63. int lockfile(int fd)
  64. {
  65.         struct flock lock;
  66.         
  67.         lock.l_type = F_WRLCK;
  68.         lock.l_start = 0;
  69.         lock.l_whence = SEEK_SET;
  70.         lock.l_len = 0;
  71.         
  72.         return(fcntl(fd, F_SETLK, &lock));
  73. }
flock5.c

点击(此处)折叠或打开

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

  8. int SetEnforceLock(char * file);
  9. int lockfile(int fd);

  10. int main(int argc, char * * argv)
  11. {
  12.         char filename[128];
  13.         int fd;
  14.         
  15.         memset(filename, 0, sizeof(filename));
  16.         strcpy(filename, "/home/eas/src/API_fun/Trans_910100.xml");
  17.         
  18.         /*lock4 程序启动后,下面的open还是能打开文件的,因为:本系统没有提供强制性记录锁*/
  19.         fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0777);
  20.         if(fd < 0)
  21.         {
  22.                 printf("open errr!\n");
  23.                 exit(0);
  24.         }
  25.         printf("errno = [%d]\n", errno);
  26.         
  27.         
  28.         return 0;
  29. }


  30. /*
  31. 对指定的文件,开启强制性锁机制
  32. */
  33. int SetEnforceLock(char * file)
  34. {
  35.         int ret = -1;
  36.         struct stat statbuf;
  37.         
  38.         ret = stat(file, &statbuf);
  39.         if(ret < 0)
  40.         {
  41.                 printf("stat err!\n");
  42.                 return -1;
  43.         }
  44.         
  45.         ret = chmod(file, (statbuf.st_mode & ~S_IXGRP) | S_ISGID);
  46.         if(ret < 0)
  47.         {
  48.                 printf("chmod err!\n");
  49.                 return -1;
  50.         }
  51.         
  52.         return 0;
  53. }


  54. /**
  55. 功能:给文件整体上加锁
  56. 等同于:#define lockfile(fd) writew_lock((fd), 0, SEEK_SET, 0)
  57. **/
  58. int lockfile(int fd)
  59. {
  60.         struct flock lock;
  61.         
  62.         lock.l_type = F_WRLCK;
  63.         lock.l_start = 0;
  64.         lock.l_whence = SEEK_SET;
  65.         lock.l_len = 0;
  66.         
  67.         return(fcntl(fd, F_SETLK, &lock));
  68. }
flock6.c

点击(此处)折叠或打开

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

  9. //设置读锁
  10. #define read_lock(fd, offset, whence, len) \
  11.                 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
  12. //设置读锁阻塞
  13. #define readw_lock(fd, offset, whence, len) \
  14.                 lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
  15. //设置写锁
  16. #define write_lock(fd, offset, whence, len) \
  17.                 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
  18. //设置写锁阻塞
  19. #define writew_lock(fd, offset, whence, len) \
  20.                 lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
  21. //解锁
  22. #define un_lock(fd, offset, whence, len) \
  23.                 lock_reg((fd), F_SETLKW, F_UNLCK, (offset), (whence), (len))

  24. //测试能否新建读锁
  25. #define is_read_lockable(fd, offset, whence, len) \
  26.                 (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
  27. //测试能否新建写锁
  28. #define is_write_lockable(fd, offset, whence, len) \
  29.                 (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)

  30. int lockfile(int fd);

  31. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len);
  32. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len);

  33. int SetEnforceLock(char * file);
  34. int SetEnforceLock2(int fd);

  35. void set_fl(int fd, int flags);
  36. void clr_fl(int fd, int flags);


  37. int main(int argc, char * * argv)
  38. {
  39.         char filename[128];
  40.         char buf[128];
  41.         int fd;
  42.         int ret;
  43.         pid_t pid;
  44.         
  45.         memset(filename, 0, sizeof(filename));
  46.         strcpy(filename, "/home/eas/src/API_fun/Trans_910100.xml");
  47.         
  48.         fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0777);
  49.         if(fd < 0)
  50.         {
  51.                 printf("open errr!\n");
  52.                 exit(0);
  53.         }
  54.         printf("fd = [%d]\n", fd);
  55.         
  56.         char wbuf[1024];
  57.         memset(wbuf, 0, sizeof(wbuf));
  58.         strcpy(wbuf, "wangxc");
  59.         
  60.         write(fd, wbuf, strlen(wbuf));
  61.         printf("write buf = [%s]\n", wbuf);
  62.         
  63.         ret = SetEnforceLock2(fd);
  64.         if(ret < 0)
  65.         {
  66.                 printf("SetEnforceLock2 err!\n");
  67.                 exit(-1);
  68.         }
  69.         
  70.         pid = fork();
  71.         if(pid < 0)
  72.         {
  73.                 printf("fork err!\n");
  74.                 exit(-1);
  75.         }
  76.         else if(pid > 0)
  77.         {
  78.                 printf("这里是父进程:\n");
  79.     
  80.                 //ret = lockfile(fd); //对整个文件设置一把写锁
  81.                 ret = write_lock(fd, 0, SEEK_SET, 0);
  82.                 if(ret < 0)
  83.                 {
  84.                         printf("write_lock err!\n");
  85.                         exit(-1);
  86.                 }
  87.                 
  88.                 if(waitpid(pid, NULL, 0) < 0) //等待子进程结束
  89.                 {
  90.                         printf("waitpid err!\n");
  91.                         exit(-1);
  92.                 }
  93.                 printf("父进程结束!\n");

  94.                 exit(0);
  95.         }
  96.         else if(pid == 0)
  97.         {
  98.                 sleep(3);
  99.                 printf("这里是子进程:\n");
  100.                 
  101.                 set_fl(fd, O_NONBLOCK); //对fd设置非阻塞模式
  102.                 
  103.                 /*
  104.                 此时:父进程已经对整个文件设置了一把写锁,子进程企图去对该文件设置一把读锁:会返回出错。
  105.                 ret = read_lock(fd, 0, SEEK_SET, 0);
  106.                 if(ret < 0)
  107.                 {
  108.                         printf("read_lock err!\n");
  109.                         printf("errno = [%d][%s]\n", errno, strerror(errno));
  110.                         exit(-1);
  111.                 }
  112.                 */
  113.                 
  114.                 lseek(fd, 0, SEEK_SET);
  115.                 
  116.                 memset(filename, 0, sizeof(filename));
  117.                 read(fd, buf, sizeof(buf));
  118.                 
  119.                 /*如果buf中有数据:则说明此操作系统是不支持强制性记录锁的
  120.                 如果buf中没有数据:则说明此操作系统是支持强制性记录锁机制的*/
  121.                 printf("read buf = [%s]\n", buf);
  122.                 
  123.                 printf("子进程结束!\n");
  124.     
  125.                 exit(0);
  126.         }
  127. }

  128. /*
  129. 对指定的文件,开启强制性锁机制
  130. */
  131. int SetEnforceLock(char * file)
  132. {
  133.         int ret = -1;
  134.         struct stat statbuf;
  135.         
  136.         ret = stat(file, &statbuf);
  137.         if(ret < 0)
  138.         {
  139.                 printf("stat err!\n");
  140.                 return -1;
  141.         }
  142.         
  143.         ret = chmod(file, (statbuf.st_mode & ~S_IXGRP) | S_ISGID);
  144.         if(ret < 0)
  145.         {
  146.                 printf("chmod err!\n");
  147.                 return -1;
  148.         }
  149.         
  150.         return 0;
  151. }

  152. /*
  153. 对指定的文件,开启强制性锁机制
  154. */
  155. int SetEnforceLock2(int fd)
  156. {
  157.         int ret = -1;
  158.         struct stat statbuf;
  159.         
  160.         ret = fstat(fd, &statbuf);
  161.         if(ret < 0)
  162.         {
  163.                 printf("stat err!\n");
  164.                 return -1;
  165.         }
  166.         
  167.         ret = fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID);
  168.         if(ret < 0)
  169.         {
  170.                 printf("chmod err!\n");
  171.                 return -1;
  172.         }
  173.         
  174.         return 0;
  175. }

  176. /**
  177. 功能:给文件整体上加一把写锁
  178. 等同于:#define lockfile(fd) writew_lock((fd), 0, SEEK_SET, 0)
  179. **/
  180. int lockfile(int fd)
  181. {
  182.         struct flock lock;
  183.         
  184.         lock.l_type = F_WRLCK;
  185.         lock.l_start = 0;
  186.         lock.l_whence = SEEK_SET;
  187.         lock.l_len = 0;
  188.         
  189.         return(fcntl(fd, F_SETLK, &lock));
  190. }

  191. /*

  192. */
  193. int    lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  194. {
  195.         struct flock lock;
  196.         
  197.         lock.l_type = type;
  198.         lock.l_start = offset;
  199.         lock.l_whence = whence;
  200.         lock.l_len = len;
  201.         
  202.         return(fcntl(fd, cmd, &lock));
  203. }

  204. //测试锁状态
  205. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
  206. {
  207.         struct flock lock;
  208.         
  209.         lock.l_type = type;
  210.         lock.l_start = offset;
  211.         lock.l_whence = whence;
  212.         lock.l_len = len;
  213.         
  214.         int ret;
  215.         ret = fcntl(fd, F_GETLK, &lock);
  216.         if(ret < 0)
  217.         {
  218.                 printf("fcntl F_GETLK err!\n");
  219.         }
  220.         
  221.         if(lock.l_type == F_UNLCK)
  222.         {
  223.                 printf("排斥锁不存在:则将l_type设置为F_UNLCK!该区域可能有锁但不排斥新锁,也可能没有锁!\n");
  224.                 return 0;
  225.         }
  226.                 
  227.         return(lock.l_pid);
  228. }


  229. /*
  230. 对一个文件描述符关闭一个文件状态标志
  231. 如:clr_fl(STDOUT_FILENO, O_SYNC); 关闭标准输出文件的同步写标志
  232. */
  233. void clr_fl(int fd, int flags)
  234. {
  235.         int val;
  236.         int ret;
  237.         //val = fcntl(fd, F_GETFL, 0);
  238.         val = fcntl(fd, F_GETFL);
  239.         if(val < 0)
  240.         {
  241.                 printf("fcntl F_GETFL err!\n");    
  242.                 return;
  243.         }
  244.     val &= ~flags; /* turn flags off */

  245.         ret = fcntl(fd, F_SETFL, val);
  246.         if(val < 0)
  247.         {
  248.                 printf("fcntl F_SETFL err!\n");    
  249.                 return;
  250.         }
  251.     return;
  252. }



  253. /*
  254. 对一个文件描述符打开一个文件状态标志
  255. 如:set_fl(STDOUT_FILENO, O_SYNC); 打开标准输出文件的同步写标志
  256. */
  257. void set_fl(int fd, int flags)
  258. {
  259.         int val;
  260.         int ret;
  261.         //val = fcntl(fd, F_GETFL, 0);
  262.         val = fcntl(fd, F_GETFL);
  263.         if(val < 0)
  264.         {
  265.                 printf("fcntl F_GETFL err!\n");    
  266.                 return;
  267.         }
  268.         val |= flags;
  269.         
  270.         ret = fcntl(fd, F_SETFL, val);
  271.         if(val < 0)
  272.         {
  273.                 printf("fcntl F_SETFL err!\n");    
  274.                 return;
  275.         }
  276.         return;
  277. }

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

上一篇:fflush函数

下一篇:fork,vfork函数

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