2019-06-11 03:21:35 +00:00
|
|
|
#include <stdio.h>
|
2019-06-17 09:05:32 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2019-09-18 11:52:04 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
2019-06-11 03:21:35 +00:00
|
|
|
|
2019-06-17 09:05:32 +00:00
|
|
|
#include "compile.h"
|
2019-06-14 07:57:16 +00:00
|
|
|
|
2019-09-18 11:52:04 +00:00
|
|
|
#define PAGE_ALIGN(addr) (((addr) + ((4096)-1)) & (~((4096) - 1)))
|
|
|
|
#define SHM_DEV_NAME ("/dev/shm_dev/shm0")
|
|
|
|
|
|
|
|
void mmap_test_read(void)
|
|
|
|
{
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
unsigned char *pBuf;
|
|
|
|
int fd = open(SHM_DEV_NAME, O_RDWR);
|
|
|
|
|
|
|
|
if(fd == -1) {
|
|
|
|
fprintf(stderr, "Open device %s error: %s\n", SHM_DEV_NAME, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pBuf = (unsigned char *)mmap(NULL, PAGE_ALIGN(1024 * 1024), PROT_READ, MAP_SHARED | MAP_LOCKED, fd, 0);
|
|
|
|
|
|
|
|
if(pBuf == NULL) {
|
|
|
|
fprintf(stderr, "mmap memory %u error: %s\n", PAGE_ALIGN(1024 * 1024), strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
fprintf(stdout, "[%04u]: %s\n", cnt++, (char *)pBuf);
|
|
|
|
sleep(1);
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mmap_test_write(void)
|
|
|
|
{
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
unsigned char *pBuf;
|
|
|
|
int fd = open(SHM_DEV_NAME, O_RDWR);
|
|
|
|
|
|
|
|
if(fd == -1) {
|
|
|
|
fprintf(stderr, "Open device %s error: %s\n", SHM_DEV_NAME, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pBuf = (unsigned char *)mmap(NULL, PAGE_ALIGN(1024 * 1024), PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, 0);
|
|
|
|
|
|
|
|
if(pBuf == NULL) {
|
|
|
|
fprintf(stderr, "mmap memory %u error: %s\n", PAGE_ALIGN(1024 * 1024), strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
memset(pBuf, 0, 512);
|
|
|
|
sprintf(pBuf, "Test %u times of share memory\n", cnt++);
|
|
|
|
sleep(1);
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:05:32 +00:00
|
|
|
/**
|
|
|
|
* @brief 应用程序主函数
|
2019-06-19 06:21:51 +00:00
|
|
|
* @param argc 输入参数个数
|
|
|
|
* @param argv 输入参数详细内容
|
|
|
|
* @return 0
|
2019-06-17 09:05:32 +00:00
|
|
|
*/
|
2019-06-14 07:57:16 +00:00
|
|
|
int main(int argc, char **argv)
|
2019-06-11 03:21:35 +00:00
|
|
|
{
|
2019-06-17 09:05:32 +00:00
|
|
|
int c, optidx = 0;
|
2019-06-19 09:33:11 +00:00
|
|
|
static const struct option long_opts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
2019-09-18 11:52:04 +00:00
|
|
|
{ "shm_read", no_argument, NULL, 'r' },
|
|
|
|
{ "shm_write", no_argument, NULL, 'w' },
|
2019-06-17 09:05:32 +00:00
|
|
|
// TODO 添加其它需要处理的参数配置
|
2019-06-19 09:33:11 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
2019-06-17 09:05:32 +00:00
|
|
|
};
|
|
|
|
|
2019-09-18 11:52:04 +00:00
|
|
|
while((c = getopt_long(argc, argv, "hvrw", long_opts, &optidx)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'v':
|
|
|
|
fprintf(stdout, "User demo version: %s(%s)\n", sGATE_GIT_TAGS, sGATE_GIT_VERS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
//TODO 添加其它必要处理参数过程
|
|
|
|
case 'r':
|
|
|
|
mmap_test_read();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
mmap_test_write();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
fprintf(stdout, "Usage: %s [-h] [-v] ...\n", argv[0]);
|
|
|
|
fprintf(stdout, "options:\n");
|
|
|
|
fprintf(stdout, "\t-v, --version show program version\n");
|
|
|
|
fprintf(stdout, "\t-h, --help print this message\n");
|
|
|
|
break;
|
2019-06-17 09:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-06-11 03:21:35 +00:00
|
|
|
}
|