#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <sys/types.h>
#define MEMSIZE ((long)2*1024*1024*1024)
int main (void)
{
int fd1, fd2, fd3, fd4, fd5, err;
char *mem1, *mem2, *mem3, *mem4, *mem5;
printf ("sizeof(void *) = %d\n", sizeof(void *));
printf ("sizeof(int) = %d\n", sizeof(int));
printf ("sizeof(long) = %d\n", sizeof(long));
printf ("memsize = %ld\n", MEMSIZE);
err = shm_unlink ("bank1");
printf ("shm_unlink bank1 = %d\n", err);
err = shm_unlink ("bank2");
printf ("shm_unlink bank2 = %d\n", err);
err = shm_unlink ("bank3");
printf ("shm_unlink bank3 = %d\n", err);
err = shm_unlink ("bank4");
printf ("shm_unlink bank4 = %d\n", err);
err = shm_unlink ("bank5");
printf ("shm_unlink bank5 = %d\n", err);
fd1 = shm_open ("bank1", O_RDWR|O_CREAT);
printf ("fd1 = %d\n", fd1);
fd2 = shm_open ("bank2", O_RDWR|O_CREAT);
printf ("fd2 = %d\n", fd2);
fd3 = shm_open ("bank3", O_RDWR|O_CREAT);
printf ("fd3 = %d\n", fd3);
fd4 = shm_open ("bank4", O_RDWR|O_CREAT);
printf ("fd4 = %d\n", fd4);
fd5 = shm_open ("bank5", O_RDWR|O_CREAT);
printf ("fd5 = %d\n", fd5);
err = ftruncate (fd1, MEMSIZE);
printf ("ftruncate bank1 = %d\n", err);
err = ftruncate (fd2, MEMSIZE);
printf ("ftruncate bank2 = %d\n", err);
err = ftruncate (fd3, MEMSIZE);
printf ("ftruncate bank3 = %d\n", err);
err = ftruncate (fd4, MEMSIZE);
printf ("ftruncate bank4 = %d\n", err);
err = ftruncate (fd5, MEMSIZE);
printf ("ftruncate bank5 = %d\n", err);
mem1 = mmap (NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);
printf ("mmap bank1 = %p\n", mem1);
mem2 = mmap (NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
printf ("mmap bank2 = %p\n", mem2);
mem3 = mmap (NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd3, 0);
printf ("mmap bank3 = %p\n", mem3);
mem4 = mmap (NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd4, 0);
printf ("mmap bank4 = %p\n", mem4);
mem5 = mmap (NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd5, 0);
printf ("mmap bank5 = %p\n", mem5);
memset (mem1, 0xAA, MEMSIZE);
memset (mem2, 0xBB, MEMSIZE);
memset (mem3, 0xCC, MEMSIZE);
memset (mem4, 0xDD, MEMSIZE);
memset (mem5, 0xEE, MEMSIZE);
}
Output:
sizeof(void *) = 8
sizeof(int) = 4
sizeof(long) = 8
memsize = 2147483648
shm_unlink bank1 = 0
shm_unlink bank2 = 0
shm_unlink bank3 = 0
shm_unlink bank4 = 0
shm_unlink bank5 = 0
fd1 = 3
fd2 = 4
fd3 = 5
fd4 = 6
fd5 = 7
ftruncate bank1 = 0
ftruncate bank2 = 0
ftruncate bank3 = 0
ftruncate bank4 = 0
ftruncate bank5 = 0
mmap bank1 = 0x6008000
mmap bank2 = 0x8ff4e000
mmap bank3 = 0x10ff4e000
mmap bank4 = 0x18ff4e000
mmap bank5 = 0x20ff4e000