Page 1 of 1

Are locks implemented/simulatable in PHP?

Posted: Wed Jun 06, 2007 12:00 pm
by the_drizzle
I've been yearning to use locks in PHP for quite some time now, and I always end up finding a context-sensitive workaround. Is there a library somewhere that I can use, or I should I just try to simulate locks? I can imagine solutions by forking processes but I'm running the scripts inside of Apache, and my understanding is that Apache doesn't support forking processes.

Thanks for any help,

Graham

Posted: Wed Jun 06, 2007 12:42 pm
by volka
What kind of lock? file lock, database lock, ... ?

Posted: Wed Jun 06, 2007 4:34 pm
by the_drizzle
I just want to manipulate a general lock. It's fine if I have to acquire a lock for some crazy thing (i.e. a file) and use that, as long as I can play around with the lock itself. My understanding is that if I can *manually* lock an object like a file, then there should be a generic way to lock anything. In fact, the "file lock" would probably use that generic lock.

So, if I can manually manipulate a lock, then there shouldn't be any difference between a "file lock" and a "database lock" other than context. The difference that your thinking about is probably "locking a file" vs. "locking a database table." In this case, there is no notion of a "lock" to the source code, just a concept of "locking," for example when something writes to the file or database. The actual locks are being used beneath the covers.

Every OS provides lock system calls. I guess I'm looking for the closest thing PHP has that uses such calls.

Posted: Wed Jun 06, 2007 9:08 pm
by feyd
There are no generic locks in PHP.

Posted: Thu Jun 07, 2007 5:32 am
by volka
the_drizzle wrote:Every OS provides lock system calls.
Can you provide an example of such a generic lock?

Posted: Thu Jun 07, 2007 4:45 pm
by the_drizzle
http://www.google.ca/url?sa=t&ct=res&cd ... KwI1Lvsjmg

Here's a thing talking about Unix semaphores, which are basically locks but slightly better. Actually, a binary semaphore and a lock are essentially identical.

Now I'm not at all a Unix expert. In fact, my experience with Unix is something very close to zero, so don't quote me on this next bit. I believe that Unix has an internal system that provides general locks to the OS itself, and something like semaphore system calls are just a layer of abstraction between user code and the Unix locks.

Sorry for the initial confusion. I'll try to be more clear with my questions next time.
Thanks for everyone's help.

Posted: Thu Jun 07, 2007 5:41 pm
by volka
I'm not sure what you're looking for. May be http://de2.php.net/sem may be something else.

Posted: Fri Jun 08, 2007 1:01 am
by Chris Corbyn
I think you're thinking about threaded languages like Java? There's no need to lock objects in PHP since there's no concept of threading... everything executes in a linear fashion.

EDIT | Sorry, I should have read your last post since I got the wrong end of the stick.

Posted: Fri Jun 08, 2007 10:11 am
by the_drizzle
Yeah, sorry d11, you're right about the multi-threading. I was actually thinking about multiple processes or multiple Apache threads handling several requests simultaneously. So in theory, it may have been possible to share OS locks amongst threads that are responding to different requests. It was my fault for not being clear enough.

Thanks Volka for the link. I just don't understand why I didn't search for "semaphore" in php.net first .... Sometimes it just feels like one of those days.