exit(0) //正常退出当前进程
fork() //之后的语句,子进程和父进程同时运行。但是在子进程中fork返回0,在父进程中fork返回大于1的进程ID。也就是说,你定义的I在父进程和子进程中有不同的值。
if(fork()==0){//这个判断,子父进程都有,但是在父进程中fork()的返回值大于0,所以父不会执行这段程序,fork()在子进程中返回0,子进程会执行该程序块。经常会在该程序块中使用exit(0),这个语句使子进程退出执行。如果没有该语句,这个程序块后面的程序都将被父进程和子进程执行}
通过一个小例子分析fork的工作原理。
#include <stdio.h>
#include <unistd.h>



int main()...{

char father[]="father";
char child[]="child";
int nPid,nFatherPid;
//asigned and then compared.
if((nPid=5)==5)printf("success %d! ",(nPid=3));

nFatherPid=getpid();
printf("father getpid()=%d ",getpid());


if((nPid=fork())==0)...{// child process,in child process fork() return 0
printf("child getpid()= %d nPid=%d ",getpid(),nPid);

}

if(nPid==nFatherPid)...{
printf("father getpid()=%d, nFatherPid=%d ",getpid(),nFatherPid);
}
//father and child are also running.
printf("father or child ,getpid()=%d nPid=%d ",getpid(),nPid);

//child stoped.
if(nPid==0)exit(0);

printf("father nPid=%d ",nPid);
printf("getpid()=%d ",getpid());
return 1;


}
