- 论坛徽章:
- 0
|
- [oracle@localhost ~]$ cat test_fork.c
- #include <stdio.h>
- int main(void)
- {
- printf("hello!");
- int i;
- i = fork();
- printf("aaa\n");
- return 0;
- }
- [oracle@localhost ~]$ gcc test_fork.c
- [oracle@localhost ~]$ ./a.out
- hello!aaa
- hello!aaa
- [oracle@localhost ~]$
复制代码- [oracle@localhost ~]$ cat basic_pro.c
- #include <stdio.h>
- int main(void)
- {
- printf("aaa\n");
- int i;
- i = fork();
- printf("i = %d\n", i);
- return 0;
- }
- [oracle@localhost ~]$ gcc basic_pro.c
- [oracle@localhost ~]$ ./a.out
- aaa
- i = 0
- i = 4006
- [oracle@localhost ~]$
复制代码 我的疑惑是,为什么第一个程序中的printf("hello!");执行了两次,而第二个程序中的printf("aaa\n");执行了一次?!
fork不是建立了基本相同的进程吗?所以,我认为main函数里的第一个printf语句,无论在父进程还是子进程里面,都应该执行一次才对啊?
|
|