- 论坛徽章:
- 0
|
本帖最后由 digu 于 2010-09-04 21:42 编辑
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/stat.h>
- int main()
- {
- char buf[] = "abcde";
- int fd = open("file", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
- write(fd, buf, 5);
- struct flock lock1;
- lock1.l_type = F_WRLCK;
- lock1.l_start = 0;
- lock1.l_whence = SEEK_SET;
- lock1.l_len = 0;
- fcntl(fd, F_SETLK, &lock1);
- printf("parent pid:%d\n", getpid());
- pid_t pid = fork();
- if (pid == 0)
- {
- struct flock lock2;
- fcntl(fd, F_GETLK, &lock2);
- printf("child file locked by process: %d\n", lock2.l_pid);
- lseek(fd, 0, SEEK_SET);
- if (write(fd, "ddddd" ,5) < 0)
- fprintf(stderr, "child write locked~~~\n");
- }
- waitpid(pid, NULL, 0);
- return 0;
- }
复制代码 linux下得到file文件的内容是ddddd
父进程设置了文件锁,子进程中为什么还能修改文件锁定部分
加了一段测试代码显示子进程中file文件是加锁了的。
请指点一下,谢谢了~ |
|