[ back to toc ]

Multithread file locking problem

Date: 2002/04/18 10:09

Q:
Hi,
My question is about locking files when more than one threads are willing
to process those files. My development env. is : Red Hat 7.2 with kernel
2.4.7, using gnu gcc. I face seg.dump in the following issue;

One thread is *constantly* waiting for a user to enter a filename and
tries to open and write that file in a specified directory for other
thread to process,

The other thread is *constantly* seeks the specified directory for a new
file, and when found it transmits it to an directory,

Unfornetly, i even cant open the file for writing,
How can i lock file and tell other thread to wait for my unlock? i cant
achieve it with flock() command, cause, to use it i first should open the
file !!! and get file id, but i cant? could you please tell me how to open
these files and lock till the end of write process so that second thread
cant see these files till first thread ends?
thankx in advance,
hoping to hearing from you soon,
Best Wishes

A:
Thread A opens and creates the file myfile.txt, puts data into it and
closes the file. After this thread A opens/creates the file myfile.flag
and closes it (produces a zero length file).

Thread B waits (hope you use some sleep instructions in the code to save
CPU) until it can see myfile.flag. When this appears it moves the file
myfile.txt to wherever it wants. When the file myfile.flag starts to be
old (e.g. 30 seconds old) thread B deletes the myfile.flag.

Regards,
Peter

[ back to toc ]