技术员联盟提供win764位系统下载,win10,win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:主页 > 教程 > 服务器类 >

linux进程创建

来源:技术员联盟┆发布时间:2018-06-02 18:27┆点击:

  /****fork_test.c *****/#include#include#includemain(){ pid_t pid; /*此时仅有一个进程*/ int n=4; pid=fork(); /*此时已经有两个进程在同时运行*/ if(pid<0) printf("error in fork!/n");else if(pid==0) /*返回0表示子进程*/ { n++; printf("I am the child process, my process ID is %d,n=%d/n",getpid(),n); } else /*返回大于0表示父进程*/ { n--; printf("I am the parent process, my process ID is %d,n=%d/n",getpid(),n); }}

  语句“pid=fork()”,产生了两个进程,原来存在的父进程,新出现的子进程。

  父子进程的区别除了PID不同fork函数的返回值也不相同。在父进程中,返回子进程Pid,子进程则返回0;