代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
static int a = 0;
printf("hello(%d). pid : %d, ppid: %d, a = %d\n", \
__LINE__, getpid(), getppid(), a);
fork();
fork();
fork();
a ++;
printf("hello(%d). pid : %d, ppid: %d, a = %d\n", \
__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]$
执行过程及分析
![无](https://i-blog.csdnimg.cn/blog_migrate/8399cfc0ded024d88f0a30373298ea9f.png)