- 论坛徽章:
- 0
|
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 int main(int argc, char *argv[])
7 {
8 char *p;
9 char str[100];
10 pid_t pid;
11
12 p = (char *)malloc(100);
13 if ( (pid = fork()) < 0 ) {
14 printf("fork is error!\n");
15 }
16 else if ( pid == 0 ) {
17 printf("C P = %p\n", p);
18 free(p);
19 printf("C Str = %p\n", str);
20 strcpy(str, "This is C");
21 printf("C Str = %p\n", str);
22 printf("C Str = %s\n", str);
23 }
24 else {
25 printf("P P = %p\n", p);
26 free(p);
27 printf("P Str = %p\n", str);
28 strcpy(str, "This is P");
29 printf("P Str = %p\n", str);
30 printf("P Str = %s\n", str);
31 }
32 return 0;
33 } |
又写了个,结果是:
-bash-3.2$ gcc -o test6 test6.c
-bash-3.2$ ./test6
C P = 0x8058860
C Str = 0x5d1556f8
C Str = 0x5d1556f8
C Str = This is C
P P = 0x8058860
P Str = 0x5d1556f8
P Str = 0x5d1556f8
P Str = This is P
-bash-3.2$
我想了下,既然是父子进程,所谓进程都是由独立空间的,虽然指向的地址相同,但是对于父子进程来说都是不透明独立私有的。这样来说就是解释了书上说的复制堆栈了。之前没想明白这点,我以为是写时复制造成了父子进程指向的堆空间地址相同呢,呵呵。。。。
原以为自己很明白这些东西了,突然范个混,仔细一深究,发现里面还很多东西需要探索的,呵呵.只是探讨,大牛飘过 |
|