Create a virtual mapping in the address space of another process
#include <sys/mman_peer.h>
void *mmap_peer( pid_t pid,
void *addr,
size_t len,
int prot,
int flags,
int fd,
off_t off );
void *mmap64_peer( pid_t pid,
void *addr,
size_t len,
int prot,
int flags,
int fd,
off64_t off );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The mmap_peer() function maps a region within the object of the specified process ID beginning at off and continuing for len into the address space, and returns the location. The mmap64_peer() function is a large-file support version of mmap() and is useful only on 32-bit architectures.
The address returned by mmap_peer() is valid only in the context of the process pid.
The target process can:
The address of the mapped-in object, or MAP_FAILED if an error occurred (errno is set).
In addition to the errno codes set by mmap(), the following can be set:
The following example forks into parent and child process, and the parent process waits for the child process to terminate. The child process does the following:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <process.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
pid_t pid;
pid_t parent_pid;
char buff[256];
const int mmap_len = 4096;
parent_pid = getpid();
pid = fork();
assert( pid != -1);
if( pid == 0 ) {
/* This is the child process */
void *ptr, *old_ptr;
int ret;
int failed;
failed = 0;
old_ptr = mmap_peer( parent_pid, 0, mmap_len, PROT_READ | PROT_WRITE,
MAP_SHARED, NOFD, 0);
if (old_ptr == MAP_FAILED) {
SPRNT( buff, sizeof(buff), "mmap_peer failed (errno %d)", errno);
failed++;
}
if (!failed) {
ret = munmap_peer(parent_pid, old_ptr, mmap_len);
if ( ret == -1 ) {
SPRNT( buff, sizeof(buff), "munmap_peer failed (errno %d)", errno);
failed++;
}
}
} else {
/* This is the parent process */
wait(NULL);
}
return EXIT_SUCCESS;
}
mmap_peer() is QNX Neutrino; mmap64_peer() is Large-file support
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |