Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2007, 2:04 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Question Implementing a Queue

I'm stating this as a conceptual problem which I'd like to be able to implement in php, but I'm not exactly sure how to start or organize it. So let me set up how the user interaction goes.
Setup: There is a folder and within this folder when users who frequent the site submit to a form files are created sequentially within this folder. So for instance the first file in the folder would be 000.php and then the next file would be 001.php and so on.
Problem: Let's say two users access the form at the same time and thus both would be trying to create the file 002.php when one user submits, the form will create 002.php. However when the other user submits this previous file will be overwritten with a new 002.php rather than incrementing to 003.php. Obviously I've now lost and overwritten data. I don't want this to happen.
Approach: The most obvious solution is I could perform a last second check before I actually write to the file and if it already exists then increment it and finalize the write. However, I think that might create a problem if someone creates the incremented file the instant after the check is performed (say there was a third user doing the same thing). Furthermore, there are reasons I can't perform that check in other sections of the code since some filenames aren't done incrementally (certain other files in the folders have special names and are also generated spontaneously).
I'm thinking that optimally what I need is a queue which processes users as they access the folder to read/write and so on. However, since php isn't a constant running process (I could maintain the queue in an array for instance which then I'd know how to do), I figure I'd have to store the folder access "stack" in a file, but then I'm not entirely sure how to process it. Like wait until everyone in the queue has pressed submit and then go ahead and process the entries, or wait until sequentially each person has pressed their submit as I pop people off of it and tell those that try to submit to try again, etc.
So in conclusion, any suggestions, guides, ideas, references?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 29th, 2007, 2:58 PM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
If Someone was creating a large file this will block and slow down other users interactions with the site, why don't you just use a timestamp as the filename, i think it would properly be hard for two people to submit a form at exactly the same time.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 29th, 2007, 4:30 PM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
I've never used PHP, but I have an idea you can try. In C, you have the tmpfile() and tmpnam() functions; these create a temporary file in the current directory and a unique name for a temporary file. I'm assuming that PHP may well have something similar. You can then have both users create a file (each file will have its own unique name), and when one submits, it tries to rename the file as xxx.php, with xxx being your incrementing value. One of these rename attempts (ie, the first) should work; the other will not, and will raise an error/exception/whatever. If you detect this error condition, increment and attempt the rename again.

Given that this situation should only arise when two people try to save the form at the same moment, it shouldn't be a very frequent occurrence.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old May 29th, 2007, 4:39 PM   #4
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
@kruptof: You're absolutely right, the odds of two people submitting the data at the same time is remote, but it can happen. This sort of a database question in essence in which preserving already existent data is a necessary worry.
@lectricpharaoh: I will begin research on that posthaste.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 29th, 2007, 4:44 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Put the next name to be used in a file and flock that file when reading/updating it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 29th, 2007, 4:47 PM   #6
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
There is such a function tmpfile. However, using it to do what I want isn't quite so apparent. I'll have to think on it and write back.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 29th, 2007, 4:50 PM   #7
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Hmm... there seems to be a pretty explicit warning on that flock function that it may not be very reliable everywhere.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 29th, 2007, 5:02 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Are you going to use it everywhere, or just on your non-NFS, non-FAT server?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 29th, 2007, 5:02 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by grimpirate
There is such a function tmpfile. However, using it to do what I want isn't quite so apparent. I'll have to think on it and write back.
It should be trivial, unless I misunderstand your first post. Let's recap, then.
  • You want to create a file, xxx.php, based on the user's interaction with a form.
  • You determine the value of xxx based on when the user accesses the form, and thus, you can have a name collision when two or more users access the form at the same time.
  • Such a name collision will cause the file generated by one user's form interaction to be overwritten with that from another, and this is a bad thing.
Now, rather than opening a file named xxx.php (and potentially overwriting one with the same name), you use tmpfile() to create the file, write the output, and close the file. You then try to rename it to xxx.php. If xxx.php does not exist, this should work; otherwise, you increment xxx, and try again (repeat until successful). Of course, this presupposes that you already have a mechanism in place to keep track of your incrementing three-digit value, but I assumed you'd already coded this, or know how to do so.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old May 29th, 2007, 5:16 PM   #10
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
@DaWei: well the end result is to how posts are made on my bulletin board script. The script itself I don't know what sort of servers it will be executed on. Therefore, I would like to avoid any unecessary headaches that might be caused to the end user if their server just so happens to fall into the warning. Something less buggy would be preferable. Not trying to be a stickler though DaWei, your suggestion certainly is right along what I was thinking I just never noticed the flock function while looking through the filesystem functions in php.
@lectricpharaoh: according to the description of the function tmpfile the moment I close the file it gets automatically deleted. So it seems like what you're saying is an extra step. Why not eliminate the temp file and just check before actually writing? 'cause the moment you call fopen the file is already in existence. The problem is that split second of various instances of the script running. Where one could accidentally overwrite the other in that moment between the check and the actual fopen call, due to the fact that instructions may not be running sequentially. Two scripts may execute with their instructions overlapping one another. I don't really know enough about the PHP language to say this wouldn't happen. Aside: the 3 digit value is computed by counting the number of files in the folder.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
queue - shared memory programmingnoob C++ 4 Mar 26th, 2007 8:19 PM
message queue game programmingnoob C 19 Sep 29th, 2006 5:14 AM
Queue again zman2245 C++ 3 Sep 9th, 2005 1:07 PM
Queue. Array vs Linked List zman2245 C++ 9 Sep 9th, 2005 1:14 AM
Queuing and Queue Tables foxcity911 Other Programming Languages 1 Aug 6th, 2005 11:19 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC