Thanks for any your help,
--min
===============================================================================
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <signal.h>
int shmid;
char *buf;
caddr_t get_shmem(caddr_t addr, int len)
{
int fd; caddr_t memaddr;
if ((fd = open("/dev/zero",O_RDWR)) == -1)
return ((caddr_t) -1);
memaddr = mmap (addr,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (memaddr == (caddr_t)-1)
return ((caddr_t) -1);
close(fd);
return memaddr;
}
void faultHandler(int X)
{
printf("Catch exception\n");
mprotect(buf+4096,1024*1024,PROT_READ|PROT_WRITE);
signal(SIGSEGV,faultHandler); /*re-install our pager. */
}
main()
{
int i;
/*Lets reserve some memory first */
buf = (char *)get_shmem((char *)(4096*4096),1024*1024*1024);
mprotect(buf+4096,1024*1024*1024,PROT_NONE); /* No one touch my memory beyond 4K*/
signal(SIGSEGV,faultHandler); /*install our pager. Now you can touch my memory */
for (i=0;i<4096;i++)
buf[i]= i %127;
printf("No exception happened so far\n");
for (i=4096;i<4096*2;i++)
buf[i]= i %127;
printf("Exception should have happened\n");
#if 0
for (i=0;i<4096*2;i++)
printf("%c ",buf[i]);
#endif
}