LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 12:45 PM   #1
new2linuxsyn
LQ Newbie
 
Registered: Jan 2011
Location: UK
Distribution: Red Hat Enterprise Linux
Posts: 2

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, 01:01 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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 01:04 PM.
 
Old 02-23-2012, 08:48 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

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


Reply



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 12:50 PM
VPATH mechanism in make file sonu kumar Linux - Newbie 1 01-11-2010 11:42 AM
Is there any locking mechanism in linux scriptblues Linux - Newbie 7 06-12-2009 05:57 AM
Linux Traditional File Access Mechanism dolphine Linux - General 2 09-27-2006 02:43 PM
locking a usage policy file/ftp file permissions gbow Linux - Newbie 0 02-16-2004 05:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:11 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
Open Source Consulting | Domain Registration