免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1599 | 回复: 0

redhat9的linux内核不支持mandatory locking? [复制链接]

论坛徽章:
0
发表于 2009-01-20 12:59 |显示全部楼层
apueP457Determine whether mandatory locking is supported的例子,我测试下来的结果是我的系统不支持,可是书上前面讲linux2.4的内核是支持的
怎么回事呢

附上书中的测试示例
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>


  9. //-----------------------------
  10. static volatile sig_atomic_t sigflag;
  11. static sigset_t newmask, oldmask, zeromask;

  12. static void sig_usr(int signo)
  13. {
  14. while((fd = func(a, b, c)) != NULL)
  15.         sigflag = 1;
  16.         return;
  17. }

  18. void TELL_WAIT(void)
  19. {
  20.         if (signal(SIGUSR1, sig_usr) == SIG_ERR)
  21.         {
  22.                 printf("signal(SIGUSR1) error:%s\n", strerror(errno));
  23.                 return;
  24.         }
  25.         if (signal(SIGUSR2, sig_usr) == SIG_ERR)
  26.         {
  27.                 printf("signal(SIGUSR2) error:%s\n", strerror(errno));
  28.                 return;
  29.         }
  30.        
  31.         sigemptyset(&zeromask);
  32.         sigemptyset(&newmask);
  33.         sigaddset(&newmask, SIGUSR1);
  34.         sigaddset(&newmask, SIGUSR2);
  35.        
  36.         if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
  37.         {
  38.                 printf("SIG_BLOCK error:%s\n", strerror(errno));
  39.                 return;
  40.         }
  41. }

  42. void TELL_PARENT(pid_t pid)
  43. {
  44.         kill(pid, SIGUSR2);
  45. }

  46. void WAIT_PARENT(void)
  47. {
  48.         while (sigflag == 0)
  49.                 sigsuspend(&zeromask);
  50.         sigflag = 0;
  51.        
  52.         if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
  53.         {
  54.                 printf("SIG_SETMASK error:%s\n", strerror(errno));
  55.                 return;
  56.         }
  57.         return;
  58. }

  59. void TELL_CHILD(pid_t pid)
  60. {
  61.         kill(pid, SIGUSR1);
  62. }

  63. void WAIT_CHILD(void)
  64. {
  65.         while (sigflag == 0)
  66.                 sigsuspend(&zeromask);
  67.         sigflag = 0;

  68.         if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
  69.         {
  70.                 printf("SIG_SETMASK error:%s\n", strerror(errno));
  71.                 return;
  72.         }
  73.         return;
  74. }
  75. //---------------------------------------------

  76. static int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  77. {
  78.         struct flock lock;

  79.         lock.l_type = type;
  80.         lock.l_start = offset;
  81.         lock.l_whence = whence;
  82.         lock.l_len = len;

  83.         return (fcntl(fd, cmd, &lock));
  84. }


  85. #define read_lock(fd, offset, whence, len) \
  86.         lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
  87. #define readw_lock(fd, offset, whence, len) \
  88.         lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
  89. #define write_lock(fd, offset, whence, len) \
  90.         lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
  91. #define writew_lock(fd, offset, whence, len) \
  92.         lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
  93. #define un_lock(fd, offset, whence, len) \
  94.         lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
  95. //------------------------------------------
  96. void set_fl(int fd, int setflag)
  97. {
  98.         int flag;
  99.         if ((flag = fcntl(fd, F_GETFL, 0)) == -1)
  100.         {
  101.                 printf("fcntl(F_GETFL) error:%s\n", strerror(errno));
  102.                 return;
  103.         }
  104.         flag |= setflag;
  105.         if (fcntl(fd, F_SETFL, flag) == -1)
  106.         {
  107.                 printf("fcntl(F_SETFL) error:%s\n", strerror(errno));
  108.                 return;
  109.         }
  110.         return;
  111. }
  112. //----------------
  113. int main(int argc, char *argv[])
  114. {
  115.         int fd;
  116.         pid_t pid;
  117.         char buf[5];
  118.         struct stat statbuf;

  119.         if (argc != 2)
  120.         {
  121.                 printf("usage:%s filename\n", argv[0]);
  122.                 exit(0);
  123.         }

  124.         if ((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1)
  125.         {
  126.                 printf("open() error:%s\n", strerror(errno));
  127.                 exit(0);
  128.         }

  129.         if (write(fd, "abcdef", sizeof("abcdef")) != sizeof("abcdef"))
  130.         {
  131.                 printf("write() error:%s\n", strerror(errno));
  132.                 exit(0);
  133.         }

  134.         if (fstat(fd, &statbuf) == -1)
  135.         {
  136.                 printf("fstat() error:%s\n", strerror(errno));
  137.                 exit(0);
  138.         }

  139.         if (fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) == -1)
  140.         {
  141.                 printf("fchmod error:%s\n", strerror(errno));
  142.                 exit(0);
  143.         }

  144.         TELL_WAIT();
  145.        
  146.         if ((pid = fork()) == -1)
  147.         {
  148.                 printf("fork() error:%s\n", strerror(errno));
  149.                 exit(0);
  150.         }
  151.         else if (pid > 0)
  152.         {
  153.                 if (write_lock(fd, 0, SEEK_SET, 0) < 0)
  154.                 {
  155.                         printf("write_lock error:%s\n", strerror(errno));
  156.                         exit(0);
  157.                 }
  158.                 TELL_CHILD(pid);

  159.                 if (waitpid(pid, NULL, 0) == -1)
  160.                 {
  161.                         printf("waitpid error:%s\n", strerror(errno));
  162.                 }
  163.         }
  164.         else
  165.         {
  166.                 WAIT_PARENT();
  167.                 set_fl(fd, O_NONBLOCK);

  168.                 if (read_lock(fd, 0, SEEK_SET, 0) != -1)
  169.                 {
  170.                         printf("child:read_lock() succeeded\n");
  171.                         exit(0);
  172.                 }
  173.                 printf("read_lock of already-locked region returns errno = %d(%s)\n", errno, strerror(errno));
  174.                 if (lseek(fd, 0, SEEK_SET) == -1)
  175.                 {
  176.                         printf("lseek() error:%s\n", strerror(errno));
  177.                         exit(0);
  178.                 }
  179.                 if (read(fd, buf, 2) < 0)
  180.                 {
  181.                         printf("read failed(mandatory locking works)\n");
  182.                         exit(0);
  183.                 }
  184.                 else
  185.                         printf("read ok(no mandatory locking), buf = %2.2s\n", buf);
  186.         }
  187.         exit(0);
  188. }


复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP