Shared memory locking
Moderator: General Moderators
Shared memory locking
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
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
- carlmcdade
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 02, 2004 2:19 am
- Location: sweden
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?
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.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?
- carlmcdade
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 02, 2004 2:19 am
- Location: sweden
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?
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?
- carlmcdade
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 02, 2004 2:19 am
- Location: sweden
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.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.
-
ianlandsman
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 30, 2004 9:50 pm
- Location: New York