Link to home
Start Free TrialLog in
Avatar of victory323
victory323

asked on

Locking a file

i'm writing a C program using client-server method, the server sends a file for the client after checking if the file is not locked. The file is locked if it is in use by onther client.

my problem is with the locking mechanism. i have tried to store the file name in a link list and if any client try to get any file , the server should check the file whether it is in the list or not.
but this did not give me a result.

please help me if you have a simple idea and show me how to do it.
 
i need it as soon as possible

thank you all
Avatar of victory323
victory323

ASKER

i've tried to increase the points but i don't have alot
please take my question into consideration.
i need it urgently
Thank you
What OS are you working on?
As a portable way, you could try to create another file while the file you use is locked and check from the other process if that file exists.
After finishing processing, delete the file.

But usually, this is done by shared memory mechanisms. The file creation is somewhat of an extension for semaphores: don't do anything 'till I'm doing.
try semaphores ... to my knowledge they can provide system wide synchronization/locking mechanism

not sure abt windows .. but available on most linux
>>but available on most linux
i meant most Unix and similar systems
Avatar of Kent Olsen
If you're on a *nix system, you have a number of options.  I'm a fan of akshayxx's suggestion to use semaphores.

If you hope to have your application run on both *nix and Windows platforms, you might utilize the message queue and have a single task/thread manage the locks.  (This is also a good *nix solution.)

Borland's C++ has the Syncronize() method to single thread execution in an other-wise multi-threaded application.  Syncronize(LockFile()) and Syncronize(UnLockFile()) force the LockFile() and UnLockFile() methods to be run in the applications primary thread, ensuring single threaded execution.  Borland's C++ Builder supports both *nix and Windows applications


Kdo
for simple mutual exclusion use mutexes.
As you use a client-server model, the common architecture is three-tier. Therefore locking can be done in 3 posible level.
1. Data level (file locking)
2. Server level (multithreading locking)
3. Client level (distributed locking)

File locking is the easiest. You may use some function from io.h (lock, unlock) to do this, but unfortunatelly it's not part of standad C. So it may differ from compiler to compiler.

The second is the most effective, but it higly depends on the multithreading model. You will have different implementation for Windows or Unix.

The third, you need special distributed file system locking. UNIX has NFS that support distributed locking. I'm not sure about Windows.

I suggest that you use the second option. There are several pattern/architecture for this.
1. Blackboard. You use a single global data to store the locking information. Each thread will access this global data with synchronisation (with mutex / semaphore). Fast, but high risk for deadlock.

2. Monitor. You use single thread as a monitor that access the I/O. Other threads send the request to a global mailbox (queue), the monitor gets the request one by one and services it. Slower, but stable.

I hope this intro can shape up your mind. Please tell us more about your OS / Compiler and the locking model you like before we continue the discussion.
 
i'm working on Linux mandrake.
do you mean that there are functions such as Lock and unlock if so please give me the code.
i've tried to use link list but i think i did not do it in the right way i will bring the code that i wrote and paste it soon.
thanks to all
Great ... another Linux programmers is here !!

If you choose to File level lock, this is the manual of file locking. I can't make it shorter :)
http://www.gnu.org/manual/glibc-2.0.6/html_chapter/libc_8.html#SEC146

But yeah ... send your code. I bet you are using Blackboard pattern now.
man flock
man lockf
A note about flock, lockf (and fcntl) -- these are process level locks. If your client/server implementation is multi-threaded these types of locks will not be of much use.
ewest.. what do you mean (( if my client/server implementation is multithreaded then flock,lockf are not much of use )).

by the way, could you please show me how are these functions work??
 thank you.
ewest.. what do you mean (( if my client/server implementation is multithreaded then flock,lockf are not much of use )).

by the way, could you please show me how are these functions work??
 thank you.
It mean, as he said, the lock is useless between thread. If thread 1 lock it, thread 2 can still gain access. To be effective, you have to use multitasking with fork(), not multithreading with pthread().

Oh ... man, this getting complicated :)



Hi Kocil,

I don't think that it's all that complicated.  Someone posts a question, declines to research the link you provided, and now wants the entire web-page pasted into a response.  :)


Victory,

Kocil pointed you to the right place.  Near the bottom of the page is a section entitled "File Locks".  It's URL is:

http://www.gnu.org/manual/glibc-2.0.6/html_chapter/libc_8.html#SEC146

Please read it.  It should be an effective solution and implement fairly easily.


Kdo
ASKER CERTIFIED SOLUTION
Avatar of ewest
ewest

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
...oh and just to be clear: that sample code contains only the barest of error checking/handling.
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by ewest.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer