root/lib/replace/test/shared_mmap.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* this tests whether we can use a shared writeable mmap on a file -
   2    as needed for the mmap variant of FAST_SHARE_MODES */
   3 
   4 #if defined(HAVE_UNISTD_H)
   5 #include <unistd.h>
   6 #endif
   7 #include <sys/mman.h>
   8 #include <sys/types.h>
   9 #include <sys/stat.h>
  10 #include <fcntl.h>
  11 
  12 #define DATA "conftest.mmap"
  13 
  14 #ifndef MAP_FILE
  15 #define MAP_FILE 0
  16 #endif
  17 
  18 main()
     /* [<][>][^][v][top][bottom][index][help] */
  19 {
  20         int *buf;
  21         int i; 
  22         int fd = open(DATA,O_RDWR|O_CREAT|O_TRUNC,0666);
  23         int count=7;
  24 
  25         if (fd == -1) exit(1);
  26 
  27         for (i=0;i<10000;i++) {
  28                 write(fd,&i,sizeof(i));
  29         }
  30 
  31         close(fd);
  32 
  33         if (fork() == 0) {
  34                 fd = open(DATA,O_RDWR);
  35                 if (fd == -1) exit(1);
  36 
  37                 buf = (int *)mmap(NULL, 10000*sizeof(int), 
  38                                    (PROT_READ | PROT_WRITE), 
  39                                    MAP_FILE | MAP_SHARED, 
  40                                    fd, 0);
  41 
  42                 while (count-- && buf[9124] != 55732) sleep(1);
  43 
  44                 if (count <= 0) exit(1);
  45 
  46                 buf[1763] = 7268;
  47                 exit(0);
  48         }
  49 
  50         fd = open(DATA,O_RDWR);
  51         if (fd == -1) exit(1);
  52 
  53         buf = (int *)mmap(NULL, 10000*sizeof(int), 
  54                            (PROT_READ | PROT_WRITE), 
  55                            MAP_FILE | MAP_SHARED, 
  56                            fd, 0);
  57 
  58         if (buf == (int *)-1) exit(1);
  59 
  60         buf[9124] = 55732;
  61 
  62         while (count-- && buf[1763] != 7268) sleep(1);
  63 
  64         unlink(DATA);
  65                 
  66         if (count > 0) exit(0);
  67         exit(1);
  68 }

/* [<][>][^][v][top][bottom][index][help] */