I have three instances of the same program running and writing to one file, I need to implemet file locking, can anyone please explain to me of how to implement this for the above scenario.
if (fp = fopen ("test.out", "+a" == NULL)
{
printf("File open errror\n"
}
I have three instances of program (say abc) running. Lets assume the instances to be a1, a2, a3. Now each instance will try to write to test.out so how can I make sure that only one writes to the file and the other wait until the lock is released in order to prevent data from corruption.
File locking is not an easy topic, and probably is not platform specific. Unix has a lockf function which locks and unlocks a file opened at low level. I'm giving you a test file, slock, I've been playing with.
I'm testing this under Unix Solaris 7. Execute slock which creates and locks the file if it isn't already existing, and sits there waiting for input. This locks the file until the user presses <CR> which unlocks at deletes the file.
In another unix instance, try executing slock and verify you can't do it until pressing <CR> in the slock program which originally locked the file.
What little I know about file locking I learned from W. Richard Stevens book "Unix Network Programming" ISBN 0-13-949876-1.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.