通过共享内存和信号量的乒乓机制实现大数据块在不同程序之间的数据传输。
zips:
git:
共享数据结构体:
/* share memory type */
typedef struct __SHARE_BUF_NODE__{
int max_buf_size;//the share buffer size
int share_size;//the share block size in the share buffer
void *share_pt;//the share address
}SHARE_BUF_NODE;
发送端:
1)初始化共享模块
shm_init(0);
2)添加共享通道,其中max_buf_size为共享缓冲区的大小。
SHM_FD shm_chn_add(int max_buf_size),
3)推送共享数据
shm_push(SHM_FD mfd, SHARE_BUF_NODE *node)
接收端:
1)初始化共享模块
shm_init(1);
2)绑定共享通道
SHM_FD shm_chn_attach(void);
3)获取数据
int shm_pull(SHM_FD mfd, SHARE_BUF_NODE *node)
4)释放数据句柄
shm_release(int fd);
发送端代码:
-
/******************************************************************************
-
-
This is Iskey.
-
-
******************************************************************************/
-
/**
-
* @file test_push.c
-
* @brief test mem-share. work in push model.
-
* @author iskey@outlook.com
-
* @version Initial Draft
-
* @note none
-
* @date 2013/3/19
-
*/
-
/******************************************************************************
-
* Function List :
-
* main
-
* History :
-
* 1.Date : 2013/3/19
-
* Author : iskey
-
* Modification: Created file
-
*
-
******************************************************************************/
-
-
/** external variables */
-
-
/** external routine prototypes */
-
-
/** internal routine prototypes */
-
-
/** project-wide global variables */
-
-
/** module-wide global variables */
-
-
/** constants */
-
-
/** macros */
-
-
/** routines' implementations */
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#include "mem-share.h"
-
-
void main()
-
{
-
SHM_FD fd,fd1;
-
-
shm_init(0);
-
fd= shm_chn_add(800000);
-
if(-1== fd){
-
printf("shm channel add error!\n");
-
return;
-
}
-
fd1= shm_chn_add(2048);
-
if(-1== fd1){
-
printf("shm channel add error!\n");
-
return;
-
}
-
SHARE_BUF_NODE *tmp;
-
tmp= malloc(sizeof(SHARE_BUF_NODE));
-
-
tmp->share_size= sizeof("WWffffffffffffffffWXXXJKOJKOO");
-
tmp->share_pt= "WWffffffffffffffffWXXXJKOJKOO";
-
-
int test= 20000;
-
while(test--)
-
{
-
shm_push(fd, tmp);
-
printf("share channel %d is pushed successfully by share size= %d times=%d\n",fd,tmp->share_size, test);
-
}
-
-
// tmp->share_size= sizeof("yyyyyyyyyyyyycccccccccc");
-
// tmp->share_pt= "yyyyyyyyyyyyycccccccccc";
-
// shm_push(fd1, tmp);
-
// shm_release(fd1);
-
// printf("share channel %d is pushed successfully by share size= %d\n",fd1,tmp->share_size);
-
-
getchar();
-
}
接收端代码:
-
/******************************************************************************
-
-
This is Iskey.
-
-
******************************************************************************/
-
/**
-
* @file test_pull.c
-
* @brief test mem-share. work in pull model
-
* @author iskey@outlook.com
-
* @version Initial Draft
-
* @note none
-
* @date 2013/3/19
-
*/
-
/******************************************************************************
-
* Function List :
-
* main
-
* History :
-
* 1.Date : 2013/3/19
-
* Author : iskey
-
* Modification: Created file
-
*
-
******************************************************************************/
-
-
/** external variables */
-
-
/** external routine prototypes */
-
-
/** internal routine prototypes */
-
-
/** project-wide global variables */
-
-
/** module-wide global variables */
-
-
/** constants */
-
-
/** macros */
-
-
/** routines' implementations */
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include "mem-share.h"
-
-
void main()
-
{
-
SHM_FD fd,fd1;
-
-
shm_init(1);
-
fd= shm_chn_attach();
-
// fd1= shm_chn_attach();
-
-
SHARE_BUF_NODE *r_tmp;
-
r_tmp= malloc(sizeof(SHARE_BUF_NODE));
-
-
char *pull_buf;
-
pull_buf= malloc(500);
-
-
int test= 20000;
-
while(test--)
-
{
-
int handle= shm_pull(fd, r_tmp);
-
memcpy((void *)pull_buf, (const void *)(r_tmp->share_pt), (r_tmp->share_size));
-
printf("sahre mem buffer max size is %d\n",r_tmp->max_buf_size);
-
printf("share mem buffer size is %d\n",r_tmp->share_size);
-
printf("share mem buffer is %s times=%d\n",pull_buf, test);
-
shm_release(handle);
-
}
-
-
// shm_pull(fd1, r_tmp);
-
// printf("sahre mem buffer max size is %d\n",r_tmp->max_buf_size);
-
// printf("share mem buffer size is %d\n",r_tmp->share_size);
-
// printf("share mem buffer is %s\n",(unsigned char *)(r_tmp->share_pt));
-
// shm_release(fd1);
-
-
getchar();
-
}
阅读(7959) | 评论(0) | 转发(0) |