执行fork()函数会创建一个新的进程,分别是父进程和子进程,这样的父子关系就相当于是克隆了一个和父亲一样的儿子。
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t ftip;
int count = 0;
ftip = fork();
if(ftip<0)
printf("error in fork!");
else if(ftip==0){
printf("I am the child process, my process id is %d\n", getpid());
printf("我是儿子\n");
count++;
}else{
printf("I am the parent process, my process id is %d\n", getpid());
printf("我是爹\n");
count++;
}
printf("统计结果是:%d\n", count);
return 0;
}