代码
/*****************************************************
* > File Name: fork.c
* > Author: fly
* > Create Time: 2021-07-27 2/30 13:10:25 +0800
*==================================================*/
#include
#include
#include
int main(int argc, char* argv[])
{
static int a = 0;
printf("hello(%d). pid : %d, ppid: %d, a = %dn",
__LINE__, getpid(), getppid(), a);
fork();
fork();
fork();
a ++;
printf("hello(%d). pid : %d, ppid: %d, a = %dn",
__LINE__, getpid(), getppid(), a);
return 0;
}
编译、执行
[fly@fly-vm test]$ make fork
gcc -o fork fork.c -g -Wall[fly@fly-vm test]$ ./fork
hello(17). pid : 90746, ppid: 4636, a = 0
hello(25). pid : 90746, ppid: 4636, a = 1
hello(25). pid : 90747, ppid: 90746, a = 1
[fly@fly-vm test]$ hello(25). pid : 90749, ppid: 1, a = 1
hello(25). pid : 90748, ppid: 1, a = 1
hello(25). pid : 90750, ppid: 1, a = 1
hello(25). pid : 90753, ppid: 1, a = 1
hello(25). pid : 90752, ppid: 1, a = 1
hello(25). pid : 90751, ppid: 1, a = 1
[fly@fly-vm test]$
执行过程及分析
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net