在我的机器上 test_anon_shared 似乎可以工作,而 test_anon 因“无效参数”错误而失败。知道我哪里出错了吗?
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
void *test_anon(void) {
printf("testing MAP_ANONYMOUS only!\n");
void *mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
perror("test_anon");
abort();
}
}
void *test_anon_shared(void) {
printf("testing MAP_ANONYMOUS | MAP_SHARED!\n");
void *mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (mem == MAP_FAILED) {
perror("test_anon_shared");
abort();
}
}
int main(void) {
printf("test_anon_shared = %p\n", test_anon_shared());
printf("test_anon = %p\n", test_anon());
}