简体   繁体   English

C - 管理同一文件的共享库

[英]C - Shared library with management of the same file

I'd like to create shared library in C for linux, some abstract implementation of database management.我想在 C 中为 linux 创建共享库,数据库管理的一些抽象实现。 Shared library whould be responsible for read file containing database and write differences into it.共享库负责读取包含数据库的文件并将差异写入其中。 But I have no idea how to handle multiprocessing problems of file handling for this case eg.: App1 try to write differences into database file and App2 has currently opened file with database to read it.但我不知道如何处理这种情况下的文件处理的多处理问题,例如:App1 尝试将差异写入数据库文件,而 App2 目前已打开带有数据库的文件以读取它。 In the case of this example I'd like to inform app1 that file is currently open and delay write sequence until App2 will finish database file read.在此示例中,我想通知 app1 文件当前已打开并延迟写入序列,直到 App2 完成数据库文件读取。

I was thinking of using some mutual exclusion mechanisms or by using global enum variable to manage current file status, but after I read some of posts I understood that every application that uses shared library create it's own copy in memory and they don't share any memory section during work.我正在考虑使用一些互斥机制或使用全局枚举变量来管理当前文件状态,但是在阅读了一些帖子后,我了解到每个使用共享库的应用程序都会在 memory 中创建自己的副本,并且它们不共享任何memory 段工作期间。

Shared library whould be responsible for read file containing database and write differences into it.共享库负责读取包含数据库的文件并将差异写入其中。

That is possible but quit complicated solution.这是可能的,但退出复杂的解决方案。

While you would need to make sure that multiple processes do not interfere with each other.虽然您需要确保多个进程不会相互干扰。 It's possible to do so with file logs (see man flock ), and record locking in man fcntl but you must insure that multiple processes update disjoint file chunks "in place" (without resizing the file).可以使用文件日志(请参阅manflock )执行此操作,并在man fcntl中记录锁定,但您必须确保多个进程“就地”更新不相交的文件块(无需调整文件大小)。

This is also prone to deadlocks -- if one of the processes takes a lock on a region and then goes into infinite loop, other processes may get stuck as well.这也容易出现死锁——如果其中一个进程锁定一个区域,然后进入无限循环,其他进程也可能会卡住。

A much simpler solution involves client-server, where the server implements all writes and clients send it read and modify requests.一个更简单的解决方案涉及客户端-服务器,其中服务器实现所有写入,客户端向其发送读取和修改请求。

PS There are existing libraries implementing either approach. PS 现有的库实现了这两种方法。 You will likely save yourself several months of development time using existing solutions.使用现有解决方案,您可能会节省几个月的开发时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM