我正在使用 tdb 来尝试熟悉 Linux 上 C 语言中的数据库管理。根据tdb 的描述
tdb 是一个简单的数据库。
在概念上,它与 GDBM 和 BSD 的 DB 非常相似,只是它允许多个同时写入者并在内部使用锁定来防止写入者相互践踏。tdb 也非常小。界面
该接口与 gdbm 非常相似,除了以下几点:
- 不同的开放界面。tdb_open 调用更类似于传统的 open()
- 没有 tdbm_reorganise() 函数
- 没有 tdbm_sync() 函数。无论如何,库中没有缓存任何操作
- 增加交易支持
使用 tdb 的一般规则是调用者释放所有返回的 TDB_DATA 结构。只需调用 free(p.dptr) 即可释放名为 p 的 TDB_DATA 返回值。这与 gdbm 相同。
现在我想做一个小测试程序来测试我的数据库的多个写连接。但它失败了。
#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;
}
输出是:
Database successfully opened!
RUN FINISHED; Segmentation fault; core dumped; real time: 240ms; user: 0ms; system: 20ms
如果我只打开一个与数据库的连接,则没有问题。如何在数据库上打开多个阅读器?