LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-23-2012, 01:45 PM   #1
new2linuxsyn
LQ Newbie
 
Registered: Jan 2011
Posts: 1

Rep: Reputation: 0
UNIX File locking mechanism


Hi All,

How file locking mechanism work in unix/Linux. For example if one process is writing data to file. Can another external process acess this file to move?
I guess in windows file is locked until writing is finished.

Any ideas

Many thanks
 
Old 02-23-2012, 02:01 PM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,218

Rep: Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154Reputation: 1154
Usually another process could move the file mid-write, which will cause the first process to create a new file or crash, depending on how it's written.

It's a good idea to use a mutex (mutual exclusion) file when this is a concern, or temp files and symlinks, depending on your specific setup. A mutex is a file that is written, who's mere existence tells other programs that you're accessing some shared file. Your first process checks for the existence of a mutex file, if one is there it waits, otherwise it writes its own. It then does whatever it needs to on the shared file, finishes up, and finally it deletes its mutex to tell all other processes that the shared file is available for modification.

A variation on the mutex idea is for the writing code to create a small "finished" or "done" file when the writing is complete. Any other process should then ignore the file of interest (call it "file"), until the "done" file appears (call it "file.done"), indicating that "file" has finished writing.

A setup with temp files and symlinks is a little more complicated, but can be very useful if you have one process periodically writing to a file, and another process reading from it, but you only want to read from it if the file is complete. In that case your "file" would be a symlink, that would either point to "fileA" or "fileB". The writing process would check the target of the symlink, and then write to the OTHER file (so if "file" points to "fileA", the writing process would write to "fileB"). When the writing is complete, the writing process moves the symlink target from "fileA" to "fileB". The reading process can then just read from "file", without regard for which target "file" is actually pointing to.

Last edited by suicidaleggroll; 02-23-2012 at 02:04 PM.
 
Old 02-23-2012, 09:48 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
http://en.wikipedia.org/wiki/File_locking#In_Unix
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux File Locking Mechanism Samrat_Chakravarty Linux - Kernel 1 07-07-2010 01:50 PM
VPATH mechanism in make file sonu kumar Linux - Newbie 1 01-11-2010 12:42 PM
Is there any locking mechanism in linux scriptblues Linux - Newbie 7 06-12-2009 06:57 AM
Linux Traditional File Access Mechanism dolphine Linux - General 2 09-27-2006 03:43 PM
locking a usage policy file/ftp file permissions gbow Linux - Newbie 0 02-16-2004 06:35 AM


All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration