父子进程共享文件表的
2007-07-12 10:15
218 查看
通过一段小代码,证明当父进程fork后,并没有复制文件表,只是复制了 文件描述符,所以父子进程
有同样的 状态标识符,当前位移
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int main(int argc,char * argv[])
{
int fd;
if((fd=open("lhtmpfile",O_RDWR|O_CREAT))<0)
perror("file open error");
if(!fork())
{
sleep(2);
printf("child process:%d/n",lseek(fd,0,SEEK_CUR));
exit(0);
}
lseek(fd,20,SEEK_CUR);
printf("father process:%d/n",lseek(fd,0,SEEK_CUR));
wait(NULL);
exit(0);
}
~
~
"fork_filetab.c" 21L, 421C written
$ gcc fork_*
$ ./a.out
father process:20
child process:20
$
有同样的 状态标识符,当前位移
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int main(int argc,char * argv[])
{
int fd;
if((fd=open("lhtmpfile",O_RDWR|O_CREAT))<0)
perror("file open error");
if(!fork())
{
sleep(2);
printf("child process:%d/n",lseek(fd,0,SEEK_CUR));
exit(0);
}
lseek(fd,20,SEEK_CUR);
printf("father process:%d/n",lseek(fd,0,SEEK_CUR));
wait(NULL);
exit(0);
}
~
~
"fork_filetab.c" 21L, 421C written
$ gcc fork_*
$ ./a.out
father process:20
child process:20
$
相关文章推荐
- fork()后父子进程共享文件
- 9.fork函数和父子进程共享文件
- 十、Linux系统编程-进程(三)父子进程共享文件、fork和vfork、exit和_exit、atexit注册退出事件
- 父子进程间文件共享
- [学习笔记]父子进程共享文件描述符理解
- Python mutilprocessing Processing 父子进程共享文件对象?
- ZT 父子进程共享文件描述符
- 父子进程共享文件描述符
- 父子进程文件共享对socket的影响
- 关于父子进程共享文件的学习研究
- 用文件映射(File Mapping)实现进程间内存共享
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
- 最简单易懂的进程通信入门实例(“共享外存/文件”)
- 用内存映射文件在进程中共享数据http://blog.csdn.net/zicheng_lin/article/details/6660404
- 共享内存实现父子进程间通讯
- Linux进程实践(2) --僵尸进程与文件共享
- 使用内存映射文件在进程间共享数据
- Linux文件共享(一)——进程与打开文件
- 用文件映射(File Mapping)实现进程间内存共享[转]
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped