当进程执行 fork() 系统调用时,会从中生成一个子进程。fork() 调用之后的所有代码都被复制到内存的新物理页,即帧。我无法可视化子进程的虚拟内存部分。因为在下面的代码中,char 变量的地址在子进程和父进程中是相同的。
#include <stdio.h>
#include <sys/types.h>
int main(void)
{
pid_t pid;
char y='Y';
char *ptr;
ptr=&y;
pid = fork();
if (pid == 0)
{
y='Z';
printf(" *** Child process ***\n");
printf(" Address is %p\n",ptr);
printf(" char value is %c\n",y);
sleep(5);
}
else
{
sleep(5);
printf("\n ***parent process ***\n",&y);
printf(" Address is %p\n",ptr);
printf(" char value is %c\n",y);
}
}
及其输出:
*** Child process ***
Address is 69002894
char value is Z
***parent process ***
Address is 69002894
char value is Y