繁体   English   中英

在Linux中从一个进程到另一个进程的地址映射

[英]address mapping from one process to another in Linux

能做到吗? 进程A执行x = malloc(...) x是进程A的地址空间(堆)中的虚拟地址。 我想要一个系统调用,该调用将x并从进程A的地址空间中取消映射,并将其映射到进程B的虚拟地址空间virt_to_phys()phys_to_virt()工作? virt_to_phys()将在进程A的上下文中完成,而phys_to_virt()在进程B的上下文中完成。 我说得通吗 我没有深入研究Linux内核中的地址映射机制。

您可以使用POSIX共享内存来执行此操作

流程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

暂无
暂无

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

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