《王保明老师----Linux开发学习笔记》------讲08:进程 -----多进程下的文件描述符
来源:互联网 发布:韩顺平php入门到精通 编辑:程序博客网 时间:2024/06/05 11:53
#include<signal.h>
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
signal (SIGCHLD,SIG_IGN);
printf("before fork pid: %d \n",getpid());
/*
*/
fd=open("./1.txt",O_RDWR);
if(fd==-1)
{
perror("the error tips");
return -1;
}
pid=fork();
if(pid==-1)
{
//printf("")
perror("title");
return -1;
}
if(pid>0)
{
abc++;
printf("parent: pid :%d \n",getpid());
/*
下面开始往父进程里面写东西:
即:使用write() system call to write something to the file
ssize_t write(int fd, const void *buf,size_t count);
*/
write(fd,"parent",6);
//sleep(20);
/*printf("abc:%d \n",abc);*/
}
else if(pid==0)
{
/*
下面开始在子进程里面也写入文件内容
*/
printf("child :%d ,parent: %d \n",getpid(),getppid());
write(fd,"child",5);
//printf("abc: %d \n",abc);
}
printf("fork after ....\n");
return 0;
}
/*let's to annalysis the prom ,the parent process and the child process write to the same fd in the same moment,
so who will write finally ???
*/
in the first ,we have to build the
首先,我们应当明确:
1.子进程确实复制了父进程的打开的文件描述符
2.那么,问题来了,如果父子进程同时向同一个文件描述符写入,那么会出现什么情况???
是先让父进程写入???还是先让子进程写入??
3.还有一点:因为子进程复制了父进程的文件描述符,所以不能忘记父进程关闭该文件描述符,子进程使用完文件后也要关闭这个文件描述符。
因为:子进程和父进程都有自己各自的进程空间。
父子进程的有同一个文件描述符,但该文件描述符在内核中只存在一个文件表。
所以,父子进程的读写操作都会造成文件表中的当前文件偏移量的值发生变化,因为他俩是同一个的文件偏移量
4. 记住:文件表是归Linux内核管理。就是VFS来管理 文件表。
5.
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
signal (SIGCHLD,SIG_IGN);
printf("before fork pid: %d \n",getpid());
/*
*/
fd=open("./1.txt",O_RDWR);
if(fd==-1)
{
perror("the error tips");
return -1;
}
pid=fork();
if(pid==-1)
{
//printf("")
perror("title");
return -1;
}
if(pid>0)
{
abc++;
printf("parent: pid :%d \n",getpid());
/*
下面开始往父进程里面写东西:
即:使用write() system call to write something to the file
ssize_t write(int fd, const void *buf,size_t count);
*/
write(fd,"parent",6);
//sleep(20);
/*printf("abc:%d \n",abc);*/
}
else if(pid==0)
{
/*
下面开始在子进程里面也写入文件内容
*/
printf("child :%d ,parent: %d \n",getpid(),getppid());
write(fd,"child",5);
//printf("abc: %d \n",abc);
}
printf("fork after ....\n");
return 0;
}
/*let's to annalysis the prom ,the parent process and the child process write to the same fd in the same moment,
so who will write finally ???
*/
in the first ,we have to build the
首先,我们应当明确:
1.子进程确实复制了父进程的打开的文件描述符
2.那么,问题来了,如果父子进程同时向同一个文件描述符写入,那么会出现什么情况???
是先让父进程写入???还是先让子进程写入??
3.还有一点:因为子进程复制了父进程的文件描述符,所以不能忘记父进程关闭该文件描述符,子进程使用完文件后也要关闭这个文件描述符。
因为:子进程和父进程都有自己各自的进程空间。
父子进程的有同一个文件描述符,但该文件描述符在内核中只存在一个文件表。
所以,父子进程的读写操作都会造成文件表中的当前文件偏移量的值发生变化,因为他俩是同一个的文件偏移量
4. 记住:文件表是归Linux内核管理。就是VFS来管理 文件表。
5.
0 0
- 《王保明老师----Linux开发学习笔记》------讲08:进程 -----多进程下的文件描述符
- 《王保明老师----Linux开发学习笔记》------讲03:进程
- 《王保明老师----Linux开发学习笔记》------讲03:进程 -----fork( )函数 的疑问
- 《王保明老师----Linux开发学习笔记》------讲03:进程 -----fork( )函数 的疑问
- 《王保明老师----Linux开发学习笔记》------讲03:进程 -----fork( )函数的一点理解
- 《王保明老师----Linux开发学习笔记》------讲09:进程 -----vfork()+execve()拉起一个可执行程序
- 《王保明老师----Linux开发学习笔记》------讲12:守护进程 -----如何分析和理解下面这个创建守护进程的实例
- 多进程下的文件描述符
- linux下查看进程使用文件描述符的详细信息
- linux下的进程描述符task_struct
- Linux下的进程描述
- Linux子进程继承父进程的文件描述符
- linux 进程文件 文件描述符
- Linux下学习守护进程的笔记
- 《深入理解linx内核》学习笔记3---子进程共享父进程打开的文件描述符
- linux下的进程描述符——task_struct
- linux下的进程描述符task_struct 结构体认识
- Linux下进程间传递描述符
- 简易手电筒实现
- Android的事件处理1
- iClap是什么,能解决什么问题?
- 运行时编程block
- MySql大文件插入错误
- 《王保明老师----Linux开发学习笔记》------讲08:进程 -----多进程下的文件描述符
- 不同内核浏览器的差异以及浏览器渲染简介
- 每天一个linux命令(1):wget命令
- Linux目录规范和含义(整理)
- 字典树java版本
- [Cocoa]_[画直线、矩形、文字]
- systemctl命令用法
- 通过修改配置文件,手动创建一个Linux可登陆账户
- Centos7安装mysql缺乏yum源怎么安装