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中包含: 进程状态.进程打开的文件. ...
随机推荐
- zcat +文件名.gz | grep "查找内容"
linux gz查看 zcat +文件名.gz | grep "查找内容" 解压 rar x xxxx.rar
- 线段树 面积并问题 hdu 1255 1542
重点整理面积并的思想 以及PushUp的及时更新 还有就是cover的实现 以及建树每个节点存的信息(每个节点存的是一个线段的信息) http://www.tuicool.com/articles/6 ...
- linux 用户切换组
问题: 因为默认的的网站路径 /var/www/html 是root 用户 root组的, 想要修改什么的需要用sudo 很麻烦. 解决: 将当前用户 hehecat加入至root组,使之有权限对目录 ...
- [C#] LINQ之SelectMany和GroupJoin
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .NET Core 常用第三方包
.NET Core 常用第三方包 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10845370.html 写在前面 最近在学习.NET Core 中经常用 ...
- Go 修改字符串中的字符(中文乱码)
问题复现:修改字符串的第一个中文 先对原字符串做切片,然后进行拼接,得到新的字符串 func ModifyString(str string) string { tempStr := str[1:] ...
- wepy全局拦截器
wepy有支持全局拦截器,但是请求需要使用wepy.request().then(): 在app.wpy文件中配置以下内容,与data同级 constructor(){ super(); this.u ...
- 基于微软hyper-v虚拟化服务器搭建方法和步骤整理
基于Microsoft基础设施私有云计算搭建 摘要:私有云是指组织机构建设的专供自己使用的云平台,它所提供的服务不是供他人使用,而是供自己的内部人员或分支机构使用,不同于公有云,私有云部署在企业内部网 ...
- vue 钩子函数的初接触
vue-router的路由钩子函数: 第一种:全局钩子函数. router.beforeEach((to, from, next) => { console.log('beforeEach') ...
- Java 之 泛型
一.泛型概述 集合中是可以存放任意对象的,只要把对象存储集合后,那么这时他们都会被提升成 Object 类型.当我们取出一个对象,并且进行相应的操作,这时必须采用类型转换. 先观察下面代码: publ ...