#include<stdio.h> main() { int p1,p2; while((p1=fork())==-1);//创建子进程1 if(p1==0) //子进程 P1返回输出 putchar('b'); else //父进程返回 { while((p2=fork())==-1); //创建子进程2 if(p2==0) putchar('c'); //子进程P2返回输出'c' else putchar('a'); //父进程返回输出'a' } } #include<stdio.h> main() { int p1; putchar('x'); //父子共享部分,都要输出'x' while((p1=fork())==-1); if(p1==0) putchar('b'); //子进程输出'b' else putchar('a'); //父进程输出'a' putchar('y'); //父子共享部分,都要输出'y' } int fork(void) { int ret; /* Posix mandates that the timers of a fork child process be * disarmed, but not destroyed. To avoid a race condition, we're * going to stop all timers now, and only re-start them in case * of error, or in the parent process */ __timer_table_start_stop(1); ret = __fork(); if (ret != 0) { /* not a child process */ __timer_table_start_stop(0); } return ret; } #include<stdio.h> main() { int p1; putchar('x'); //父子共享部分,都要输出'x' while((p1=fork())==-1); if(p1==0) { putchar('b'); //子进程输出'b'后终止执行 exit(0); } else putchar('a'); putchar('y'); //只有父进程输出'y' } #include<stdio.h> main() { int p1; int x=1; //x的初值,父子进程共享 while((p1=fork())==-1); if(p1==0) { putchar('b'); //子进程提示符 x=9; //子进程中对x重新赋值 printf("%d",x); //子进程中输出x } else { putchar('a'); //父进程提示符 printf("%d",x); //父进程中输出x } }