简体   繁体   English

在 fork() 之后地址、值和指针会发生什么变化

[英]What happens to address's, values, and pointers after a 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() .我正在研究一个问题,我将在 C 中检查fork()调用之前和之后的值和地址。我的方法是显示变量值和地址,假设在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.如果没有,我如何能够更改该地址中的值,而父和子的地址都相同。

code(for reference):代码(供参考):

#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; 
}

output(for refrence):输出(参考):

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.这是因为两个进程中的相同地址映射到不同的物理地址。

The address is the same, but doesn't refer to the same memory.地址是相同的,但不指向相同的内存。 Each process has its own address space.每个进程都有自己的地址空间。 This is accomplished on modern systems by virtual memory - on older systems it would be accomplished by segment-base addressing or simply swapping (unloading one process and loading the other one in the same region of memory).这是通过虚拟内存在现代系统上实现的——在旧系统上,这将通过基于段的寻址或简单的交换(卸载一个进程并将另一个进程加载到同一内存区域中)来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM