Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
互斥还是SQLITE3中的另一种并发机制?(C语言)_C_Sqlite_Mutex - Fatal编程技术网

互斥还是SQLITE3中的另一种并发机制?(C语言)

互斥还是SQLITE3中的另一种并发机制?(C语言),c,sqlite,mutex,C,Sqlite,Mutex,我正在用C语言编写一个函数库,它提供了读取、写入和清理sqlite3数据库表寄存器的基本函数 当我用一个终端执行它时,所有的功能都正常工作。但是,我的问题是当我试图使用来自多个终端的功能时。例如,在一个单终端中,我有一个程序,它使用一个函数将信息存储在数据库文件的表中。另一方面,在另一个终端中,我有一个程序,它使用不同的函数,将信息存储在同一数据库的另一个表中。然后,在这种环境中,信息无法正确存储。因此,我认为我必须使用信号量或互斥机制来实现所有函数 在sqlite3文档中搜索,我发现有一些函数

我正在用C语言编写一个函数库,它提供了读取、写入和清理sqlite3数据库表寄存器的基本函数

当我用一个终端执行它时,所有的功能都正常工作。但是,我的问题是当我试图使用来自多个终端的功能时。例如,在一个单终端中,我有一个程序,它使用一个函数将信息存储在数据库文件的表中。另一方面,在另一个终端中,我有一个程序,它使用不同的函数,将信息存储在同一数据库的另一个表中。然后,在这种环境中,信息无法正确存储。因此,我认为我必须使用信号量或互斥机制来实现所有函数

在sqlite3文档中搜索,我发现有一些函数提供互斥机制。这些功能如下:

sqlite3_互斥体*sqlite3_db_互斥体sqlite3*;=>返回数据库连接的指针互斥体 无效sqlite3_互斥体_输入sqlite3_互斥体*;=>进入临界区 void sqlite3_mutex_leavesqlite3_mutex*;=>离开临界区 我已经用我的函数尝试过了,但是结果是一样的,当我试图用不同的进程在同一个数据库中存储信息时,信息无法正确存储。我为存储信息的两个函数实现的代码更简单,如下所示:

第一个函数=>将数据存储在表1中

  int add_register_table1(unsigned short int type,unsigned short int err, const char *msg, const char *agent)
{
    //Local variables:
    int rc, t0;
    char buffer[BUFFER_SIZE];
    char *errMsg = 0;
    sqlite3 *db;
    sqlite3_mutex* mutex;

    //Initializing variables:
    t0 = (int) time(NULL);  //Save the current time in seconds

    //Opening the syslog.db database connection:
    rc = sqlite3_open(database.db, &db);

    if(rc != SQLITE_OK)     //Error checking
        return ERROR;

    mutex = sqlite3_db_mutex(db);   //Acquire the mutex

    //Writing data:
    sqlite3_mutex_enter(mutex);   //Enter critical zone
    sprintf (buffer,"INSERT INTO table1 VALUES ('%d','%d','%d','%s','%s');", t0, type, err, 
                                                                                         msg, agent);
    rc = sqlite3_exec(db, buffer, NULL, NULL, &errMsg);

    sqlite3_mutex_leave(mutex);   //Leave critical zone

    if(rc != SQLITE_OK)     //Error checking
        return ERROR;

    //Closing the database connection:
    sqlite3_close(db);

    return NOERROR;
}
第二个函数相同,但它将信息存储在同一数据库文件的另一个表中:

int add_register_table2(unsigned short int type,unsigned short int err, const char *msg, const char *agent)
{
    //Local variables:
    int rc, t0;
    char buffer[BUFFER_SIZE];
    char *errMsg = 0;
    sqlite3 *db;
    sqlite3_mutex* mutex;

    //Initializing variables:
    t0 = (int) time(NULL);  //Save the current time in seconds

    //Opening the syslog.db database connection:
    rc = sqlite3_open(database.db, &db);

    if(rc != SQLITE_OK)     //Error checking
        return ERROR;

    mutex = sqlite3_db_mutex(db);   //Acquire the mutex

    //Writing data:
    sqlite3_mutex_enter(mutex);   //Enter critical zone
    sprintf (buffer,"INSERT INTO table2 VALUES ('%d','%d','%d','%s','%s');", t0, type, err, 
                                                                                         msg, agent);
    rc = sqlite3_exec(db, buffer, NULL, NULL, &errMsg);

    sqlite3_mutex_leave(mutex);   //Leave critical zone

    if(rc != SQLITE_OK)     //Error checking
        return ERROR;

    //Closing the database connection:
    sqlite3_close(db);

    return NOERROR;
}
我的问题如下:

·看看我的C代码,使用的sqlite3互斥函数使用得很好,并且必须正确运行,或者我的代码中这些函数的实现是错误的

·是否有任何其他类型的机制来实现与多个进程并发的写/读数据库?这些机制是什么?如何实施

从几天前开始,我一直在处理这些问题,但我没有找到解决办法


非常感谢

你不需要互斥,反正它们只在一个进程内工作。你说存储不正确是什么意思?当我说存储不正确时,我的意思是当不同的程序在不同的表中存储信息,但同时在同一个数据库文件中存储信息时,其中一些信息没有存储或丢失,这意味着我无法使用sqlite3同时存储不同进程?您使用的是什么文件系统?我使用的是NTFS文件系统。我一直在搜索有关此问题的更多文档,我发现sqlite3在读取模式下可以正常并发工作,但在写入模式下不能。这是真的吗?不可能同时写?