#include <stdio.h>
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
|
#include "src2/shmh.h"
|
|
#include <chrono>
|
|
static int idx = 0;
|
static int get_memfd(void* args, struct fd_msg* msg){
|
int fd = basic_shm_create("shm", 128*1024*1024);
|
if (fd > 0){
|
// struct stat st;
|
// if (fstat (fd, &st))
|
// {
|
// return -1;
|
// }
|
// printf("st size %ld\n", st.st_size);
|
if (msg) msg->fd = fd;
|
|
unsigned char* addr;
|
if (basic_shm_mmap(fd, &addr) > 0){
|
sprintf((char*)addr, "get_memfd set msg %d", idx++);
|
basic_shm_unmmap(fd, &addr);
|
}
|
return fd;
|
}
|
return -1;
|
}
|
|
int main(int argc, char const *argv[])
|
{
|
int sock = unix_domain_client_fd("/tmp/memfd_shm.sock", 0);
|
if (sock <= 0){
|
printf("unix_domain_client_fd failed\n");
|
return -1;
|
}
|
|
int count = 0;
|
|
using namespace std::chrono;
|
auto s = steady_clock::now();
|
auto e = s;
|
|
while (true) {
|
|
int fd = get_memfd(NULL, NULL);
|
if (fd < 0){
|
printf("memfd %d no exist\n", fd);
|
continue;
|
}
|
// struct stat st;
|
// if (fstat (fd, &st))
|
// {
|
// return -1;
|
// }
|
// printf("st size %ld\n", st.st_size);
|
|
count++;
|
|
int ret = sendfd(sock, &fd, 1);
|
|
close(fd);
|
|
usleep(2);
|
|
// e = steady_clock::now();
|
// if (duration_cast<milliseconds>(e-s).count() > 1000){
|
// s = e;
|
// printf("======>> around 1 s recv count %d\n", count);
|
// _exit(0);
|
// count = 0;
|
// }
|
}
|
|
close(sock);
|
|
return 0;
|
}
|