当前位置:网站首页>进程fork
进程fork
2022-07-22 10:35:00 【安若兮~】
printf缓冲区(打印会满足以下三个条件之一)
1.缓冲区放满会直接打在屏幕上
2.缓冲区未满,用户强制刷新
3.程序结束
fflush(stdout); 直接刷新输出缓冲区
主函数参数
int main(int argc,char* argv[],char* envp){}
// 参数个数 参数内容 环境变量
当执行某个命令时,如./mian命令,./main本质上是一个可执行程序,bush复制自己本身,将复制出来的子bush替换成main程序,然后运行main程序。
父进程的父进程是bush
fork 复制一个进程,产生子进程
fork(); 如果返回值为0;说明创建子进程成功,-1表示创建失败
pid_t pid=fork();
if(pid==-1){//创建判断
exit(1);
}
if(pid==0){
//子进程
}
else{
//父进程
}
注意: 子进程pid 父进程ppid
获取子进程 id getpid() 获取父进程 id getppid()
父子进程使用的不是一块内存空间
我们需要注意:在程序中看到的地址是逻辑地址而不是物理地址,所以,当地址相同的时候也不一定是一块内存。
求物理地址:&n%4k=商(页号),余数(页内偏移量)(4k具体看系统)
写时拷贝:是为了提高fork效率,将可以共享的尽量共享,不能共享的则只能拷贝
这里是子进程结束后才能获取到退出码。在子进程结束之前,wait进程都是阻塞状态
僵尸进程:僵尸状态的进程(子进程先结束,父进程没有获取子进程的退出码)
wait():获取子进程的退出码
pid_t wait(int *wstatus);(这里是4个字节的指针)
#include<sys/wait.h>
int val=0;
int child_pid=wait(&val);
if(WIFEXITED(val)){
printf("error exit: %d\n"WEXITSTATUS(val));
}
exit(3);//程序退出码
孤儿进程(父进程先结束,那么就要为子进程重新找一个父进程,新的父进程来获取子进程的退出码。防止出现僵死进程)
fork()||fork();
此时会产生3个结果
for(int i=0;i<2;i++){
fork();
printf("A"):
}
边栏推荐
- 1045 Favorite Color Stripe (30 分)
- What does Baidu snapshot hijacking mean? How to solve Baidu snapshot hijacking and Baidu hijacking
- Causal learning weekly, issue 10: introduction to the latest causal discovery related papers in iclr2022
- Flask Cross - Domain
- Kubernetes基础入门
- JNI 数据类型用法
- PAT-2021年冬季考试(满分)
- LeetCode103——zigzagLevelOrder of binary tree
- Mockito3.8 如何mock静态方法 (如何mock PageHelper)
- mysql索引
猜你喜欢
mysql引擎
LeetCode0003——longest substring without repeating characters——Sliding Window
LeetCode912——sort an array—— quick sort algorithm
她力量系列三丨把握当下,坚持热爱,与食物图像识别结缘的科研之路
On the horizontal trigger and edge trigger of epoll
"Pre training weekly" No. 38: transformer, Bert structure optimization
AMBert
她力量系列八丨陈丹琦:我希望女生能够得到更多的机会,男生和女生之间的gap会逐渐不存在的
LeetCode160 & LeetCode141——double pointers to solve the linked list
YOLO v1、v2、v3
随机推荐
1076 Forwards on Weibo (30 分)
本地镜像发布到私有库
Aminer paper recommendation
vim配置
Introduction to machine learning: Logistic regression-2
Plug in installation of elastic search getting started (5)
笔记:C语言
Unix C语言POSIX的线程创建、获取线程ID、汇合线程、分离线程、终止线程、线程的比较
Guidelines for installation and use of Damon database
1080 Graduate Admission (30 分)
Her power series II UCLA Li Jingyi: the last thing women need to do is "doubt themselves"
redission看门狗实现过程详解
AMBert
信号降噪方法
Introduction to machine learning: linear regression-1
Flask cross domain
LeetCode103——zigzagLevelOrder of binary tree
LeetCode21——Merge two sorted lists——Iteration or recursion
7-2 Rank a Linked List (25 分)
LeetCode53——Maximum Subarray——3 different methods