Using raw files can be a very fast method to store data and to exchange data among clients. However, you must be aware of the fact that we have several clients. These clients may want to write into one and the same file simultaneously. If this is not done in a controlled way, the database may become inconsistent.
Example: Assume that we simple want to count the number of clients we have. To do that we have a file that contains a number that initially is zero. Each client opens this file, reads the number and writes it back. Now assume that we have two clients that simultaneously open the file and read the number . Having done that one of these clients increases the number and writes back into the file. Then the other client comes and also writes into the file. This is not the desired result.
One way to make sure that only one client at the time has access to the file is locking. The following fragment of a C-program shows the principle:
FILE *filedescriptor; char *filename;
filedescriptor = fopen (filename,"a");
flock (fileno (filedescriptor),LOCK_EX);
now we can do whatever we want with the file, knowing that we are alone.
fprintf(filedescriptor,"this and that")
fflush (filedescriptor); flock (fileno (filedescriptor),LOCK_UN); fclose (filedescriptor);
Notice that most operating systems only support advisory locks. This means that it is essential that when opening the file you check whether another process has a lock on this file. If you don't check there may well be a lock and you may still be able to write into the file.
Oliver Kirchkamp 2013-04-15