I need file lock mechanism in Linux platform

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 25
Reputation: nabilchampion is an unknown quantity at this point 
Solved Threads: 0
nabilchampion nabilchampion is offline Offline
Light Poster

I need file lock mechanism in Linux platform

 
0
  #1
Mar 20th, 2008
Hi all
there is a Windows API function as
  1. HANDLE CreateFile(
  2. LPCTSTR lpFileName,
  3. DWORD dwDesiredAccess,
  4. DWORD dwShareMode,
  5. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  6. DWORD dwCreationDisposition,
  7. DWORD dwFlagsAndAttributes,
  8. HANDLE hTemplateFile
  9. );
This function contains argument for share mode.. When I provide it 0 value then no other process can open/access the file already opened while created this call. That is while I have its handle opened if i try to access file through notepad.. it is restricted.
I need to have a similar working in Linux platform. I found two functions given in Linux which are fcntl() and flock() these two provides file locking facility in Linux. But I have tested it by opening a file and then locking it using fcntl() but then when i open the same file through editor or open() system call it is opened and locking is not functional . I need you people's help. I want to have a lock on the file that no process shall be able to open the file while is is locked. plz help me out and suggest me what i am missing.
Regards
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: I need file lock mechanism in Linux platform

 
0
  #2
Mar 20th, 2008
On *nix all you have to do is pass the right arguments in the open statement and it will work the same way as CreateFile() odes. check your compiler to see if it has fsopen() because I'm not sure if that function is standard C or not.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC