我正在学习 fork ()函数 . 当我运行以下代码时:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int getpid(void);
pid_t fork(void);
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
sleep(1);//wait for parent process changing pid.
printf("Child:%d\nAddress:%p\n",pid,&pid);
exit(0);
}
printf("Parent:%d\nAddress:%p\n",pid,&pid);
pid=10;
exit(0);
}
程序总是这样输出:( pid的地址相同 . )
Parent:31244
Address:0x7ffe5f3cd344
Child:0
Address:0x7ffe5f3cd344
如果父母和孩子使用相同的内存,为什么子进程的pid没有变为10? (Ubuntu&gcc 6.2.0)
感谢帮助 :)