Hi, how can I read shared memory from another process? I got this code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main( int argc, char** argv )
{
int fd;
unsigned* addr;
fd = shm_open( "/mi_memoria", O_RDWR | O_CREAT, 0777 );
if( fd == -1 ) {
fprintf( stderr, "Open failed:%s\n", strerror( errno ) );
return EXIT_FAILURE;
}
if( ftruncate( fd, sizeof( *addr ) ) == -1 ) {
fprintf( stderr, "ftruncate: %s\n", strerror( errno ) );
return EXIT_FAILURE;
}
addr = mmap( 0, sizeof( *addr ), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
if( addr == MAP_FAILED ) {
fprintf( stderr, "mmap failed: %s\n", strerror( errno ) );
return EXIT_FAILURE;
}
printf( "Map addr is 0x%08x\n", addr );
*addr = 'A';
close( fd );
return EXIT_SUCCESS;
}
I want to read *addr from another process.