Linux之创建多个子进程
/***
fork_test.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> int main()
{
pid_t pid;
printf("xxxxxxxx\n"); pid = fork();
if(- == pid)
{
perror("fork error:");
exit();
}
else if(pid == )
{
printf("I'm child,pid = %u,ppid = %u\n",getpid(),getppid());
}
else
{
printf("I'm parent,pid = %u, ppid = %u\n",getpid(),getppid());
sleep();
}
printf("YYYYYYYYYYY\n");
return ;
}
运行结果:
ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ ./fork_test
xxxxxxxx
I'm parent,pid = 2610, ppid = 2558
I'm child,pid = 2611,ppid = 2610
YYYYYYYYYYY
YYYYYYYYYYY
循环创建N个子进程:
使用for循环创建五个子进程:
/***
fork_test.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> int main()
{
int i;
pid_t pid;
printf("xxxxxxxx\n"); for(i = ; i < ; i++)
{
pid = fork();
if(- == pid)
{
perror("fork error:");
exit();
}
else if(pid == )
{
printf("I'm child,pid = %u,ppid = %u\n",getpid(),getppid());
}
else
{
printf("I'm parent,pid = %u, ppid = %u\n",getpid(),getppid());
sleep();
}
}
printf("YYYYYYYYYYY\n");
return ;
}
运行程序后却创建了2^5-1个子进程。
问题分析:

问题解决:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> int main()
{
int i;
pid_t pid;
printf("xxxxxxxx\n"); for(i = ; i < ; i++)
{
pid = fork();
if(pid == )
{
break;
}
} if(i < )
{
sleep(i);
printf("I'm %d child,pid = %u\n",i+,getpid()); }
else
{
sleep(i);
printf("I'm parent\n"); }
return ;
}
在子进程pid == 0 时直接break出来就好了。
运行结果:
ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ make fork_test
gcc fork_test.c -o fork_test -Wall -g
ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ ./fork_test
xxxxxxxx
I'm 1 child,pid = 3157
I'm 2 child,pid = 3158
I'm 3 child,pid = 3159
I'm 4 child,pid = 3160
I'm 5 child,pid = 3161
I'm parent
Linux之创建多个子进程的更多相关文章
- fork同一时候创建多个子进程的方法
Fork同一时候创建多个子进程方法 第一种方法:验证通过 特点:同一时候创建多个子进程.每一个子进程能够运行不同的任务,程序 可读性较好,便于分析,易扩展为多个子进程 int main(void) { ...
- linux内核分析作业6:分析Linux内核创建一个新进程的过程
task_struct结构: struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标识符 u ...
- 第六周——分析Linux内核创建一个新进程的过程
"万子恵 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 &q ...
- 实验六:分析Linux内核创建一个新进程的过程
原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 题目自拟,内容围绕对Linu ...
- Linux如何创建一个新进程
2016-03-31 张超<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux如何创建一个新进程 ...
- 第六周分析Linux内核创建一个新进程的过程
潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...
- 实验 六:分析linux内核创建一个新进程的过程
实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...
- 20135202闫佳歆--week6 分析Linux内核创建一个新进程的过程——实验及总结
week 6 实验:分析Linux内核创建一个新进程的过程 1.使用gdb跟踪创建新进程的过程 准备工作: rm menu -rf git clone https://github.com/mengn ...
- 《Linux内核--分析Linux内核创建一个新进程的过程 》 20135311傅冬菁
20135311傅冬菁 分析Linux内核创建一个新进程的过程 一.学习内容 进程控制块——PCB task_struct数据结构 PCB task_struct中包含: 进程状态.进程打开的文件. ...
随机推荐
- 标准Trie、压缩Trie、后缀Trie
ref : https://dsqiu.iteye.com/blog/1705697 1.Trie导引 Trie树是一种基于树的数据结构,又称单词查找树.前缀树,是一种哈希树的变种.应用于字符串的统计 ...
- APK反编译教程
在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...
- Centos 在VM中设置静态ip
cd /etc/sysconfig/network-scripts 然后代开第一个文件 一般是ifcfg-ens331)开始配置原来是这样的 修改/etc/sysconfig/network # Cr ...
- js循环及for-in , for-of的区别
循环 字符串遍历:可通过for-of遍历字符串 for-in:遍历对象自身可继承可枚举属性 Object.keys():返回对象自身可枚举属性的键组成的数组 Object.getOwnProperty ...
- sql server存储过程返回数据只有一个字符
SqlParameter[] param = { new SqlParameter("@shopId",shopId), new SqlParameter("@newSh ...
- Spring中Bean的管理问题
首先,配置文件中定义的bean并不是都在启动时实例化. <bean id="accountService" class="com.foo.DefaultAccoun ...
- JdbcTemplate批量插入数据
运行环境:SpringBoot,注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; 1.单表批量插入数据 @Test public ...
- 【转】DELPHI开始支持LINUX DOCKER
这是咏南翻译Marco Cantu的文章. 在过去的几年中,将服务器端解决方案(实际上是任何类型的应用程序)部署到轻量级DOCKER而不是物理机器或虚拟机已经变得越来越普遍,因为这允许更大的灵活性(在 ...
- 《数据结构与算法之美》 <03>数组:为什么很多编程语言中数组都从0开始编号?
提到数组,我想你肯定不陌生,甚至还会自信地说,它很简单啊. 是的,在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构.尽管数组看起来非常 ...
- ELK6.x_Kafka 安装配置文档
1. 环境描述 1.1. 环境拓扑 如上图所示:Kafka为3节点集群负责提供消息队列,ES为3节点集群.日志通过logstash或者filebeat传送至Kafka集群,再通过logstash发 ...