Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程

linux進程創建

日期:2017/1/20 17:37:33      編輯:Linux系統常見問題解答

 /****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;

Copyright © Windows教程網 All Rights Reserved