linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
来源:互联网 发布:数据结构与算法分c答案 编辑:程序博客网 时间:2024/06/08 16:35
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void cyg_error(char * msg)
{
printf("%s:%s/n",msg,strerror(errno));
exit(0);
}
//构建自己的安全的fork函数
pid_t myFork()
{
pid_t mypid;
if ((mypid = fork())<0)
{
cyg_error("fork");
}
return mypid;
}
void fdInFork()
{
int fd;
char c;
int size;
int pid;
fd = open("test.txt",O_RDONLY,0); //打开一个共享文件描述符
//test.txt文件的内容 "abcdefghijklmn"
if((pid = myFork())==0) //子进程
{
size = read(fd,&c,1);
printf("_________the character is %c/n",c);
lseek(fd,3,1);
size = read(fd,&c,1);
printf("_________the character is %c/n",c);
size = read(fd,&c,1);
printf("~~~~~~~~~the character is %c/n",c);
close(fd);//子进程中关闭了该描述符,但是共享文件操作的痕迹还是被保留了
size = read(fd,&c,1);//会返回-1,因为已经关掉
printf("size %d_________the character is %c/n",size,c);
exit(0);
}
//waitpid(-1,NULL,0);//等待子进程结束
wait(NULL);
size = read(fd,&c,1);//接着子进程的游标位置开始读
printf("########the character is %c/n",c);
close(fd);//当文件的所有引用都关掉时,内存文件映像才被回收
printf("###the character is %c/n",c);
}
函数执行结果
_________the character is a
_________the character is e
~~~~~~~~~the character is f
size -1_________the character is f
########the character is g
###the character is g
内存映像图
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1641884
- linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
- linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
- 父子进程间文件描述符的共享
- linux 经典的例子 解释 dup dup2 文件描述符重定向函数 输入输出重定向
- linux 经典的例子 解释 dup dup2 文件描述符重定向函数 输入输出重定向
- linux 经典的例子 解释 dup dup2 文件描述符重定向函数 输入输出重定向
- Linux中fork的使用(05)---父子进程共享文件描述符
- 父子进程间文件共享
- 在父子进程间用管道传递文件描述符
- 映像内存文件学习
- 关于内存映像文件
- Windows内存映像文件
- 内存映像文件简介
- linux c 通过内存映射,操作文件
- 借助内存映像文件实现DLL共享EXE中的ADOConnection
- delphi 实现两个exe文件共享内存映像的代码
- delphi 实现两个exe文件共享内存映像的代码
- 通过文件映射共享内存
- 热死啦!!!!
- Strus 2的新表单标志的使用
- 风总是吹
- javascript中RegExp对象的test方法
- 如何刷新JDialog
- linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
- Struts 2中的OGNL
- 在Struts 2中实现CRUD
- 讨论 2007/06/07
- 在SharePoint2007中实现Form认证需注意的几个问题
- 在Struts 2中实现文件上传
- C#16位MD5加密算法的一个错误测试数据处理
- jsp中获得SQL SERVER详细的时间
- IT,技术人员的不归路