Page 1 of 1
How to use "System V" semaphore functions in PHP
Posted: Wed Jan 28, 2004 8:39 pm
by panahi
I am trying to restrict access to some MYSQL data on the web server, so that two users (in browsers, over the web) can't edit the same data at the same time.
I am trying to use PHP's semaphore functions.
I have included System V in my PHP build. I can call the sem_get() and sem_acquire(), etc.. functions.
When I call "$sem_id = sem_get($ID,1);" I get "Resource id #17" as a result.
When I call "$result = sem_acquire($sem_id);" I get a result of $result = 1.
This seems great. However, when I leave the first browser with the page open in it, and open up another browser with the same page and the same value for "$ID" I get the exact same results.:
$sem_id = Resource id #17
$result = 1
I was expecting to get the same result from "$sem_id = sem_get($ID,1);" but then suspend on "$result = sem_acquire($sem_id);".
EDIT----01-29-2004 3:34 pm----
Another related problem:
If I use a different key ($ID) value to sem_get($ID), the Resource id still comes up as "Resource id #17". It seems like it should be creating a new resource id for a new semaphore.
----EDIT
Anybody have any ideas why this does not work as expected?
Posted: Wed Jan 28, 2004 11:00 pm
by xisle
would a table lock be easier...
Re: table lock
Posted: Thu Jan 29, 2004 9:33 am
by panahi
What is a table lock?
Is it a MySQL function?
Posted: Thu Jan 29, 2004 9:34 am
by m3rajk
yes. mysql has a function to lock tables.
http://www.mysql.com
Re: Table lock
Posted: Thu Jan 29, 2004 10:38 am
by panahi
Thanks for the idea m3rajk.... I may use it on another endeavor.
But I just thought through this and I really need to prevent the user from accessing the html form where the data is input to the browser. This is lbefore the browser writes the data to the MySQL table.
If the table lock can prevent a read of a certain MySQL record, then it could work for this application. Or if it could just tell me that someone has read that record, and has not written to it yet.
I think I can get the semaphore thing to work. Any clues as to why it doesn't. One more thing to note on that subject: If I put the sem_acquire($ID) statement a second time on the same page after the first sem_acquire($ID) and before a sem_release($ID) then the browser locks up. This is what I would expect to happen if another browser called a sem_acquire with the same $ID.
Any ideas?
Posted: Thu Jan 29, 2004 12:41 pm
by m3rajk
go to
http://www.mysql.com and look up table locking. i haven't used it so i'm ont entirely sure how it works. your response sounds like you want us to tell you exactly how it works to know if it is what you need. we've not only told you about a built in function, but i've twice linked you to the site where you can find out the details on your own
panahi wrote:If the table lock can prevent a read of a certain MySQL record, then it could work for this application. Or if it could just tell me that someone has read that record, and has not written to it yet.
Re: How do I use semaphores in PHP
Posted: Thu Jan 29, 2004 1:33 pm
by BDKR
panahi wrote:I am trying to restrict access to some MYSQL data on the web server, so that two users (in browsers, over the web) can't edit the same data at the same time.
I am trying to use PHP's semaphore functions.
I have included System V in my PHP build. I can call the sem_get() and sem_acquire(), etc.. functions.
When I call "$sem_id = sem_get($ID,1);" I get "Resource id #17" as a result.
When I call "$result = sem_acquire($sem_id);" I get a result of $result = 1.
This seems great. However, when I leave the first browser with the page open in it, and open up another browser with the same page and the same value for "$ID" I get the exact same results.:
$sem_id = Resource id #17
$result = 1
I was expecting to get the same result from "$sem_id = sem_get($ID,1);" but then suspend on "$result = sem_acquire($sem_id);".
Anybody have any ideas why this does not work as expected?
Just to revisit the the use of semaphores, it's really not a problem that you got the same resource id from two seperate processes. What's really important is that only one process has grabbed the semaphore. And that is really what it's use is all about. Making sure that only process at a time is getting access to a critical section of memory or code.
Just keep in mind that a resource id is
NOT the same as the data that resource represents or links too.
Cheers,
BDKR
Re: Semaphore Resource ID
Posted: Thu Jan 29, 2004 2:13 pm
by panahi
Well,
You're right, the fact that it is grabbing the same resource id is fine., it is actually what should happen. I understand this much.
However, it is the fact that System V is allowing a process to sem_acquire() the semaphore with "Resource id#17" when the semaphore with "Resource id#17" has already been sem_acquire()ed by another process and has not been released yet.
This is the main problem for me right now.
Another problem:
To boot: if I use a different key ($ID) value to sem_get($ID), the Resource id still comes up as "Resource id #17". It seems like it should be using a different resource id for a different semaphore.
Any ideas?
--------------------------------------------------------------------------------------
m3rajk,
(EDIT----01-29-04: 4:09pm----
I looked at the MySQL LOCK functions. They look perfect for what I am doing. Thanks.
I just have to do:
mysql> LOCK TABLES transition WRITE;
mysql> UPDATE transition SET data=$data WHERE ID=$ID;
mysql> UNLOCK TABLES;
Then, when I try to read the MySQL data from the table:"transition" in a new browser window the thread should suspend until I UNLOCK the TABLE.
I know this is a different subject, maybe I should start a new FORUM thread: (See MYSQL LOCK functions)
Back to System V semaphore functions......
------EDIT)
m3rajk
Posted: Thu Jan 29, 2004 2:15 pm
by panahi
Posted: Thu Jan 29, 2004 2:17 pm
by Straterra
I hate to nag, but rather, in stead of posting a whole new topic 2 minutes later, just edit the first one and add what you wanted in the second post..after, of course, putting EDIT: