Solved

How to lock a directory

Posted on 2007-11-27
10
1,678 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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
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
Comment Utility
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
Comment Utility
You could use flock on the single file.  
0
 
LVL 13

Author Comment

by:kawas
Comment Utility
so there is no way to lock a directory?
0
 

Expert Comment

by:shayne321
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Author Comment

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

Expert Comment

by:shayne321
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I ended up creating a file that i flock on while processing the directory
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now