Hi all,
is it possible to map a region on memory copy on write using this idea?
I have an invalid argument in the mmap call.
Why?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define MAXPATH 255
int main (int argc, char **argv)
{
char *s,*x;
int i;
int addr;
int fd;
int size=40960;
char file_memory[MAXPATH];
pid_t p=getpid();
s=malloc(size);
sprintf(file_memory,"/proc/%d/mem",(int)p);
fd = open(file_memory,O_RDONLY);
if (fd < 0) {
perror(file_memory);
exit(-1);
}
printf ("start %s\n",file_memory);
printf("s = %p\n",s);
memset(s, 'X', size);
//lseek(fd,(int)s,SEEK_SET);
//read(fd,&addr,1);
//printf("%c", addr);
x = mmap(0,size,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,(int)s);
if ((int)(x)>0) {
printf("x = %p\n",x);
*x='P';
for (i=0;i<40; i++)
printf("sval=%c;xval=%c;",*(x+i),*(s+i));
} else {
printf("%s\n",strerror(errno));
}
return 0;
}