Re: Shared Memory Stuff ?
| From: | Jeff Davis | Date: | Sun, 15 Oct 2000 08:49:09 +0000 |
| Subject: | Re: Shared Memory Stuff ? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-20226@lists.php.net to get a copy of this message | ||
I asked the same question not long ago, so I will give you my interpretation
of shared memory and then the responses I received on this list.
Think of shared memory as a file in RAM, and semaphores as flock()-type
functions. You choose a key (like a filename, except has to be a number),
permissions, and the size of the memory block (ok, so this is unlike a file
system, but you have to pick a value at the creation). Then you reaad and
write with various processes. This is fast IPC (interprocess communication),
designed to move small amounts of data between programs (that run otherwise
independently of eachother). Semaphores control the writing so that you
don't write while another process reads or writes, kind of like flock()
I don't think the shared memory is cleaned up automatically, so watch for
memory leaks when using this feature. You also may have some difficuly if
you have never programmed in C before. Finally, don't even try this in
windows, it is not supported by the OS itself (windows has no shared memory
at all, from what I understand).
The replies I got are listed below.
I hope this helps you out, I know it enlightened me.
Regards,
Jeff Davis
REPLIES:
When you create a shared memory segment the system needs a way to identify
your shared memory segment from all other shared memory segments out there.
This is accomplished by assigning each shared memory segment an identifier
of sorts. The system then uses that identifier as a pointer to your shared
memory segment. The key should be a unique number so that the system doesn't
confuse your shared memory segment with someone else's memory segment.
The access modes for shared memory are the same as for files, ex 644 would
mean that the shared memory segment is world readable but only the owner
(person who created the memory segment) can write to it.
Shared memory is the fastest IPC (Inter Process Communication) method which
means its generally used to quickly transfer data between separate process',
ex. I use shared memory to transfer date between a C daemon running on a
system and a php client.
Ilia Alshanetsky
iliaa@home.com
PHP has several problems related to shared memory.
1) PHP sticks a php-signature on a shared memory block, last time I checked
you could not just 'dump' data in a block.
So, in order to communicate with a C program you'd need to use the 'php
method' of reading and writing shared memory. (Which can be quite a PITA,
since you'd have to dig into the php sourcecode to figure out the block
layout and encoding.)
2) Synchronisation. Usually, when dealing with a shared piece of memory
there is some kind of synchronisation, and very often that is done using
semaphores. Now, when you enter a WAIT state the process is suspended, and
if the process waiting is never signalled the process *stays* suspended. PHP
execution timers do *not* terminate processes that are in suspended state.
This is a very nasty effect that makes semaphores a very dangerous tool in
php. Deadlocks are not a good thing.
So, if you're absolutely sure what you're doing, shared memory may be a very
fast way of transferring data. But I would advise using alternative methods
of communication if you want to communicate with other php scripts or other
processes. (For example, pipes or unix domainsockets)
-Sander
The p
----- Original Message -----
From: "Sander Pilon" <sander@3dnews.net>
To: "Ilia Alshanestky" <iliaa@home.com>; "Jeff Davis"
<jdavis@wasabimg.com>;
<php-general@lists.php.net>
Sent: Saturday, October 14, 2000 5:18 PM
Subject: RE: [PHP] Shared Memory
> PHP has several problems related to shared memory.
>
> 1) PHP sticks a php-signature on a shared memory block, last time I
checked
> you could not just 'dump' data in a block.
> So, in order to communicate with a C program you'd need to use the 'php
> method' of reading and writing shared memory. (Which can be quite a PITA,
> since you'd have to dig into the php sourcecode to figure out the block
> layout and encoding.)
>
This is no longer an issue if you use the new shmop() of shared memory
functions that have been added in php 4.0.3.
Shmop doesn't add any extra headers to the shared memory segment, which
means you don't have the headache of creating headers etc...
> 2) Synchronisation. Usually, when dealing with a shared piece of memory
> there is some kind of synchronisation, and very often that is done using
> semaphores. Now, when you enter a WAIT state the process is suspended, and
> if the process waiting is never signalled the process *stays* suspended.
PHP
> execution timers do *not* terminate processes that are in suspended state.
> This is a very nasty effect that makes semaphores a very dangerous tool in
> php. Deadlocks are not a good thing.
>
> So, if you're absolutely sure what you're doing, shared memory may be a
very
> fast way of transferring data. But I would advise using alternative
methods
> of communication if you want to communicate with other php scripts or
other
> processes. (For example, pipes or unix domainsockets)
>
> -Sander
>
> >
> > When you create a shared memory segment the system needs a way to
identify
> > your shared memory segment from all other shared memory segments
> > out there.
> > This is accomplished by assigning each shared memory segment an
identifier
> > of sorts. The system then uses that identifier as a pointer to your
shared
> > memory segment. The key should be a unique number so that the
> > system doesn't
> > confuse your shared memory segment with someone else's memory segment.
> > The access modes for shared memory are the same as for files, ex 644
would
> > mean that the shared memory segment is world readable but only the owner
> > (person who created the memory segment) can write to it.
> > Shared memory is the fastest IPC (Inter Process Communication)
> > method which
> > means its generally used to quickly transfer data between
> > separate process',
> > ex. I use shared memory to transfer date between a C daemon running on a
> > system and a php client.
> >
> >
> > Ilia Alshanetsky
> > iliaa@home.com
> >
> >
> >
> > ----- Original Message -----
> > From: "Jeff Davis" <jdavis@wasabimg.com>
> > To: <php-general@lists.php.net>
> > Sent: Saturday, October 14, 2000 4:05 PM
> > Subject: [PHP] Shared Memory
> >
> >
> > > I read the following at the php.net manual:
> > >
> > > shm_open() takes 4 parameters: key, which is the system's id for the
> > > shared memory block, this parameter can be passed as a decimal or hex.
> > >
> > > I am interested in using shared memory, but I don't know what to
supply
> > > for the key. I also don't know how the access mode (ie 0644) works
with
> > > memory. What kinds of situations might shared memory be used for? Is
it
> > > to hold data from one script to the next, or should it not be sued for
> > > that?
> > >
> > > Thanks,
> > > Jeff Davis
>
Mukul Sabharwal wrote:
> Hi,
>
> I wanted to know more about shared memory and what it
> could be used for ? And how is it different from what
> we use normally in programming ?
>
> thanks.
>
> =====
> To Find Out More About Me : http://mukul.tsx.org/
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger - Talk while you surf! It's FREE.
> http://im.yahoo.com/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net