I'm using tdb to try to get acquainted with database management in C on Linux. Per tdb's description
tdb is a Trivial database.
In concept, it is very much like GDBM, and BSD's DB except that it allows multiple simultaneous writers and uses locking internally to keep writers from trampling on each other. tdb is also extremely small. Interface
The interface is very similar to gdbm except for the following:
- different open interface. The tdb_open call is more similar to a traditional open()
- no tdbm_reorganise() function
- no tdbm_sync() function. No operations are cached in the library anyway
- added transactions support
A general rule for using tdb is that the caller frees any returned TDB_DATA structures. Just call free(p.dptr) to free a TDB_DATA return value called p. This is the same as gdbm.
Now I wanted to make a small test program to test multiple write-connections to my database. But it fails.
#include <tdb.h>
int main(int argc, char** argv) {
struct tdb_context * tdb = tdb_open("test.tdb", 0, 0,
O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
if(!tdb){
printf("%s\n", tdb_errorstr(tdb));
} else {
printf("Database successfully opened!\n");
}
struct tdb_context * anothertdb = tdb_open("test.tdb", 0, 0,
O_RDWR| O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); //why does this fail?
if(!anothertdb){
printf("%s\n", tdb_errorstr(anothertdb));
} else {
printf("Another reader successfully opened!\n");
}
if(tdb_close(anothertdb)){
printf("Error while closing database!\n");
} else {
printf("closing anothertdb-connection\n");
}
if(tdb_close(tdb)){
printf("Error while closing database!\n");
} else {
printf("closing tdb-connection\n");
}
return 0;
}
The output is:
Database successfully opened!
RUN FINISHED; Segmentation fault; core dumped; real time: 240ms; user: 0ms; system: 20ms
If I open just a single connection to the database there are no problems. How do I open multiple readers on a database?