- 8
- 0
- 约4.52千字
- 约 9页
- 2018-08-27 发布于江苏
- 举报
fork创建多个子进程3
HYPERLINK /s/blog_605f5b4f0100x444.html 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 =
原创力文档

文档评论(0)