![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
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 |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
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:
|
|
|
|
|
|
|
#3 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4
![]() |
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.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
#4 |
|
King of Portal
|
@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 |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
King of Portal
|
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 |
|
|
|
|
|
#7 |
|
King of Portal
|
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 |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4
![]() |
Quote:
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
|
#10 |
|
King of Portal
|
@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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |