Hi, i am trying to read and write on a particular memory address using mmap function.with my write code i am able to write data on to a particular memory but with my read code i am not able to read data on that particular memory address.i am using MAP_ANONYMOUS in mmap API with out using of file If any one knows about mmap read/write on a particular memory address help me below i am mentioning those read/write files i have used.
Code:
WRITE.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define FILE_LENGTH 64
int main (int argc, char* const argv[])
{
int fd;
int i;
char buffer[10];
void* file_memory;
for(i=0;i<10;i++)
{
buffer[i]='A';
}
write (0, buffer, 10);
file_memory = mmap (0x5F000000,FILE_LENGTH, PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (file_memory == MAP_FAILED) {
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
sprintf((char*) file_memory, "%d\n", 10);
munmap (file_memory, FILE_LENGTH);
return 0;
}
[
Code:
B]READ.c[/B]
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
int i;
int fd;
int *map; /* mmapped array of int's */
map = mmap (0x5F000000, 64, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
printf(" map=%d\n", map);
//map=1;
if (map == MAP_FAILED) {
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
printf("read data %s\n",map);
if (munmap(map, 64) == -1) {
perror("Error un-mmapping the file");
}
return 0;
}
Code:
Code:
out put of read.c
read data
you can say memory sharing between two process only. i wanted to write data on to 0x5F000000 address location and read that same 0x5F000000 address location using mmap with out using file