Databases with raw files

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 $n$. Having done that one of these clients increases the number and writes $n+1$ back into the file. Then the other client comes and also writes $n+1$ 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;

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