fork()多次之后父子进程的数目变化
2016-07-27 09:31
459 查看
代码1:
结果:
分析:因为每一个child和father都有自己的进程地址空间,所以就会每一个地址空间中都有自己的i,j,所以就是这种结果
代码2:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int i=0;
for(i=0; i<2; i++)
{
fork();
//printf("-");
printf("-\n");
}
wait(NULL);
wait(NULL);
return 0;
}
结果:
代码3:
有pid可知:最后其实是产生出来了4个进程(包括父进程和子进程)
#include <stdio.h> int main() { int i=0; int j=0; for(i=0; i< 3; i++) { int id = fork(); if(id == 0) { printf("i am child:%d\n",j); } else { j++; printf("i am father:%d\n",j); } } sleep(2); return 0; }
结果:
分析:因为每一个child和father都有自己的进程地址空间,所以就会每一个地址空间中都有自己的i,j,所以就是这种结果
代码2:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int i=0;
for(i=0; i<2; i++)
{
fork();
//printf("-");
printf("-\n");
}
wait(NULL);
wait(NULL);
return 0;
}
结果:
代码3:
#include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { int i=0; for(i=0; i<2; i++) { fork(); printf("ppid:%d pid:%d i=%d \n",getppid(),getpid(),i); } sleep(2); return 0; }运行结果:
有pid可知:最后其实是产生出来了4个进程(包括父进程和子进程)
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍