Test shared memory
write shared memory on vm B
xzl@xzl-test-second-vm:~/Xiaotest$ sudo ./write_shm abcdefg
The device address 0xe0000000 (lenth 268435456)
can be accessed over
logical address 0x7fddc081c000
*access_address = 1718121316
write a
write b
write c
write d
write e
write f
write g
read shared memory on vm A
xzl@xzl-test-first-vm:~/Xiaotest$ sudo ./read_shm 6
The device address 0xe0000000 (lenth 268435456)
can be accessed over
logical address 0x7f94ca9e3000
*access_address = 1684234849
read a
read b
read c
read d
read e
read f
write_shm.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#define UIO_DEV "/dev/uio0"
#define UIO_ADDR "/sys/class/uio/uio0/maps/map1/addr"
#define UIO_SIZE "/sys/class/uio/uio0/maps/map1/size"
static char uio_addr_buf[16], uio_size_buf[16];
int main(int argc, char ** argv)
{
if(argc != 2){
printf("<USAGE: cmmand value>\n");
return 1;
}
int uio_fd, addr_fd, size_fd;
int uio_size;
int str_len = strlen(argv[1]);
void* uio_addr, *access_address;
int i,j;
uio_fd = open(UIO_DEV, /*O_RDONLY*/O_RDWR);
addr_fd = open(UIO_ADDR, O_RDONLY);
size_fd = open(UIO_SIZE, O_RDONLY);
if( addr_fd < 0 || size_fd < 0 || uio_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
exit(-1);
}
read(addr_fd, uio_addr_buf, sizeof(uio_addr_buf));
close(addr_fd);
read(size_fd, uio_size_buf, sizeof(uio_size_buf));
close(size_fd);
uio_addr = (void*)strtoul(uio_addr_buf, NULL, 0);
uio_size = (int)strtol(uio_size_buf, NULL, 0);
access_address = mmap(NULL, uio_size, PROT_READ | PROT_WRITE,
MAP_SHARED, uio_fd, 1*getpagesize());
if ( access_address == (void*) -1) {
fprintf(stderr, "mmap: %s\n", strerror(errno));
exit(-1);
}
printf("The device address %p (lenth %d)\n"
"can be accessed over\n"
"logical address %p\n", uio_addr, uio_size, access_address);
printf("*access_address = %u\n",*((unsigned long*) access_address));
for(i=0; i<str_len; i++) {
(*((char *)(access_address + i))) = argv[1][i];
printf("write %c\n",argv[1][i]);
}
munmap(access_address, uio_size);
close(uio_fd);
return 0;
}
read_shm.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#define UIO_DEV "/dev/uio0"
#define UIO_ADDR "/sys/class/uio/uio0/maps/map1/addr"
#define UIO_SIZE "/sys/class/uio/uio0/maps/map1/size"
static char uio_addr_buf[16], uio_size_buf[16];
int main(int argc, char ** argv)
{
if(argc != 2){
printf("<USAGE: cmmand count>\n");
return 1;
}
int uio_fd, addr_fd, size_fd;
int uio_size;
int str_len = (int)strtol(argv[1],NULL,0);
void* uio_addr, *access_address;
int i,j;
uio_fd = open(UIO_DEV, /*O_RDONLY*/O_RDWR);
addr_fd = open(UIO_ADDR, O_RDONLY);
size_fd = open(UIO_SIZE, O_RDONLY);
if( addr_fd < 0 || size_fd < 0 || uio_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
exit(-1);
}
read(addr_fd, uio_addr_buf, sizeof(uio_addr_buf));
close(addr_fd);
read(size_fd, uio_size_buf, sizeof(uio_size_buf));
close(size_fd);
uio_addr = (void*)strtoul(uio_addr_buf, NULL, 0);
uio_size = (int)strtol(uio_size_buf, NULL, 0);
access_address = mmap(NULL, uio_size, PROT_READ | PROT_WRITE,
MAP_SHARED, uio_fd, 1*getpagesize());
if ( access_address == (void*) -1) {
fprintf(stderr, "mmap: %s\n", strerror(errno));
exit(-1);
}
printf("The device address %p (lenth %d)\n"
"can be accessed over\n"
"logical address %p\n", uio_addr, uio_size, access_address);
printf("*access_address = %u\n",*((unsigned long*) access_address));
for(i=0; i<str_len; i++) {
printf("read %c\n",*((char *)(access_address+i)));
}
munmap(access_address, uio_size);
close(uio_fd);
return 0;
}