mmap on Linux-1.3.35

Harald Evensen (min@oti.on.ca)
Thu, 19 Oct 1995 11:49:22 -0400


Hi, a couple of shared memory questions on Linux.
1. Is it true using shmget can give you 1M at most. Is this value
adjustable (e.g. to 4M)?
2. I thought Linux now supports functions like mmap/mprotect.
But I could not get the following test program to work on Linux. I always get address
0xFFFFFFFF back. This code works fine on AIX4.1. I am running Linux1.3.35 ELF on a
486/DX2 box.

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
}