Solved

How to lock a directory

Posted on 2007-11-27
10
1,719 Views
Last Modified: 2010-04-21
I have a directory that contains a bunch of files that represent a file cache. I would like to deploy my code to a web server, but I havent made the code thread safe.

I have read about flock, and pretty much just want confirmation or a better way of doing things.

I would like to lock the directory when i update the file cache. How can I do this?

Thanks.
0
Comment
Question by:kawas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 20361427
What is the problem you will have if you don't lock the directory?

Are you concerned a script might read some old cache files, and some new, if it runs while you are updating the cache?
0
 
LVL 13

Author Comment

by:kawas
ID: 20361613
exactly. Also, the cache works in such a way that I have a single file that is used as a  list file that allows me to determine if the other files are stale. if they are stale, they get updated and a new list file is created.

I need only non-stale files served, so I would like to lock a directory containing the files while i process them all.

Thanks.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 20361845
You could use flock on the single file.  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:kawas
ID: 20361863
so there is no way to lock a directory?
0
 

Expert Comment

by:shayne321
ID: 20361902
Is this a Windows or UNIX/Linux system?  also are you looking to lock it in such a way that no other process can access it, or only so that your script does not access it while an earlier run of your script is still running?
0
 
LVL 13

Author Comment

by:kawas
ID: 20361928
no other process and on linux (but would prefer a portable solution)
0
 

Expert Comment

by:shayne321
ID: 20362189
One way to do it if the other processes do not run under the same user ID is to remove world-execute permissions from the dir when your script starts, and add them back when it is done:

chmod(0750, $DIR);
## do stuff
chmod(0755, $DIR);

  However you'd obviously want to be very careful your script can't die in a way you don't anticipate.  Not sure how chmod() behaves on windows, though..  I doubt it would behave correctly.

Shayne
0
 
LVL 13

Author Comment

by:kawas
ID: 20362229
hmm, there isnt a safer way of doing this? My problem doesn't come across as something new. Has anyone ever wrote a script that reads/writes common files? How did you handle the thread issues?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20363708
You can flock a directory, but like flocking a file, it only prevents other processes from doing a flock on the same file.
If all processes cooperate and only do things when the flock grants them permission, that's fine.
is you don't unlock, flock releases the lock when the process exits
0
 
LVL 13

Author Closing Comment

by:kawas
ID: 31411291
I ended up creating a file that i flock on while processing the directory
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question