Shared memory locking

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
tomtomtom
Forum Newbie
Posts: 8
Joined: Tue Dec 28, 2004 12:39 pm

Shared memory locking

Post by tomtomtom »

Hey,

I'm implementing shared memory into my PHP application for performance reasons, but I'm stuck.

Since the memory will be receiving up to 10-15 reads per second and 1 or 2 writes per second, I want to have a locking mechanism, using semaphores.

I'm not sure what to lock though. The obvious case is where the script reads data, manipulates it, then writes it, which I will enclose in a lock.

But I'm guessing it'd be a good idea to lock all write operations. But then what about reads?

Is it theoretically possible for a read to happen at the same instant as a write, so that the read operation picks up a corrupt mixture of new and old data?

So, should I lock all reads? Can I do this using a read-lock, so that more than one read can occur simultaneously?


Hope this makes some kind of sense. I'd appreciate any help / ideas!

Thomas
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

i think when the mem's locked it cant be read either so the read(s) will have to wait until the write's done anyways.
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

Coming from ASP and VB I can see how shared memory would be used for a large app where a single instance of an object would be used to communicate to other processes and dlls thus saving resources. But how can this increase performance in PHP? I thought shared memory was just sort of communication scheme. Locking and unlocking memory spaces might improve stability but how can it increase throughput when everything has to wait in line?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

carlmcdade wrote:I thought shared memory was just sort of communication scheme. Locking and unlocking memory spaces might improve stability but how can it increase throughput when everything has to wait in line?
Shared memory could be used as a cache storage for example. And, as with any shared resources, it should be properly locked. Otherwise you would get sometimes complete garbage, especially under high load. Read lock does not prevent concurrent reads, it only forces write operations to wait.
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

Weirdan wrote: Shared memory could be used as a cache storage for example.
That's interesting ! How is this with shared hosting environment and is it something that is easier to clean and maintenance than what I use now? Which is MySQL tables or flat files.
tomtomtom
Forum Newbie
Posts: 8
Joined: Tue Dec 28, 2004 12:39 pm

Post by tomtomtom »

SHM-based caching is great. I think it doesn't come into its own though until load is so high that reading from the HDD is the bottleneck (up to that point a file-based cache is easier - jpcache is good). It should be used in addition to a backing database, because it's volatile.

Back the locking... locking every read would be incredibly inefficient... isn't there a read-lock in PHP's implementation of semaphores, as there is in the flock() function???? If not, is it possible to simulate this behaviour?
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

It sucks that this does not work in windows and is too volatile for me to be experimenting with on a hosted site. I guess I will have to install Xandros again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

carlmcdade wrote:It sucks that this does not work in windows and is too volatile for me to be experimenting with on a hosted site. I guess I will have to install Xandros again.
It's odd to me why they haven't implemented it. Those services exist on the bulk of windows machines used as servers, although sometimes hidden from the user.
ianlandsman
Forum Newbie
Posts: 24
Joined: Thu Dec 30, 2004 9:50 pm
Location: New York

Post by ianlandsman »

Not sure if this is an option for you, but I've used the system memory functions of mmcache very successfully. They abstract all the tough stuff.
Post Reply