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中包含: 进程状态.进程打开的文件. ...
随机推荐
- java——值传递和引用传递
值传递 在方法被调用时,实参通过形参把它的内容副本传入方法内部,此时形参接收到的内容是实参值的一个拷贝,因此在方法内对形参的任何操作,都仅仅是对这个副本的操作,不影响原始值的内容. 先来看个例子: p ...
- (八)springmvc之静态资源的访问。
一.直接调用 行内样式或者js直接调用没有问题. <span style="font-size:26px;color: Blue">行内样式</span> ...
- Zuul + Ribbon 脱离Eureka完成负载均衡+重试机制
Zuul + Ribbon 脱离Eureka完成负载均衡+重试机制 因为没有注册中心,所以需要网关对下游服务做负载均衡,然后果断集成Ribbon.中间遇到很多坑,最后终于解决了. 其实Ribbon里面 ...
- ajax简单页面
简单的注册页面运用ajax 主页面 <head><meta http-equiv="Content-Type" content="text/html; ...
- linux shell数值比较和字符串比较
说明: 1. 把字符串当成整型进行比较,由于abcd等字符对不上0123当程序尝试去转成二进制时无法完成转换,所以用于数值比较的运算不能用于字符串比较:但是把整型当成字符串进行比较,0123这些数值完 ...
- stm32 printf重定向
printf函数调用fputc int fputc(int ch, FILE *p) { USART_SendData(USART1, ch); //重定向到串口 while(USART_GetFla ...
- windows环境安装haproxy及初步配置负载均衡使用示例
安装HaProxy 首先需要下载windows环境下需要文件,这里下载的是一个别人编译好的一个文件,这里省去了编译的过程,使用的版本是haproxy-1.7.8. 下载后直接解压到对应的目录下.示例( ...
- 【Hibernate】事务处理
一.概述 一.概述 事务 事务就是逻辑上的一组操作,要么全都成功,要么全都失败!!! 事务特性 原子性:事务一组操作不可分割. 一致性:事务的执行前后,数据完整性要保持一致. 隔离性:一个事务在执行的 ...
- 面试总结关于Spring面试问题(精选)
1.什么是Spring? Spring是一个反转控制IOC和AOP的开发框架和平台. 2.解释一下Spring? 轻量 : Spring 在大小和透明度上是轻量的,Spring基本核心版本大概只有1M ...
- SSL/TLS 受诫礼(BAR-MITZVAH)攻击漏洞(CVE-2015-2808)
最近发现SSL/TLS漏洞已经修改过,但是绿盟扫描器还可以扫描出来,网上看了很多文章,但是能用的比较少,今天刚好有空,就自己写一下. 方法一: 控制面板--->系统和安全--->管理工具- ...