home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / linux / 6844 < prev    next >
Encoding:
Text File  |  1992-07-28  |  2.0 KB  |  83 lines

  1. Path: sparky!uunet!cs.utexas.edu!torn!cunews!revcan!software.mitel.com!sharman!sharman
  2. From: sharman@Software.Mitel.COM (Richard Sharman)
  3. Newsgroups: comp.os.linux
  4. Subject: I can't get mmap to work
  5. Message-ID: <SHARMAN.92Jul28002859@sharman.Software.Mitel.COM>
  6. Date: 28 Jul 92 04:28:59 GMT
  7. Sender: sharman@Software.Mitel.COM
  8. Organization: Mitel. Kanata (Ontario). Canada.
  9. Lines: 72
  10.  
  11. I was trying to use "mmap", and am having problems.
  12.  
  13. I have a simple example, below, which attempts to use mmap on a file,
  14. and simply prints the value returned.  I looked in the source
  15. (linux/mm/mmap.c) and it seemed that the address has to be a valid
  16. address in the data space, at a multiple of 0x1000, and the length
  17. also has to be a multiple of that.
  18.  
  19. The demo program fails on mmap, with errno=1 (EPERM).  This I don't
  20. understand, since I couldn't see any failures of the kind
  21. return(-EPERM).   I tried it on the SUN under gdb, and the mmap
  22. succeeded and buff1 looked ok.  (It bombed on the printf though!)
  23.  
  24. System: 0.96c-pl2   (and GCC 2.2.2 of course!)
  25.  
  26. Here is the code:
  27. ---------------------
  28. /* attempt to use mmap */
  29.  
  30. #include <sys/types.h>
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <sys/mman.h>
  34. #include <fcntl.h>
  35. #include <errno.h>
  36.  
  37. main(int argc, char **argv)
  38. {
  39.  
  40.   caddr_t file1, file2 ;
  41.   int      fd1, fd2 ;
  42.   int      save_errno ;
  43.   
  44. #define LEN1 2048
  45.   caddr_t buff1 ;
  46.  
  47.   buff1 = (caddr_t) memalign(0x1000, LEN1) ;
  48.   if ( buff1 == NULL ) {
  49.     perror("malloc failed") ;
  50.     exit(2) ;
  51.   }
  52.  
  53.  
  54.   if ( argc !=2 ) {
  55.     fprintf(stderr, "no file specified\n") ;
  56.     exit(2) ;
  57.   }
  58.  
  59.   fd1 = open(argv[1], O_RDONLY) ;
  60.   if ( fd1 < 0 ) {
  61.     perror("open failed") ;
  62.     exit(2) ;
  63.   }
  64.   file1 = mmap(buff1, LEN1, PROT_READ, MAP_SHARED|MAP_FIXED, fd1, 0) ;
  65.   save_errno = errno ;
  66.   perror("") ;
  67.   printf("mmap returned %x  errno was %d\n", file1, save_errno) ;
  68.  
  69.   exit(0) ;
  70. }
  71.   
  72. ----------------------
  73. and this is the output:
  74. ----------------------
  75. ~/test% demo1 demo1.c
  76. Operation not permitted
  77. mmap returned ffffffff  errno was 1
  78. ~/test%
  79.  
  80.  
  81. Any help appreciated,
  82. Richard
  83.