Skip to content

Instantly share code, notes, and snippets.

@Keno
Created May 24, 2020 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keno/5e40ea95267e1b1e8e9c2d58e43ae0d2 to your computer and use it in GitHub Desktop.
Save Keno/5e40ea95267e1b1e8e9c2d58e43ae0d2 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main(void) {
size_t page_size = sysconf(_SC_PAGESIZE);
void* p = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(p != MAP_FAILED);
struct iovec in_iov[2];
struct iovec out_iov[2];
out_iov[0].iov_base = in_iov[0].iov_base = in_iov[1].iov_base = p;
out_iov[0].iov_len = in_iov[0].iov_len = 1;
for (int i = 1; i < 20; ++i) {
for (int j = 1; j < 8; ++j) {
int len = i+j;
in_iov[1].iov_len = len;
out_iov[1].iov_len = len;
out_iov[1].iov_base = p + page_size - i;
errno = 0;
int nbytes = process_vm_readv(getpid(), out_iov, 2, in_iov, 2, 0);
printf("Size %d copy at page_size - %d: %d\n", len, i, nbytes - 1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment