- 论坛徽章:
- 2
|
本帖最后由 cdsfiui 于 2015-10-23 11:29 编辑
回复 10# darling54454
这个回复非常棒,我在书上找了半天都没有找到hehe
不过,我即使把程序改成了:
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/stat.h>
- #include<sys/types.h>
- #include<fcntl.h>
- #include<dirent.h>
- #include<sys/file.h>
- #include<unistd.h>
- int main()
- {
- int fd=open("mandatory.txt",O_RDWR);
- struct flock lk;
- lk.l_type=F_WRLCK;
- lk.l_start=0;
- lk.l_whence=SEEK_SET;
- lk.l_len=0;
- lk.l_pid=getpid();
- int flag=fcntl(fd,F_SETLKW,&lk);
- struct stat statbuf;
- fstat(fd,&statbuf);
- fchmod(fd,(statbuf.st_mode&~S_IXGRP)|S_ISGID);//你说的要用fchmod
- printf("%d\n",flag);
- getchar();//运行到这里的时候,我新开一个shell窗口
- close(fd);
- return 0;
- }
复制代码 我在1L观察到的问题仍然存在啊,文件并没有被锁住,其他的shell还是能写它。
为什么呢? |
|