两个不同的C程序正在访问文件 [英] Two different C programs are accessing a file

查看:37
本文介绍了两个不同的C程序正在访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux平台上有一个应用程序,该应用程序需要服务器程序才能将数据连续写入bin文件中.同时,另一个程序需要读取写入的值.如果在读取和写入过程中未锁定文件,是否应该担心?

I have an application on Linux platform which requires a server program to write data to a bin file continuously. At the same time another program needs to read the written values. Should I be concerned if I am not locking the file during the read and the write process?

推荐答案

您应该关注.我假设您确定没有其他程序(问题中提到的两个可执行文件除外)正在访问该文件.您确实应该锁定以序列化该访问.使用 flock(2)

You should be concerned. I assume you are sure that no other program (than the two executables mentioned in your question) are accessing that file. You should indeed lock to serialize that access. Use flock(2), or lockf(3) which uses fcntl(2)

顺便说一句,文件是按顺序读取和写入的吗?您是否考虑过使用一些更高级的东西,例如 GDBM 或某些数据库,例如 postgresql

BTW, is the file read and written sequentially? Did you consider using some higher-level thing, e.g. GDBM or some database like mariadb or postgresql or mongodb, etc...

这篇关于两个不同的C程序正在访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆