Though somewhat off-topic, I am trying to port our simulation/control/data_acq system 
to RTLinux.  
Key to the success of this endeavor, is the capability of having one process able to 
read/write 
into the process space of another.  In the example provided I can both mmap() and 
read()/write()
/proc on our current platform.  On Linux 2.4.1 (RH7.1) however, neither worked.

A couple of years ago, when I first tried this with RH5.2 (I don't remember the Linux 
version)
I remember seeing a similar (if not identical) disclaimer to what's in the current 
proc(5) 
manpage.  The last sentence gave me hope the capability I was seeking was to be 
implemented
imminently.

  mem    This is not the same as the mem (1:1) device, despite the fact that it has the
         same device numbers.  The /dev/mem device is the physical memory before any 
         address translation is done, but the mem file here is the memory of the 
process
         that accesses it.  This cannot be mmap(2)'ed currently, and will not be until 
a 
         general mmap(2) is added to the kernel.  (This might have happened by the 
time 
         you read this.)

Either Linux can in some form support this and I'm doing something wrong (and the 
documentation
is out-of-date); or it is currently not yet possible through /proc.  

Thank you for taking the time to read this note.  Any help or pointers would be very 
much
appreciated (I hope someone else has crossed this bridge before me).

Dean W. Anneser
Software Engineering Fellow
Real-Time Test Systems
Pratt & Whitney Aircraft 
400 Main St., m.s. 161-05
East Hartford, CT  06108 
email: [EMAIL PROTECTED] 
phone: 860.565.9372    fax: 860.557.3482             
"One test result is worth one thousand expert opinions" - Wernher von Braun



example code:

Use mmap.c to view the parameter iii in program tst.c, while tst.c is
running in background:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= mmap.c =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
 
#define MMAP 1
#define RW   2


void main(int argc, char *argv[])
{
  int i, j, k;
  int iii = 0;
  int *piii;

  char tmp[256];
  int fd;
  caddr_t p;

  struct utsname sysname;
  struct stat statinfo;

  int bytes_read = 0;

  int page_base;
  int page_offset;
  int page_size;

  int pid = 0;
  int addr = 0;
  int use = MMAP;
  int ret = 0;

  int argi = 0;

  while (++argi < argc)
    {
      if (strcmp(argv[argi], "-pid") == 0)
        sscanf(argv[++argi], "%d", &pid);
      else if (strcmp(argv[argi], "-addr") == 0)
        sscanf(argv[++argi], "%x", &addr);
      else if (strcmp(argv[argi], "-use") == 0)
        {
          if (strcmp(argv[++argi], "mmap") == 0)
            use = MMAP;
          else
            use = RW;
        }
      else
        {
          printf("unrecognized option: %s\n", argv[argi]);
          printf("usage: mmap -pid <pid> -addr <addr>\n");
          exit(1);
        }
    }

  uname(&sysname);

  if ((pid == 0) || (addr == 0))
    {
      printf("usage: mmap -pid <pid> -addr <addr>\n");
      exit(1);
    }

  if (strcmp(sysname.sysname, "Linux") == 0)
    sprintf(tmp, "/proc/%d/mem", pid); /* Linux */
  else
    sprintf(tmp, "/proc/%d/as", pid); /* PowerMAX_OS or SunOS */
    
  if ((fd = open(tmp, O_RDWR)) == -1)
    {
      printf("open failure on %s\n", tmp);
      exit(1);
    }

  if (fstat(fd, &statinfo) == -1)
    {
      printf("fstat failed on %s\n", tmp);
      exit(1);
    }
 
  if (use == RW)
    {
      while (bytes_read != -1)
        {
          if ((ret = lseek(fd, addr, SEEK_SET)) == -1)
            {
              printf("lseek() error = %d\n", ret);
              exit(1);
            }
          if ((bytes_read = read(fd, &iii, sizeof(int))) <= 0)
            {
              printf("read() error = %d\n", bytes_read);
              exit(1);
            }
          printf("bytes_read = %d, iii = %d\r", bytes_read, iii);
          fflush(stdout);
          sleep(1);
        }
    }
  else
    {
      page_size = getpagesize();
      page_base = (addr / page_size) * page_size;
      page_offset = addr % page_size;

      if ((p = (caddr_t)mmap(0, page_size, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, 
page_base)) == (caddr_t)-1) 
        {
          printf("mmap failure on %s\n", tmp);
          printf("  statinfo.st_size == %d\n", statinfo.st_size);
          printf("  page_size == %#x\n", page_size);
          printf("  page_base == %#x\n", page_base);
          printf("  page_offset == %#x\n", page_offset);
          perror((char *)0);
          close(fd);
          exit(1);
        }

      piii = (int *)(p + page_offset); 

      while (1)
        {
          printf("iii = %d\r", *piii);
          fflush(stdout);
          sleep(1);
        }
    }
}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= tst.c =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <stdio.h>

int iii, jjj, kkk;

void main()
{
  iii = jjj = kkk = 0;
  while (1)
    {
      iii += 1;
      jjj += 1;
      kkk += 1;
      printf("iii = %d; jjj = %d; kkk = %d\n", iii, jjj, kkk);
      fflush(stdout);
      sleep(1);
    }
}


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Makefile =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

all: mmap tst

%: %.c
        cc -g $(LFLAGS) -o $@ $< 

clean:
        rm mmap tst

mmap: mmap.c

tst: tst.c


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= running =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

/home/anneser/mmap> nm tst | grep iii
08049658 B iii

/home/anneser/mmap> ./tst > tst.out &
[1] 1356

/home/anneser/mmap> ./mmap -pid 1356 -addr 0x08049658 -use mmap
mmap failure on /proc/1356/mem
  statinfo.st_size == 0
  page_size == 0x1000
  page_base == 0x8049000
  page_offset == 0x658
No such device

/home/anneser/mmap> ./mmap -pid 1356 -addr 0x08049658 -use rw  
read() error = -1

/home/anneser/mmap> 

----- End of forwarded message from [EMAIL PROTECTED] -----
-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/

Reply via email to