fork()之后地址,值和指针会发生什么 [英] What happens to address's, values, and pointers after a fork()

查看:89
本文介绍了fork()之后地址,值和指针会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个问题,该问题是在C中在fork()调用之前和之后检查值和地址.我的方法是显示变量值和地址,并假定在fork().令我惊讶的是,这些变量保持不变.

I'm working on a question where I am to examine values and address before and after a fork() call in C. My approach was to display the variables values and address assuming to see a difference in the address after the fork(). Much to my surprise address's for said variables remained the same.

我的问题是为什么它们相同?如果我更改孩子的变量会怎样?父母和孩子都会改变吗?如果没有,那么我该如何更改该地址中的值,而父母和孩子的地址都相同.

My questions is why are they the same? What happens if I change a variable in the child? Will it change in both parent and child? If not, how am I able to change the value in that address while the address is the same for both parent and child.

代码(仅供参考):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
  int status;
  pid_t pid;

  int a = 123456;
  float b = 123.456;
  char c = 'Z';
  int *e;
  e=&a;

  //Retriving address's
  void *ap=&a, *bp=&b, *cp=&c, *ep=&e;

    printf("Parent Before Fork:\n");
    printf("Integer a: \tvalue = %d, \taddress = %p\n", a, ap);
    printf("Float b: \tvalue = %f, \taddress = %p\n", b, bp);
    printf("Char c: \tvalue = %c, \t\taddress = %p\n", c, cp);
    printf("Pointer e: \tvalue = %p, address = %p\n", e, ep);  

    pid = fork();

    if(pid > 0)
    {
      pid = wait(&status);
      printf("\nParent After Fork:\n");
      printf("Integer a: \tvalue = %d, \taddress = %p\n", a, ap);
      printf("Float b: \tvalue = %f, \taddress = %p\n", b, bp);
      printf("Char c: \tvalue = %c, \t\taddress = %p\n", c, cp);
      printf("Pointer e: \tvalue = %p, address = %p\n", e, ep);

      sleep(1);
    }
    else if(pid == 0)
    {
      printf("\nChild After Fork:\n");
      printf("Integer a: \tvalue = %d, \taddress = %p\n", a, ap);
      printf("Float b: \tvalue = %f, \taddress = %p\n", b, bp);
      printf("Char c: \tvalue = %c, \t\taddress = %p\n", c, cp);
      printf("Pointer e: \tvalue = %p, address = %p\n", e, ep);
   }
   else
     printf("fork() did not work");

return 0; 
}

输出(用于参考):

Parent Before Fork:
Integer a:  value = 123456,         address = 0x7fff8b8e378c
Float b:    value = 123.456001,     address = 0x7fff8b8e3790
Char c:     value = Z,              address = 0x7fff8b8e3787
Pointer e:  value = 0x7fff8b8e378c, address = 0x7fff8b8e3798

Child After Fork:
Integer a:  value = 123456,         address = 0x7fff8b8e378c
Float b:    value = 123.456001,     address = 0x7fff8b8e3790
Char c:     value = Z,              address = 0x7fff8b8e3787
Pointer e:  value = 0x7fff8b8e378c, address = 0x7fff8b8e3798

Parent After Fork:
Integer a:  value = 123456,         address = 0x7fff8b8e378c
Float b:    value = 123.456001,     address = 0x7fff8b8e3790
Char c:     value = Z,              address = 0x7fff8b8e3787
Pointer e:  value = 0x7fff8b8e378c, address = 0x7fff8b8e3798

推荐答案

子进程具有父地址空间的副本.在现代系统中,地址是虚拟化的,因此任何特定的指针地址都可以在一个进程中映射到与另一个进程中不同的物理地址.

The child process has a copy of the parent address space. In modern systems addresses are virtualized, so that any particular pointer address can map in one process to a different physical address than it does in another process.

如果我更改孩子中的变量会怎样?父母和孩子都会改变吗?

What happens if I change a variable in the child? Will it change in both parent and child?

子代有其自己的变量副本,因此在子代中更改变量不会影响父代中变量的值.

The child has its own copy of the variable, so changing a variable in the child will not affect the value of the variable in the parent.

如果没有,我如何更改该地址中的值,而父母和孩子的地址都相同.

If not, how am I able to change the value in that address while the addres is the same for both parent and child.

这是由于两个进程中的相同地址映射到不同的物理地址.

This is due to the same address in both processes mapping to different physical addresses.

这篇关于fork()之后地址,值和指针会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆