博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多进程下的文件描述符
阅读量:3700 次
发布时间:2019-05-21

本文共 1053 字,大约阅读时间需要 3 分钟。

传智扫地僧课程学习笔记。

父进程创建子进程后,

父 子进程共享文件描述符,都可以进行操作,

但是父子进程,操作文件的先后顺序,是随机的,

需要强调一点的是,两个进程都有独立的地址空间,都需要close(fd)关掉文件,

简单说,就是,操作的是一个文件表,在这个文件表中,会记录有2个进程在操作,想知道更细节的话,去课程中看,

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(void ){ pid_t pid; int fd; signal(SIGCHLD, SIG_IGN); printf("befor fork pid:%d \n", getpid()); /* RETURN VALUE open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). */ fd = open("./1.txt", O_RDWR); if (fd == -1) { perror("myopen"); return -1; } pid = fork(); //errno if (pid == -1) { perror("tile"); return -1; } if (pid > 0) { printf("parent: pid:%d \n", getpid()); //#include
//ssize_t write(int fd, const void *buf, size_t count); write(fd, "parent", 6); close(fd); //sleep(20); } else if (pid == 0) { printf("child: %d, parent: %d \n", getpid(), getppid()); write(fd, "child", 5); //sleep(100); close(fd); exit(0); } printf("fork after....\n"); return 0;}

转载地址:http://hobcn.baihongyu.com/

你可能感兴趣的文章
ubuntu18.04 部署zabbix
查看>>
网络映射frp
查看>>
区块链部署
查看>>
grafana忘记登陆密码
查看>>
PyCharm安装requests模块
查看>>
Ansible常用模块介绍
查看>>
ubuntu18.04部署MongoDB
查看>>
Web页面执行shell命令
查看>>
Kubernetes(一) 跟着官方文档从零搭建K8S
查看>>
grafana-server页面配置
查看>>
入坑Golang——变量
查看>>
入坑Golang —— 数据类型的基本介绍
查看>>
Golang 基本数据类型和string的转换
查看>>
Golang指针用法
查看>>
Golang 水仙花数与乘法表
查看>>
python自动检测更新业务脚本
查看>>
inotify+rsync实现实时同步
查看>>
python导入自定的py文件
查看>>
docker快速部署openVpn
查看>>
go 语言 proxy.golang.org timeout 无法访问 处理方法
查看>>