[Gllug] Possible to read shared memory from bash?

Julian Somers lists at bigpip.com
Tue Oct 10 13:04:40 UTC 2006


On 22:35 Fri 06 Oct     , Pete Ryland wrote:
>    On 06/10/06, Julian Somers <[1]lists at bigpip.com> wrote:
> 
>      On 14:51 Thu 05 Oct     , - Tethys wrote:
>      > On 10/5/06, Julian Somers <[2]lists at bigpip.com> wrote:
>      >
>      > >Bash can see my shared memory block with `ipcs`, and delete it
>      with
>      > >`ipcrm -M $shmkey`. Is there a way to read its contents?
>      >
>      > Not off the top of my head, no. But it should be trivial to knock
>      up a
>      > wrapper around shmat(3) that will give you access to shared
>      memory
>      > segments from a shell.
>      Since my C doesn't even stretch to knocking up a trivial wrapper, I
>      have
>      made a php script that fetches the stuff out of shared memory for
>      the
>      shell scripts. Probably more of a pass-the-parcel than a wrapper,
>      but it
>      works.
>      My understanding of shared memory is that I need to avoid
>      simultaneous
>      writes, but don't need to worry about simultaneous reads. Is this
>      the
>      case? And do I need to block anything from reading the memory while
>      I'm writing to it?
> 
>    Can you not re-write your bash scripts in php?
>    #!/usr/bin/php works just the same. :-)
>    It's ok to have multiple readers, but reading memory that is being
>    written to is normally to be avoided, and multiple writers to the same
>    location is definitely to be avoided (but different parts of the same
>    shared memory is of course ok).  You'd normally use some sort of
>    locking mechanism, that is a thing that gets set atomically when you
>    want to write to the shared memory.  When locked, the other processes
>    should block until it is unset.  This can be implemented in any number
>    of ways, from a semaphore to a lock file.
>    Another approach is to use some sort of intermediary that has sole
>    control of the memory through which all the other things get access.
>    Pete

I found a better solution for my 'application variables': an sqlite
database stored in /dev/shm, which is mounted tmpfs. It's at least 50%
faster than using shared memory directly since I don't need to serialise
and unserialise the data in my scripts. And it's a lot simpler to work
with. 

Thanks for your help,

Julian


> 
> References
> 
>    1. mailto:lists at bigpip.com
>    2. mailto:lists at bigpip.com

> -- 
> Gllug mailing list  -  Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug


-- 

Julian Somers
+44 208 265 5407
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list