Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630347
  • 博文数量: 85
  • 博客积分: 1306
  • 博客等级: 中尉
  • 技术积分: 990
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-28 11:23
个人简介

嵌入式视频行业。

文章分类

全部博文(85)

文章存档

2015年(7)

2014年(5)

2013年(41)

2012年(11)

2011年(1)

2010年(3)

2008年(17)

分类: LINUX

2013-04-06 22:40:29

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

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);


发送端代码:

点击(此处)折叠或打开

  1. /******************************************************************************

  2.            This is Iskey.

  3.  ******************************************************************************/
  4. /**
  5.  * @file test_push.c
  6.  * @brief test mem-share. work in push model.
  7.  * @author iskey@outlook.com
  8.  * @version Initial Draft
  9.  * @note none
  10.  * @date 2013/3/19
  11.  */
  12. /******************************************************************************
  13.  * Function List :
  14.  * main
  15.  * History :
  16.  * 1.Date : 2013/3/19
  17.  * Author : iskey
  18.  * Modification: Created file
  19.  *
  20. ******************************************************************************/

  21. /** external variables */

  22. /** external routine prototypes */

  23. /** internal routine prototypes */

  24. /** project-wide global variables */

  25. /** module-wide global variables */

  26. /** constants */

  27. /** macros */

  28. /** routines' implementations */

  29. #include <stdio.h>
  30. #include <stdlib.h>

  31. #include "mem-share.h"

  32. void main()
  33. {
  34.     SHM_FD fd,fd1;

  35.     shm_init(0);
  36.     fd= shm_chn_add(800000);
  37.     if(-1== fd){
  38.         printf("shm channel add error!\n");
  39.         return;
  40.     }
  41.     fd1= shm_chn_add(2048);
  42.     if(-1== fd1){
  43.         printf("shm channel add error!\n");
  44.         return;
  45.     }
  46.     SHARE_BUF_NODE *tmp;
  47.     tmp= malloc(sizeof(SHARE_BUF_NODE));

  48.     tmp->share_size= sizeof("WWffffffffffffffffWXXXJKOJKOO");
  49.     tmp->share_pt= "WWffffffffffffffffWXXXJKOJKOO";
  50.     
  51.     int test= 20000;
  52.     while(test--)
  53.     {
  54.         shm_push(fd, tmp);
  55.         printf("share channel %d is pushed successfully by share size= %d times=%d\n",fd,tmp->share_size, test);
  56.     }

  57. // tmp->share_size= sizeof("yyyyyyyyyyyyycccccccccc");
  58. // tmp->share_pt= "yyyyyyyyyyyyycccccccccc";
  59. // shm_push(fd1, tmp);
  60. // shm_release(fd1);
  61. // printf("share channel %d is pushed successfully by share size= %d\n",fd1,tmp->share_size);

  62.     getchar();
  63. }
接收端代码:

点击(此处)折叠或打开

  1. /******************************************************************************

  2.            This is Iskey.

  3.  ******************************************************************************/
  4. /**
  5.  * @file test_pull.c
  6.  * @brief test mem-share. work in pull model
  7.  * @author iskey@outlook.com
  8.  * @version Initial Draft
  9.  * @note none
  10.  * @date 2013/3/19
  11.  */
  12. /******************************************************************************
  13.  * Function List :
  14.  * main
  15.  * History :
  16.  * 1.Date : 2013/3/19
  17.  * Author : iskey
  18.  * Modification: Created file
  19.  *
  20. ******************************************************************************/

  21. /** external variables */

  22. /** external routine prototypes */

  23. /** internal routine prototypes */

  24. /** project-wide global variables */

  25. /** module-wide global variables */

  26. /** constants */

  27. /** macros */

  28. /** routines' implementations */

  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "mem-share.h"

  33. void main()
  34. {
  35.     SHM_FD fd,fd1;

  36.     shm_init(1);
  37.     fd= shm_chn_attach();
  38. // fd1= shm_chn_attach();

  39.     SHARE_BUF_NODE *r_tmp;
  40.     r_tmp= malloc(sizeof(SHARE_BUF_NODE));
  41.     
  42.     char *pull_buf;
  43.     pull_buf= malloc(500);

  44.     int test= 20000;
  45.     while(test--)
  46.     {
  47.         int handle= shm_pull(fd, r_tmp);
  48.         memcpy((void *)pull_buf, (const void *)(r_tmp->share_pt), (r_tmp->share_size));
  49.         printf("sahre mem buffer max size is %d\n",r_tmp->max_buf_size);
  50.         printf("share mem buffer size is %d\n",r_tmp->share_size);
  51.         printf("share mem buffer is %s times=%d\n",pull_buf, test);
  52.         shm_release(handle);
  53.     }

  54. // shm_pull(fd1, r_tmp);
  55. // printf("sahre mem buffer max size is %d\n",r_tmp->max_buf_size);
  56. // printf("share mem buffer size is %d\n",r_tmp->share_size);
  57. // printf("share mem buffer is %s\n",(unsigned char *)(r_tmp->share_pt));
  58. // shm_release(fd1);

  59.     getchar();
  60. }


阅读(7921) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~