Linux/c++ 多线程写文件时如何正确使用flockfile/funlockfile

lazysmile 2012-05-15 05:04:14
最近在一个多线程文件上传服务里想做一个文件锁控制多用户同时写入同样的文件而产生冲突。但是我在一个线程里使用flockfile锁住打开的文件句柄进行写动作的同时,启动另外一个线程打开相同的文件的句柄还是可以获得文件锁并进行写入操作,本来以为第二个线程使用ftrylockfile会失败的,有点搞不清状况。希望有高手指点一下这几个函数的用法。谢谢
...全文
1290 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lazysmile 2013-06-24
  • 打赏
  • 举报
回复
谢谢各位,明白了
qq120848369 2013-06-20
  • 打赏
  • 举报
回复
文件锁在内核里是PID相关的, 只能用作进程间互斥.
www_adintr_com 2013-06-20
  • 打赏
  • 举报
回复 1
flockfile 是针对文件句柄的, 不是针对文件的. 在不同的线程中, 对同一个 FILE* 使用可以. 但是在其它线程中重新打开的新的 FILE*, flockfile 就锁不住了.
黎明VS黑暗 2013-06-20
  • 打赏
  • 举报
回复
Boost库中对file_lock解释如下,它只支持不同进程间的,不支持同一个进程不同线程间的 //!A file lock, is a mutual exclusion utility similar to a mutex using a //!file. A file lock has sharable and exclusive locking capabilities and //!can be used with scoped_lock and sharable_lock classes. //!A file lock can't guarantee synchronization between threads of the same //!process so just use file locks to synchronize threads from different processes.
赵4老师 2012-05-16
  • 打赏
  • 举报
回复
仅供参考:
Windows下:
_locking
Locks or unlocks bytes of a file.

int _locking( int handle, int mode, long nbytes );

Routine Required Header Optional Headers Compatibility
_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:

EACCES

Locking violation (file already locked or unlocked).

EBADF

Invalid file handle.

EDEADLOCK

Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.

EINVAL

An invalid argument was given to _locking.

Parameters

handle

File handle

mode

Locking action to perform

nbytes

Number of bytes to lock

Remarks

The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.

mode must be one of the following manifest constants, which are defined in LOCKING.H:

_LK_LOCK

Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.

_LK_NBLCK

Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.

_LK_NBRLCK

Same as _LK_NBLCK.

_LK_RLCK

Same as _LK_LOCK.

_LK_UNLCK

Unlocks the specified bytes, which must have been previously locked.

Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.

Example

/* LOCKING.C: This program opens a file with sharing. It locks
* some bytes before reading them, then unlocks them. Note that the
* program works correctly only if the file exists.
*/

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
int fh, numread;
char buffer[40];

/* Quit if can't open file or system doesn't
* support sharing.
*/
fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO,
_S_IREAD | _S_IWRITE );
if( fh == -1 )
exit( 1 );

/* Lock some bytes and read them. Then unlock. */
if( _locking( fh, LK_NBLCK, 30L ) != -1 )
{
printf( "No one can change these bytes while I'm reading them\n" );
numread = _read( fh, buffer, 30 );
printf( "%d bytes read: %.30s\n", numread, buffer );
lseek( fh, 0L, SEEK_SET );
_locking( fh, LK_UNLCK, 30L );
printf( "Now I'm done. Do what you will with them\n" );
}
else
perror( "Locking failed\n" );

_close( fh );
}


Output

No one can change these bytes while I'm reading them
30 bytes read: /* LOCKING.C: This program ope
Now I'm done. Do what you will with them


File Handling Routines

See Also _creat, _open
lazysmile 2012-05-16
  • 打赏
  • 举报
回复
一个线程使用flockfile锁住后开始写,此时另一个文件尝试ftrylockfile的时候仍然还会成功,这就是我觉得难以理解的地方
lazysmile 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

一个线程锁住了,另一个线程肯定访问失败,你的要求是冲突的,一要锁,二要并发写,如果要并发写,锁它干什么?
[/Quote]
我只想锁住以禁止并发写的情况。现在的问题是锁不住
一叶之舟 2012-05-16
  • 打赏
  • 举报
回复
一个线程锁住了,另一个线程肯定访问失败,你的要求是冲突的,一要锁,二要并发写,如果要并发写,锁它干什么?
lazysmile 2012-05-16
  • 打赏
  • 举报
回复
继续求解
源码链接: https://pan.quark.cn/s/a4b39357ea24 微信小程序是由腾讯公司设计的一种轻量级应用开发平台,主要面向移动设备,旨在为用户带来无需下载安装即可直接使用的应用服务体验。在微信小程序的开发实践中,"模仿美团菜单 swiper分类菜单"是一项常见的技术应用,其目的是达成与美团外卖应用相似的交互模式,使用户能够轻松地浏览和挑选各种商品或服务。 在微信小程序的技术体系中,`swiper`组件属于轮播图功能模块,通常用于呈现多张图片或卡片式的切换界面,能够达成动态滑动展示不同分类菜单的功能。在模拟美团菜单的应用情境下,`swiper`组件一般与`swiper-item`组件协同工作,每一个`swiper-item`对应一个分类,用户可通过左右滑动来切换不同的分类菜单。 为了达成这样的功能,首先需要在微信小程序的`json`配置文档中引入`swiper`组件,并在`wxml`结构文档中构建相应的布局代码,将每个分类以`swiper-item`的形式进行组织。同时,需要在`wxss`样式文档中定义恰当的样式,保证分类菜单的视觉呈现符合设计规范。 接下来,我们将在`js`逻辑文档中处理`swiper`的切换逻辑,通过监听`bindchange`事件来识别当前选中的分类,并根据这一信息动态获取对应的子菜单信息。为了达成类似美团的层级菜单功能,可能还需要借助`picker`或`navigator`组件来展示二级分类或商品详情。 在开发阶段,需要关注以下关键点: 1. 数据绑定:保证数据与界面之间的双向链接准确无误,以便在选择分类时即时更新显示内容。 2. 动态加载:针对大量数据,可能需要采取异步加载机制,防止一次性加载过多内容引发性能瓶颈...

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧