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中包含: 进程状态.进程打开的文件. ...
随机推荐
- 面试经典算法:优先队列,最大堆,堆排序,左偏树Golang实现
堆排序 使用优先队列-最小/最大堆可实现. 优先队列 优先队列是一种能完成以下任务的队列:插入一个数值,取出最小的数值(获取数值,并且删除).优先队列可以用二叉树来实现,我们称这种为二叉堆. 最小堆 ...
- SQL 不同服务器数据库操作
https://www.cnblogs.com/lusunqing/p/3660190.html --创建远程链接服务器 execute sys.sp_addlinkedserver @server= ...
- c#基础知识梳理(三)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10824638.html 一.方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C ...
- Trie树(字典树)-题解 P2580 【于是他错误的点名开始了】
此题可以用STL中的map做,但是了解一下Trie树这个数据结构也是必须的. Trie树(又称字典树)有以下特点: 根节点不包含字符,除它之外的每一个节点都包含一个字符. 从根节点到某一节点,路径上经 ...
- Unity 自定义"=="操作符 [翻译来源blogs.unity3d,2014/05]
主要内容来源 https://blogs.unity3d.com/cn/2014/05/16/custom-operator-should-we-keep-it/ 在我们代码里,如果有这样的代码: i ...
- 在textarea和input光标处插入内容,支持ie
项目需求,用户要能够输入和点击外面的公式去插入到textaera中,试了好几种方法,有的是在谷歌下好使,在ie下不好使,最后找到了下面这个方法,目前在ie8以上都可以生效.直接上代码 function ...
- vue slot的使用(transform动画)
slot的说明就看vue的官方文档 但是有点模糊 理解: 是对组件的扩展,通过slot插槽向组件内部指定位置传递内容,通过slot可以父子传参: 解决什么问题:正常情况下,<Child&g ...
- mysql5.7解压包安装教程
下载mysql5.7解压包Download MySQL Community Server,解压文件.https://dev.mysql.com/downloads/mysql/ 步骤1:新建my.in ...
- macOS 在终端中使用 adb命令,每次都要source ~/.bash_profile 才生效
macOS下已经配置好Android开发环境,环境变量也添加了,但是在终端中使用adb命令每次都需要source .bash_profile之后才能识别, 否则就提示 zsh: command no ...
- xposed自定义参数
java反射机制允许在不显式调用类及类方法的前提下,实现创建类对象.调用类方法等一系列操作. 目标函数为TestArray,其参数为我们自定义的Person类的数组. public class tes ...