LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-17-2007, 11:36 PM   #1
Jacko123
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Rep: Reputation: 0
Locking and unlocking a file


Hi all,

How can I restrict a user to open the same file on multiple instances of the same application???? If you have word installed, then open multiple instances of it and try opening the same file on all the application...The user is restricted. How can I achieve this???

NOTE: I'm not looking how microsoft have implemented that but looking for a portable solution which can work both on windows and linux.

After search on google I came to know that I need to lock the file( exclusive lock) and then unlock again. I came across this link
http://msdn2.microsoft.com/en-us/lib...4f(VS.80).aspx

That link uses
Quote:
void _lock_file(FILE* file);
. In my application I use ifstream to read a file. I dont have any FIlE*.
So can anyone there tell me how can I lock a file using ifstream or some solution where I can restrict the user to open the same file on different instances of same application.

Thanks
 
Old 05-17-2007, 11:55 PM   #2
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Quote:
Originally Posted by Jacko123
How can I restrict a user to open the same file on multiple instances of the same application????
If the locking is to be restricted for the same application you dont need anything complex. You can achieve this simply by associating an integer variable for each opened file, or just one array(or other appropiate data structure) for all opened files.

However If you want to restrict other programs to open the file which have been opened by your application take a look at the man pages of fcntl and flock.
 
Old 05-17-2007, 11:58 PM   #3
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Forgot to add, you can use semaphore sets to provide locking for multiple instances (processes if I understand you correctly) of the same application.
 
Old 05-18-2007, 12:14 AM   #4
Jacko123
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sibtay
Forgot to add, you can use semaphore sets to provide locking for multiple instances (processes if I understand you correctly) of the same application.
I didnt get you. Can you be more clear about that...if possible with some examples

Thanks
 
Old 05-18-2007, 09:41 AM   #5
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Semaphores (or semaphore sets) are the conventional IPC locking mechanism. If you are looking to provide locking for multiple instances of your application you should use semaphore sets.

I dont have the time to write code examples for you, most of the help you'll find here is just guidance. You have to do your work by yourself.

Read about semaphores and IPC. Following are the first two links revealed from a google search on semaphores:

http://www.cs.cf.ac.uk/Dave/C/node26.html
http://www.ecst.csuchico.edu/~beej/g...emaphores.html
 
  


Reply

Tags
file, locking, unix


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
file locking in ftp krunal Linux - Newbie 2 10-20-2005 05:43 AM
Unlocking /root file Carpinus Linux - Newbie 4 01-16-2005 05:12 AM
locking a usage policy file/ftp file permissions gbow Linux - Newbie 0 02-16-2004 06:35 AM
File locking over Samba rtaft Linux - Software 0 12-30-2003 12:02 PM
unlocking (Win)password-protected file on CD svar Linux - General 5 10-13-2003 03:16 PM


All times are GMT -5. The time now is 09:29 PM.

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