【问题标题】:Why both the processes(parent & child) point to same code and data segment?为什么两个进程(父子进程)都指向相同的代码和数据段?
【发布时间】:2020-08-06 06:05:15
【问题描述】:

我尝试使用 fork() 创建子进程,并尝试检查子进程的代码和数据段。但是这两个进程都指向同一个位置。

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

static int global_var = 10;

void increment(int *var){
        printf("Incrementing @%p\n", var);
        *var += 1;
}

void decrement(int *var){
        printf("Decrementing @%p\n", var);
        *var -= 1;
}

void do_operation(void (*my_ops)(int *)){

        my_ops(&global_var);
        printf("@Adr=0x%p, Global Val = %d\n", &global_var, global_var);
}


int main(){

        int i, local_var;
        pid_t pid;
        pid = fork();

        switch(pid){

                case -1:
                        printf("Failed to create new Process.\n");
                        break;
                case 0:
                        printf("Calling do_operation(), which is @%p\n", &do_operation);
                        for(i=0; i<5; i++){
                                printf("Child Process with pid(%d), ", getpid());
                                do_operation(&decrement);
                                sleep(1);
                        }
                        break;

                default:
                        printf("Calling do_operation(), which is @%p\n", &do_operation);
                        for(i=0; i<5; i++){
                                printf("Parent Process with pid(%d), ", getpid());
                                do_operation(&increment);
                                sleep(1);
                        }
                        wait(NULL);
                        break;
        }

        return 0;
}

两个进程的代码段和数据段都指向同一个地址。可以看到如下输出。

Calling do_operation(), which is @0x55d37fd34846
Parent Process with pid(2718), Incrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 11
Calling do_operation(), which is @0x55d37fd34846
Child Process with pid(2719), Decrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 9
Parent Process with pid(2718), Incrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 12
Child Process with pid(2719), Decrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 8
Parent Process with pid(2718), Incrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 13
Child Process with pid(2719), Decrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 7
Parent Process with pid(2718), Incrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 14
Child Process with pid(2719), Decrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 6
Parent Process with pid(2718), Incrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 15
Child Process with pid(2719), Decrementing @0x55d37ff35010
@Adr=0x0x55d37ff35010, Global Val = 5

你可以看到两个进程中静态变量的地址是相同的。这是为什么呢?

【问题讨论】:

    标签: c process


    【解决方案1】:

    在大多数现代操作系统上,每个进程都有自己的virtual address space。因此,即使两个进程共享相同的虚拟内存地址,这并不意味着这些虚拟地址映射到相同的物理内存地址。

    当派生一个进程时,虚拟内存页面到物理内存页面的映射可能保持不变,除了为所有内存页面激活copy-on-write。这样,如果一个进程更改了内存页面,两个进程都会在物理内存中获得该内存页面的自己的副本。在这种情况下,两个进程中的虚拟内存地址将保持不变,但物理内存地址将不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2018-11-14
      • 1970-01-01
      • 2014-01-30
      • 2021-12-31
      相关资源
      最近更新 更多