程序二:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
int main()
{
int p1,p2;
while((p1=fork())==-1);
if(p1==0)
{
printf("b.My process ID is %d\n",getpid());
}
else
{
while((p2=fork())==-1);
if(p2==0){
printf("c.My process ID is %d\n",getpid());
}
else{
printf("a.My process ID is %d\n",getpid());
}
}
}
fork()調(diào)用后,產(chǎn)生父進(jìn)程和子進(jìn)程。子進(jìn)程打印a.My process ID is……,父進(jìn)程進(jìn)入else語(yǔ)句塊執(zhí)行fork(),由此父進(jìn)程再?gòu)?fù)制出一個(gè)子進(jìn)程,父進(jìn)程打印b.My process ID is……,子進(jìn)程打印c.My process ID is……。
程序三:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
void main()
{int m,n,k;
m=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",m);
printf("he\n");
n=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",n);
printf("ha\n");
k=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",k);
printf("ho\n");
}