简体   繁体   English

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

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

Can this be done? 能做到吗? Process A does x = malloc(...) . 进程A执行x = malloc(...) x is the virtual address from process A's address space (heap). x是进程A的地址空间(堆)中的虚拟地址。 I want a system call, which takes x and unmap's it from process A's address space, and maps it to the virtual address space of process B. Would virt_to_phys() and phys_to_virt() work? 我想要一个系统调用,该调用将x并从进程A的地址空间中取消映射,并将其映射到进程B的虚拟地址空间virt_to_phys()phys_to_virt()工作? virt_to_phys() would be done in process A's context and phys_to_virt() in process B's context. virt_to_phys()将在进程A的上下文中完成,而phys_to_virt()在进程B的上下文中完成。 Am I making any sense? 我说得通吗 I did not dive deeply into address mapping mechanisms in the Linux kernel. 我没有深入研究Linux内核中的地址映射机制。

You can do this using POSIX shared memory 您可以使用POSIX共享内存来执行此操作

Process 1 流程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(;;);
}

Compile and run: 编译并运行:

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

Inspect the process map: 检查流程图:

$ 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

Notice that there is a 4k test_object mapped to the virtual address space of process 1. 注意,有一个4k test_object映射到进程1的虚拟地址空间。

Process 2 工程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(;;);
}

Compile and run: 编译并运行:

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

Notice the "123" value is there from the first program. 注意第一个程序的“ 123”值在那里。

Inspect the process map: 检查流程图:

$ 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

Notice that there is a 4k test_object mapped to the virtual address space of process 2. 请注意,有一个4k test_object映射到进程2的虚拟地址空间。

Cleanup 清理

$ 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.

相关问题 Linux:通过一个进程从串行端口读取数据,然后通过另一个进程写入数据 - Linux: Read data from serial port with one process and write to it with another 如何在Linux上测量从一个核心到另一个核心的进程迁移时间? - How to measure process migration time from one core to another on linux? 如何从Linux中的另一个进程触发进程? - How to trigger a process from another process in Linux? Android应用程序到Linux的进程映射 - Android app to linux process mapping malloc什么时候处理虚拟地址到物理地址的映射? - When does malloc handle the mapping from virtual address to physics one? Linux如何在后台进程中运行cat并彼此取消链接 - Linux how to run cat and unlink after one another in background process 在Linux中,我正在寻找一种方法使一个进程发出另一个信号,并带有阻塞 - In Linux, I'm looking for a way for one process to signal another, with blocking 将物理地址映射到虚拟地址linux - Mapping physical addresses to virtual address linux 自动将一个Linux系统连接到另一个 - Automate connection from one linux system to another 在Linux中将内容从一个文件追加到另一个文件 - Append content from one file to another in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM