强制性锁对open的影响
不知者不罪 2017-10-20 11:00:02 在《unix高级环境编程》书 14.3 6讲强制性记录锁中提到:
“....... 但是,如果欲打开的文件具有强制性记录锁(读锁或写锁),而且open调用中的flag指定为O_TRUNC或O_CREAT,则不论是否 指定O_NONBLOCK,open都立即出错返回,errno设置为EAGAIN。”[/color]
问题:
但是在书中给的 测试是否支持强制性锁的例程中: 使用了open,并且指定了O_CREAT 与 O_TRUNC,书上的运行结果,在支持强制性锁的Solaris 10上运行却没有在open出打印出错误,这是为什么呢?
#include "apue.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int fd;
pid_t pid;
char buf[5];
struct stat statbuf;
if(argc != 2)
{
fprintf(stderr, "usage: %s filename\n", argv[0]);
exit(1);
}
if((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0)
err_sys("open error");
if(write(fd, "abcdef", 6) != 6)
err_sys("write error");
/* turn on set-group-ID and turn off group-execute */
if(fstat(fd, &statbuf) < 0)
err_sys("fstat error");
if(fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)
err_sys("fchmod error");
。。。。。。。。。。。。。。。。。。
exit(0);
}