fork同一时候创建多个子进程的方法

Fork同一时候创建多个子进程方法

 

 

第一种方法:验证通过 

特点:同一时候创建多个子进程。每一个子进程能够运行不同的任务,程序 可读性较好,便于分析,易扩展为多个子进程 

int main(void) 

printf("before fork(), pid = %d\n", getpid()); 

pid_t p1 = fork(); 

if( p1 == 0 )

printf("in child 1, pid = %d\n", getpid()); 

return 0; //若此处没有return 0 p1 进程也会运行 pid_t p2=fork()语句

pid_t p2 = fork(); 

if( p2 == 0 ) 

printf("in child 2, pid = %d\n", getpid()); 

return 0; //子进程结束。跳回父进程

Printf("hello world\");//没有打印

}

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; 

 

 

另外一种方法: 验证通过 

特点:同一时候创建两个子进程。结构比較繁琐。程序可读性不好,不易扩展 

 

#include<stdio.h> 

#include<unistd.h> 

#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()); 

 

 

第三种方法:for 循环方法 

特点:事实上每次循环仅仅是创建了单个进程。并没有同一时候创建多个进程 

#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); //父进程等待p1子进程运行后才干继续fork其它子进程

printf("This is parent process%d\n",getpid()); 

}

注意:标注的 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()); 

}

结论:父进程会生成 n(n+1)/2+1个子进程,为循环次数,本例中共同拥有 个子进程。 但实际上仅仅有 个是父进程产生的。其余都为子进程 fork()出来的。父进程fork3个进程,第一个子进程运行完之后又fork2个进程,第2个子进程fork1个进程。

 

 

 

正确的使用Linux中的用fork()由一个父进程创建同一时候多个子进程 的格式例如以下:

 

int status,i;

for (i = 0; i < 10; i++)

{

  status = fork();

  if (status == 0 || status == -1) break;//每次循环时,假设发现是子进程就直接从创建子进程的循环中跳出来。不让你进入循环,这样就保证了每次仅仅有父进程来做循环创建子进程的工作

}

if (status == -1)

{

  //error

}

else if (status == 0) //每一个子进程都会运行的代码

{

  //sub process

}

else

{

  //parent process

}

 

编写这样一个程序需要使用C语言的`fork()`函数创建子进程,以及`pipe()`函数建立管道。下面是一个简单的示例,展示了如何在Unix/Linux环境中实现这个功能: ```c #include <stdio.h> #include <unistd.h> #include <sys/types.h> #define BUFFER_SIZE 10 void parent(void) { int pipefd[2]; // 管道文件描述符数组 pid_t child1, child2, child3; // 创建管道 if (pipe(pipefd) == -1) { perror("Pipe creation failed"); return; } // 创建第一个子进程 child1 = fork(); if (child1 == 0) { // 子进程 close(pipefd[0]); // 关闭读端 write(pipefd[1], "Hello from Child1", strlen("Hello from Child1")); printf("Child1 wrote to pipe and exited.\n"); exit(0); } else if (child1 > 0) { // 父进程 close(pipefd[1]); // 父进程关闭写端 // 等待并处理第一个子进程消息 char buffer[BUFFER_SIZE]; read(pipefd[0], buffer, BUFFER_SIZE); printf("Parent received from Child1: %s\n", buffer); // 创建第二个子进程 child2 = fork(); if (child2 == 0) { close(pipefd[0]); write(pipefd[1], "Hello from Child2", strlen("Hello from Child2")); printf("Child2 wrote to pipe and exited.\n"); exit(0); } else if (child2 > 0) { // 接收第二个子进程消息 read(pipefd[0], buffer, BUFFER_SIZE); printf("Parent received from Child2: %s\n", buffer); // 创建第三个子进程 child3 = fork(); if (child3 == 0) { close(pipefd[0]); write(pipefd[1], "Hello from Child3", strlen("Hello from Child3")); printf("Child3 wrote to pipe and exited.\n"); exit(0); } else if (child3 > 0) { // 最后一次接收消息 read(pipefd[0], buffer, BUFFER_SIZE); printf("Parent received from Child3: %s\n", buffer); } } } else { perror("Forking error"); exit(1); } close(pipefd[0]); // 父进程中关闭剩余的管道端口 } int main() { parent(); // 调用父进程函数 return 0; } ``` 在这个例子中,父进程首先创建一个管道,然后通过`fork()`函数创建三个子进程。每个子进程会向管道中写入信息,而父进程则从管道读取这些信息。请注意,实际应用中你需要对错误情况进行适当的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值