[转]进程间通过共享内存方式传输大数据

转自:blog.chinaunix.net/uid-20682890-id-3567309.html

通过共享内存和信号量的乒乓机制实现大数据块在不同程序之间的数据传输。

zips:https://nodeload.github.com/iskey/mem-share/zip/master

git:https://github.com/iskey/mem-share.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();
    }





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值