- 论坛徽章:
- 0
|
看个代码先:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- char* tmp = NULL;
- main()
- {
- pid_t pid_tmp;
- tmp = malloc(40);
- strcpy(tmp,"parent");
- pid_tmp = fork();
- switch(pid_tmp)
- {
- case -1 :
- printf("!!!!!!fork err!!!!!!!!!!\n");
- exit(1);
- case 0 :
- printf("tmp in child process before mod:%s\n", tmp);
- memset(tmp, 0, 40);
- strcpy(tmp,"child");
- printf("tmp in child process after mod:%s\n",tmp);
- exit(0);
- }
- wait(&pid_tmp);
- printf("%s\n",tmp);
- exit(0);
- }
复制代码
运行结果如下:
tmp in child process before mod:parent
tmp in child process after mod:child
parent
我的问题是,在上面的主进程fork子进程的时候是不是在子进程对主进程中的指针变量和指针变量所指的内存都做了一份内存拷贝呢?
如果是的话,假如我主进程中的指针变量指向的是一个很大的内存块,那样的话,fork进程的时候拷贝内存是不是花很多的时间??
这是我现在项目中遇到的问题,麻烦各位解答以下:) |
|