From ohcanada, 2 Weeks ago, written in Plain Text.
  1. /*
  2.   * read_shared.c - a test example for shared memory
  3.   *
  4.   * Had to set `mem=32704k' boot-time kernel parameter (in /etc/lilo.conf)
  5.   * to reserve 64KB (out of 32768kB), at the high end
  6.   *
  7.   * Only maps one page of memory
  8.   * No ipcs/ipcrm functionality ...
  9.   * ... since Linux cannot see it
  10.   *
  11.   * Reads a count from the first byte repeatedly
  12.   * Use in conjunction with `write_shared'
  13.   *
  14.   * Ctrl-C to exit
  15.   *
  16.   */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/mman.h>
  25. #include <signal.h>
  26.  
  27. #define MYADDRESS   (32704 * 0x400) /* Start of reserved memory */
  28. #define MYSLEEP     (10000)     /* Sleep time in microseconds */
  29.  
  30. int fd;
  31. unsigned char *ucp;
  32. size_t page_size;
  33.  
  34. void
  35. cleanup (void)
  36. {
  37.   puts ("\e[?2c");              /* Restore cursor and newline */
  38.  
  39. }                               /* end cleanup */
  40.  
  41. void
  42. errorexit (char *str)
  43. {
  44.   puts (str);
  45.   exit (EXIT_FAILURE);
  46.  
  47. }                               /* end errorexit */
  48.  
  49. void
  50. signal_handler (int signum)
  51. {
  52.   cleanup ();
  53.  
  54.   if (munmap (ucp, page_size) != 0)
  55.     errorexit ("Error unmapping memory");
  56.  
  57.   else if (close (fd) != 0)
  58.     errorexit ("Error closing /dev/mem");
  59.  
  60.   else
  61.   {
  62.     char buffer[80];
  63.  
  64.     sprintf (buffer, "Terminated with signal %d", signum);
  65.     errorexit (buffer);
  66.   }
  67. }                               /* end signal_handler */
  68.  
  69. void
  70. catch_signals (void)
  71. {
  72.   /* Handler for SIGINT & SIGTERM only ... */
  73.   signal (SIGINT, signal_handler);
  74.   signal (SIGTERM, signal_handler);
  75.  
  76. }                               /* end catch_signals */
  77.  
  78. void
  79. init (void)
  80. {
  81.   catch_signals ();
  82.  
  83.   /* Get system page size ... */
  84.   if ((page_size = (size_t) sysconf (_SC_PAGESIZE)) == -1)
  85.     errorexit ("Error getting system page size");
  86.  
  87.   /* Open physical memory character device read only ... */
  88.   if ((fd = open ("/dev/mem", O_RDONLY)) < 0)
  89.     errorexit ("Error opening /dev/mem");
  90.  
  91.   /* Only read mapped page ... */
  92.   if ((ucp = (unsigned char *) mmap (0, page_size,
  93.                                     PROT_READ, MAP_SHARED | MAP_LOCKED,
  94.                                     fd, MYADDRESS)) == MAP_FAILED)
  95.     errorexit ("Error mapping memory");
  96.  
  97.   /* Prepare terminal to show output ... */
  98.   fputs ("\ec", stdout);        /* Clear the screen */
  99.   fputs ("\e[?1c", stdout);     /* No cursor */
  100. }                               /* end init */
  101.  
  102. int
  103. main (void)
  104. {
  105.   unsigned char count;
  106.   unsigned int last = -1;       /* Running count can never be this ... */
  107.  
  108.   init ();
  109.  
  110.   printf ("Reading shared memory at (physical) address 0x%08X\n", MYADDRESS);
  111.   puts ("Enter `Ctrl-C' to exit");
  112.  
  113.   while (1)                     /* Forever ... */
  114.   {
  115.     if ((count = *ucp) != last) /* Counter has changed ... */
  116.     {
  117.       printf ("\015%02X", count);
  118.       fflush (stdout);
  119.       last = count;
  120.     }
  121.  
  122.     usleep (MYSLEEP);           /* Pause for a while ... */
  123.   }                             /* end while */
  124. }                               /* end main */
captcha