- #include<sys/types.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- int main(void)
- {
- int count=0;
- pid_t pid;/*此时仅有一个进程*/
- pid=fork();/*此时已经有两个进程在同时进行*/
- if(pid<0)
- {
- printf("error in fork!");//添加#include<stdio.h>
- exit(1);//为什么不兼容,添加#include<stdlib.h>后就行了;
- }
- else if(pid==0)
- printf("I am the child process,the count is %d,my process ID %d\\n",count,getpid());
- else
- printf("I an the child process, the count is %d,my process ID %d\\n",++count,getpid());
- return 0;
- }
- ~
- //该片段来自于http://www.codesnippet.cn/detail/0508201410128.html
来源: http://www.codesnippet.cn/detail/0508201410128.html