A while back someone asked how to setup shared memory.  
I passed along a description of how I setup 3 Meg of shared
memory starting at 125 Meg and going to 128 Meg.
I have provided it below so that it may help you 
determine the problem.  

The problem as you have described it sounds like you may 
have changed the amount of shared memory you would like 
to use without changing the definition of how much you want
to use in all the necessary locations.

1.) Definition of the shared memory pointer in user space
2.) Definition of the shared memory pointer in kernel space
3.) Definition of the shared memory in lilo.conf

Make sure that all of these definitions are declared for the 
same amount of shared memory.  See below for an example.


I hope this helps.
Rich



> Here are my notes on how I setup and use shared memory.
> 
> ----- sh_mem.h -----
> 
> I created a header file called sh_mem.h that defines where
> the shared memory will begin.  
> 
> #define SH_MEM_BASE_ADDR (125 * 0x100000)
> 
> This value is three megs below the top of physical memory (128M).
> 
> It also defines the data structure for the variables I
> want to have in shared memory.
> 
> typedef struct
> {
>    char variable1[255];
>    int  variable2;
>    long variable3;
> ....
> 
> } RUN_DATA;
> 
> I then define a function that gets the physical address and returns
> that address in the form of a pointer to my structure.
>  
> RUN_DATA *get_sh_mem_ptr(void);
> 
> 
> ---- sh_mem.c ----
> 
> Here is the routine that returns the physical address in memory
> in the form of a pointer to my structure.
> 
> #include <unistd.h>                     /* open() */
> #include <fcntl.h>                      /* O_RDWR */
> #include <stdlib.h>                     /* sizeof() */
> #include <stdio.h>
> #include <sys/mman.h>                   /* mmap(), PROT_READ, MAP_FILE */
> #include "sh_mem.h"                     /* RUN_DATA */
> 
> RUN_DATA *ptr;
> 
> RUN_DATA *get_sh_mem_ptr(void)
> {
>    int fd;
> 
>    if ((fd = open("/dev/mem", O_RDWR)) < 0)
>    {
>       printf("sh_mem.c: Unable to open /dev/mem.\n\n");
>       exit(-1);
>    }
>    ptr = (RUN_DATA *) mmap(0, sizeof(RUN_DATA), PROT_READ | PROT_WRITE,
>                            MAP_FILE | MAP_SHARED, fd, SH_MEM_BASE_ADDR);
> 
>    if (ptr == MAP_FAILED)
>    {
>       printf("sh_mem.c: Unable to map shared memory.\n\n");
>       close(fd);
>       exit(-1);
>    }
>    close(fd);
>    return(ptr);
> }
> 
> 
> ---- user space program ----
> 
> #include "sh_mem.h"
> 
> extern RUN_DATA *ptr;
> 
> int main(void)
> {
> ....
> 
>    ptr = get_sh_mem_ptr();         /* init struct pointer to shared mem addr */
> ....
> 
>    strcpy(ptr->variable1, string); /* copy string to my struct in shared mem */
>    temp1 = ptr->variable2;         /* get a value from struct in shared mem  */
>    ptr->variable3 = temp2;         /* set a value in struct in shared mem    */
> ....
> }
> 
> 
> 
> ---- real time module ----
> 
> #include "sh_mem.h
> 
> RUN_DATA *ptr;      /* define the structure pointer */
> 
> int init_module(void)  /* real time module inti routine */
> {
>  ptr = (RUN_DATA *) SH_MEM_BASE_ADDR; /*init struct ptr to shared mem addr */
> ....
> 
>    strcpy(ptr->variable1, string); /* copy string to my struct in shared mem */
>    temp1 = ptr->variable2;         /* get a value from struct in shared mem  */
>    ptr->variable3 = temp2;         /* set a value in struct in shared mem    */
> ....
> 
> }
> 
> 
> Before all of this will work you need to add a line to /etc/lilo.conf
> 
> append="mem=125M"
> 
> This line is added at the end of the list before the lines
> that define the boot image.  This is what tells Linux to reserve
> the top 3 Megs of physical memory.
> 
> example:
> 
> boot=/dev/hda
> map=/boot/map
> install=/boot/boot.b
> #compact
> #linear
> prompt
> timeout=50
> append="mem=125M"
> 
> image=/boot/rtlinuz-1.1
>         label=rtlinux
>         root=/dev/hda2> 
>         read-only
> 
> ....
> 
> 
> 
> You will have to run /etc/lilo so that it knows the new configuration
> and then you have to reboot the machine and your all set.
> 
> Any program that uses shared memory like this must have root privilege.
> This can be done by running your program as root or setting uid.  However,
> that requires another discussion for which I may not be fully qualified.
> System security is involved and that is a tricky subject to master.  
> Personally I think it all depends on what you are doing and what access
> is provided to the machine.
> 
> This may seem complex but once it is done, accessing any of your data
> in shared memory is a snap (as shown above in ---- user space program ----
> and in ---- real time module ----).
> 
> This should get you rolling quickly.  As I understand the problem
> of trying to get 4 Megs of shared memory it is very difficult if not 
> impossible.  There have been several discussions in the past 3 or 4
> months on this very subject but I don't recall any clear cut solutions.
> 
> The problem as I understand it is that if you try to establish exactly
> 4 Megs of memory Linux just flat out does not recognize that it is there.
> If you try to establish 4.1 Megs Linux does not recognize the first 4 Megs
> but you do get 0.1 Megs available for your use.
> 
> I hope this gets you under way as easily as possible.  I used the 
> Shared memory How-To to get this far.  It took a little work but once
> you understand the concept it goes quite smoothly.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> ----------
> From:         [EMAIL PROTECTED][SMTP:[EMAIL PROTECTED]]
> Sent:         Thursday, May 20, 1999 1:39 PM
> To:   [EMAIL PROTECTED]
> Subject:      Shared Memory under RTL
> 
> Rich,
> 
> I was browsing through the RTL archive and came across your message in which
> you wanted to set-up 3Mb of shared memory between 125Mb and 128Mb. WOuld you
> show me how?
> 
> Also, is it true that the shared memory section limit is 4Mb? If so, can I
> get around it some way?
> 
> Thanks,
> 
> 
> +==================================================================+
>  Phil Daly, NOAO/AURA, 950 N. Cherry Avenue, Tucson AZ 85719, U S A
>  E-mail: [EMAIL PROTECTED]  V-mail: (520) 318 8438  Fax: (520) 318 8360
> 
> 
--- [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/~rtlinux/

Reply via email to