【问题标题】:address mapping from one process to another in LinuxLinux中从一个进程到另一个进程的地址映射
【发布时间】:2016-03-31 00:26:02
【问题描述】:

这可以吗?进程 A 执行x = malloc(...)。 x 是来自进程 A 的地址空间(堆)的虚拟地址。我想要一个系统调用,它从进程 A 的地址空间获取 x 并取消映射它,并将其映射到进程 B 的虚拟地址空间。 virt_to_phys()phys_to_virt() 会起作用吗? virt_to_phys() 将在进程 A 的上下文中完成,phys_to_virt() 在进程 B 的上下文中完成。我说得有道理吗?我没有深入研究 Linux 内核中的地址映射机制。

【问题讨论】:

    标签: linux


    【解决方案1】:

    你可以使用POSIX shared memory来做到这一点

    流程 1

    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    
    int main()
    {
            unsigned char *shared_byte;
            int shmfd = shm_open("/test_object", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
            ftruncate(shmfd, 1);
            shared_byte = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
            *shared_byte = 123;
            for(;;);
    }
    

    编译运行:

    $ cc proc1.c -o proc1 -lrt
    $ ./proc1 &
    [1] 5381
    

    检查流程图:

    $ pmap 5381
    5381:   ./proc1
    08048000      4K r-x-- proc1
    08049000      4K rw--- proc1
    b7540000      8K rw---   [ anon ]
    b7542000    100K r-x-- libpthread-2.22.so
    b755b000      4K r---- libpthread-2.22.so
    b755c000      4K rw--- libpthread-2.22.so
    b755d000      8K rw---   [ anon ]
    b755f000   1728K r-x-- libc-2.22.so
    b770f000      4K ----- libc-2.22.so
    b7710000      8K r---- libc-2.22.so
    b7712000      4K rw--- libc-2.22.so
    b7713000     12K rw---   [ anon ]
    b7716000     28K r-x-- librt-2.22.so
    b771d000      4K r---- librt-2.22.so
    b771e000      4K rw--- librt-2.22.so
    b7724000      4K rw-s- test_object
    b7725000      4K rw---   [ anon ]
    b7726000      8K r----   [ anon ]
    b7728000      4K r-x--   [ anon ]
    b7729000    136K r-x-- ld-2.22.so
    b774b000      4K r---- ld-2.22.so
    b774c000      4K rw--- ld-2.22.so
    bfe7f000    132K rw---   [ stack ]
     total     2220K
    

    注意有一个4k的test_object映射到进程1的虚拟地址空间。

    流程 2

    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
            unsigned char *shared_byte;
            int shmfd = shm_open("/test_object", O_RDONLY, 0);
            shared_byte = mmap(NULL, 1, PROT_READ, MAP_SHARED, shmfd, 0);
            printf("%d\n", *shared_byte);
            for(;;);
    }
    

    编译运行:

    $ cc proc2.c -o proc2 -lrt
    $ ./proc2 &
    [2] 5397
    123
    

    请注意第一个程序中的“123”值。

    检查流程图:

    $ pmap 5397
    5397:   ./proc2
    08048000      4K r-x-- proc2
    08049000      4K rw--- proc2
    b7555000      8K rw---   [ anon ]
    b7557000    100K r-x-- libpthread-2.22.so
    b7570000      4K r---- libpthread-2.22.so
    b7571000      4K rw--- libpthread-2.22.so
    b7572000      8K rw---   [ anon ]
    b7574000   1728K r-x-- libc-2.22.so
    b7724000      4K ----- libc-2.22.so
    b7725000      8K r---- libc-2.22.so
    b7727000      4K rw--- libc-2.22.so
    b7728000     12K rw---   [ anon ]
    b772b000     28K r-x-- librt-2.22.so
    b7732000      4K r---- librt-2.22.so
    b7733000      4K rw--- librt-2.22.so
    b7738000      4K rw---   [ anon ]
    b7739000      4K r--s- test_object
    b773a000      4K rw---   [ anon ]
    b773b000      8K r----   [ anon ]
    b773d000      4K r-x--   [ anon ]
    b773e000    136K r-x-- ld-2.22.so
    b7760000      4K r---- ld-2.22.so
    b7761000      4K rw--- ld-2.22.so
    bffbf000    132K rw---   [ stack ]
     total     2224K
    

    注意有一个4k的test_object映射到进程2的虚拟地址空间。

    清理

    $ killall proc1
    [1]-  Terminated              ./proc1
    $ killall proc2
    [2]+  Terminated              ./proc2
    $ cat > clean.c << EOF
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main()
    {
            shm_unlink("/test_object");
    }
    EOF
    $ cc clean.c -o clean -lrt
    $ ./clean
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多