#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<errno.h>intmain(){pid_t pid;/*pid 进程id号*/ pid=fork();/*创建一个新进程*/if(pid==0)/*返回0为子进程*/{printf("Return pid is %d\n",pid);printf("This is son process! pid is:%d\n",getpid());}elseif(pid>0)/*返回大于0为父进程*/{printf("Return pid is %d\n",pid);printf("This is parent process! pid is:%d\n",getpid());waitpid(pid,NULL,0);/*等待子进程退出*/}else{perror("fork() error!"); exit;}}
/*
* fork_test.c
*/
#include <unistd.h>
#include <stdio.h>
int main ()
{
pid_t fpid; //fpid表示fork函数返回的值
int count=0;
fpid=fork();
if (fpid < 0)
printf("error in fork!");
else if (fpid == 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;
}
i am the child process, my process id is 5574
我是爹的儿子
统计结果是: 1
i am the parent process, my process id is 5573
我是孩子他爹
统计结果是: 1
pid=fork(); // 此行后加入
printf("pid:%d\n",pid);
pid:19007
This is in the father process,here write a string to the pipe.
pid:0
This is in the child process,here read a string from the pipe.
Hello world , this is write by pipe.