At the end I tried the pidfd_getfd syscall that is working really fine and giving me back a "clone" fd of an fd in that was opened from another process. I tested it opening a text file in the first process and after cloning the fd , I could really read the file also in the second process.
Now the weird thing:
1) In the first process I allocate- a huge page, then get the fd
2) In the second process I get my "clone" fd and do an mmap, it works but if I write on that memory, the first process cannot see what I wrote
int second_process(int remote_pid, int remote_mem_fd) {
printf("remote_pid %d remote_mem_fd %d\n", remote_pid, remote_mem_fd);
int pidfd = syscall(__NR_pidfd_open, remote_pid, 0);
int my_mem_fd = syscall(438, pidfd, remote_mem_fd, 0);
printf("my_mem_fd %d\n", my_mem_fd); // This is nice
int flags = MAP_SHARED | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT);
uint64_t* addr = (uint64_t*) mmap(NULL, 1024 * 1024 * 1024, PROT_READ|PROT_WRITE, flags, my_mem_fd, 0);
if (addr == -1)
perror("mmap");
*addr = 0x0101010102020202;
}