多进程通信还是很不错的
服务端(取ring数据)
static const char *_MSG_POOL = "MSG_POOL";
static const char *RING_NAME = "t_ring";
struct rte_ring *t_ring;
struct rte_mempool *message_pool;
volatile int quit = 0;
const char *tmp="abc!";
int main(int argc, char **argv){
const unsigned flags = 0;
const unsigned ring_size = 64;
const unsigned pool_size = 1024;
const unsigned pool_cache = 32;
const unsigned priv_data_sz = 0;
int ret;
ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
if (rte_eal_process_type() == RTE_PROC_PRIMARY){
//flags为0:多生产者多消费者
//flags为RING_F_SP_ENQ:单生产者
//flags为RING_F_SC_DEQ:单消费者
t_ring = rte_ring_create(RING_NAME, ring_size, rte_socket_id(), flags);
//pool_size为mempool元素个数
//sizeof(tmp)为元素大小
message_pool = rte_mempool_create(_MSG_POOL, pool_size,
sizeof(tmp), pool_cache, priv_data_sz,
NULL, NULL, NULL, NULL, rte_socket_id(), flags);
} else {
rte_exit(EXIT_FAILURE, "No primary\n");
}
if (t_ring == NULL)
rte_exit(EXIT_FAILURE, "Problem getting ring\n");
if (message_pool == NULL)
rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
RTE_LOG(INFO, APP, "Finished Process Init.\n");
for (;;) {
void *msg;
if (rte_ring_dequeue(t_ring, &msg) < 0){
continue;
}
printf("core %u: ring %d=%d,Received '%s'\n", rte_lcore_id(), rte_ring_count(t_ring),rte_ring_get_capacity(t_ring),(char *)msg);
rte_mempool_put(message_pool, msg);
sleep(1);
}
rte_eal_mp_wait_lcore();
return 0;
}
客户端(发ring数据)
static const char *_MSG_POOL = "MSG_POOL";
static const char *RING_NAME = "t_ring";
struct rte_ring *t_ring;
struct rte_mempool *message_pool;
const char *tmp="abc!";
int main(int argc, char **argv){
int ret;
ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
if (rte_eal_process_type() == RTE_PROC_SECONDARY){
t_ring = rte_ring_lookup(RING_NAME);
message_pool = rte_mempool_lookup(_MSG_POOL);
} else {
rte_exit(EXIT_FAILURE, "No secondary\n");
}
if (t_ring == NULL)
rte_exit(EXIT_FAILURE, "Problem getting ring\n");
if (message_pool == NULL)
rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
RTE_LOG(INFO, APP, "Finished Process Init.\n");
for (;;) {
if(rte_ring_full(t_ring)){
sleep(3);
continue;
}
void *msg = NULL;
if (rte_mempool_get(message_pool, &msg) < 0){
RTE_LOG(INFO, APP, "get mempool err\n");
continue;
}
snprintf((char *)msg, sizeof(tmp), "%s", tmp);
if (rte_ring_enqueue(t_ring, msg) < 0) {
rte_mempool_put(message_pool, msg);
}
}
return 0;
}