通过实例体会 fork()同时创建多个子进程
2014-05-03 20:51
393 查看
特点:同时创建多个子进程,每个子进程可以执行不同的任务,程序可读性较好,便于分析,易扩展为多个子进程
执行结果:
第二种方法
特点:同时创建两个子进程,结构比较繁琐,程序可读性不好,不易扩展
注意:if .......else if .........else........语句只执行一次。
当执行完if语句的时候子进程结束if...else if...else语句,父进程并没有结束。
第三种方法:for循环方法
特点:其实每次循环只是创建了单个进程,并没有同时创建多个进程
执行结果:
注意:标注的return 0 对程序结果影响很大
无return 0 情况
执行结果:
结论:父进程会生成2N-1个子进程,N为循环次数,本例中共有7个子进程,但实际上只有3个是父进程产生的,其余都为子进程fork()出来的
#include <stdio.h> #include <stdlib.h> int main() { pid_t p1,p2; printf("before fork(),pid=%d\n",getpid()); p1=fork(); if(p1==0) { printf("in child 1, pid = %d\n", getpid()); return 0; } p2 = fork(); if( p2 == 0 ) { printf("in child 2, pid = %d\n", getpid()); return 0; //程序结束不再执行 printf("Hello world\n"); } /*int st1, st2; waitpid(p1, &st1, 0); waitpid(p2, &st2, 0); printf("in parent, child 1 pid = %d\n", p1); printf("in parent, child 2 pid = %d\n", p2); printf("in parent, pid = %d\n", getpid()); printf("in parent, child 1 exited with %d\n", st1); printf("in parent, child 2 exited with %d\n", st2);*/ return 0; }
执行结果:
before fork(),pid=2381 in child 1, pid = 2382 in child 2, pid = 2383
第二种方法
特点:同时创建两个子进程,结构比较繁琐,程序可读性不好,不易扩展
#include <stdio.h> #include <unistd.h>//查查在windows下能编译吗?Linux函数。 #include <sys/types.h>//这个头文件不能少,否则pid_t没有定义 main() { printf("This is parent process %d\n",getpid()); pid_t p1,p2; if((p1=fork())==0) printf("This is child_1 process %d\n",getpid()); else if((p2=fork())==0) printf("This is child_2 process %d\n",getpid()); else { wait(p1,NULL,0); wait(p2,NULL,0); printf("This is parent process %d\n",getpid()); } }执行结果:
This is parent process3553 This is child_1 process3554 This is child_2 process3555 This is parent process3553
注意:if .......else if .........else........语句只执行一次。
当执行完if语句的时候子进程结束if...else if...else语句,父进程并没有结束。
第三种方法:for循环方法
特点:其实每次循环只是创建了单个进程,并没有同时创建多个进程
#include<stdio.h> #include<sys/types.h> main() { printf("This is parent process %d\n",getpid()); pid_t p1,p2; int i; for(i=0;i<=2;i++){ if((p1=fork())==0){ printf("This is child_1 process %d\n",getpid()); return 0;//这个地方非常关键,父进程结束 } /*wait(p1,NULL,0); printf("This is parent process %d\n",getpid());/ }
执行结果:
This is parent process2655 This is child_1 process2656 This is child_1 process2657 This is child_1 process2658
注意:标注的return 0 对程序结果影响很大
无return 0 情况
#include <stdio.h> #include <unistd.h> #include <sys/types.h> main() { printf("This is parent process %d\n",getpid()); pid_t p1,p2; int i; for(i=0;i<=2;i++) { if((p1=fork())==0) { printf("This is child_1 process %d\n",getpid()); //return 0;//这个地方非常关键,父进程继续执行 } /* wait(p1,NULL,0); printf("This is parent process %d\n",getpid());*/ } }
执行结果:
This is parent process2620 This is child_1 process 2621 This is child_1 process 2622 This is child_1 process 2625 This is child_1 process 2626 This is child_1 process 2624 This is child_1 process 2623 This is child_1 process 2627
结论:父进程会生成2N-1个子进程,N为循环次数,本例中共有7个子进程,但实际上只有3个是父进程产生的,其余都为子进程fork()出来的
相关文章推荐
- linux中fork同时创建多个子进程的方法(一)
- fork同时创建多个子进程的方法
- linux中fork同时创建多个子进程的方法(二)
- Fork同时创建多个子进程方法
- Linux中fork同时创建多个子进程的方法(二)
- 进程控制:linux中fork同时创建多个子进程注意事项
- linux中fork同时创建多个子进程的方法
- linux中fork同时创建多个子进程的方法(一)
- Linux中通过fork()同时创建多个进程
- ★实验 9-2 1. 补全代码,实现以下功能 a) 调用pipe()创建无名管道 b) 调用fork创建一个子进程 c) 在父进程中向管道写入 “helloworld”, 同时打印出PID和写入
- Linux下用fork()派生的子进程通过pipe管道通讯的实例详解("生产者-消费者"问题)
- Linux 调用fork()创建两个子进程
- 通过fork、vfork和clone创建进程
- UNIX环境编程----编程实例---创建一个子进程并建立一个新的会话
- [Linux进程]使用fork函数创建多个子进程
- fork同一时候创建多个子进程的方法
- zygote通过包装fork完成activity(linux下进程)创建
- linux中fork同时创建多个子…
- 多线程编程-通过fork方式创建任务进程
- Linux中使用C语言的fork()函数创建子进程的实例教程