Are locks implemented/simulatable in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the_drizzle
Forum Newbie
Posts: 17
Joined: Fri Jun 01, 2007 11:55 am

Are locks implemented/simulatable in PHP?

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What kind of lock? file lock, database lock, ... ?
the_drizzle
Forum Newbie
Posts: 17
Joined: Fri Jun 01, 2007 11:55 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are no generic locks in PHP.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the_drizzle wrote:Every OS provides lock system calls.
Can you provide an example of such a generic lock?
the_drizzle
Forum Newbie
Posts: 17
Joined: Fri Jun 01, 2007 11:55 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm not sure what you're looking for. May be http://de2.php.net/sem may be something else.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
the_drizzle
Forum Newbie
Posts: 17
Joined: Fri Jun 01, 2007 11:55 am

Post 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.
Post Reply