fcntl() with exec in Solaris



我使用fcntl()进行文件捕获,然后调用execlp()通过nano打开文件。我在这里和另一个会话中运行该程序。来自新会话的进程也通过 nano 打开文件,但它应该等待解锁。强制锁定和咨询锁定的效果相同。

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define editor "nano"
int main(int argc, char *argv[]) {
struct flock lock;
int fd;
if ((fd = open(argv[1], O_RDWR)) == -1) {
perror("Cannot open file");
exit(EXIT_FAILURE);
}
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(fd, F_SETLKW, &lock) == -1) {
perror("fcntl failed");
exit(EXIT_FAILURE);
}
execlp(editor, editor, argv[1], NULL);
perror("exec is not working");
exit(EXIT_FAILURE);
}

man:新进程还继承了以下属性 调用过程:...文件锁(参见 fcntl(2( 和 lockf(3C((

怎么可能?

根据 Solaris 10fcntl()手册页:

当给定进程的文件描述符被该进程关闭时,将删除与该文件关联的所有锁

运行命令truss -f -a -vall -l -d -o tout nano test.c在桁架输出中生成以下行:

Base time stamp:  1527722170.2660  [ Wed May 30 19:16:10 EDT 2018 ]
6621/1:      0.0000 execve("/usr/bin/nano", 0xFEFFEADC, 0xFEFFEAE8)  argc = 2
6621/1:      argv: nano test.c
...
6621/1:      0.0417 open64("/home/achenle/junk/test.c", O_RDONLY)   = 3
6621/1:      0.0418 fcntl(3, F_GETFD, 0xFEFFEA98)           = 0
6621/1:      0.0422 fstat64(3, 0xFEFFE6A0)              = 0
6621/1:         d=0x045D0009 i=418292 m=0100644 l=1  u=1000  g=100   sz=708
6621/1:         at = May 30 19:09:05 EDT 2018  [ 1527721745.204432507 ]
6621/1:         mt = May 30 19:04:25 EDT 2018  [ 1527721465.715445770 ]
6621/1:         ct = May 30 19:04:25 EDT 2018  [ 1527721465.833365263 ]
6621/1:         bsz=1024  blks=3     fs=lofs
6621/1:      0.0424 fstat64(3, 0xFEFFE5B0)              = 0
6621/1:         d=0x045D0009 i=418292 m=0100644 l=1  u=1000  g=100   sz=708
6621/1:         at = May 30 19:09:05 EDT 2018  [ 1527721745.204432507 ]
6621/1:         mt = May 30 19:04:25 EDT 2018  [ 1527721465.715445770 ]
6621/1:         ct = May 30 19:04:25 EDT 2018  [ 1527721465.833365263 ]
6621/1:         bsz=1024  blks=3     fs=lofs
6621/1:      0.0425 ioctl(3, TCGETA, 0xFEFFE650)            Err#25 ENOTTY
6621/1:      0.0426 read(3, " # i n c l u d e   < s y".., 1024) = 708
6621/1:      0.0428 read(3, 0x0814D794, 1024)           = 0
6621/1:      0.0428 llseek(3, 0, SEEK_CUR)              = 708
6621/1:      0.0429 close(3)                    = 0

最后的close(3)释放文件上的所有锁。 请注意,它在nano启动后不到 5/100 秒内发生。

相关内容

  • 没有找到相关文章

最新更新