阅读:2887回复:8
如何加锁、等锁、解锁文件 ?
我的Filter在Create之后需要 读、写 文件的内容,在单线程的情况没有问题
在Create之后得到FileObject然后发Irp去Read、Write 但是我的测试程序一旦发起的线程多了,读写就会返回STATUS_FILE_LOCK_CONFLICT 。有时几十个线程可以,有时几个线程就不行,有时上百个线程才不行,没有规律 我的应用层测试程序 for(int i=0;i < nThreadNum;i++) { .... _beginthreadex(NULL , 0 , TestThread , context , 0 , &uThreadID); } unsigned __stdcall TestThread(LPVOID pParam) { ....... if (!WriteFile(...)) { ...... } ...... } 搜索了所有我能搜索的网站,OSR上说可以用Map的方式去读,可是还要写,不知道该怎么办 |
|
|
沙发#
发布于:2007-05-19 22:06
换句话来问:
1.如何等待正在被其他线程(IRP)锁定的文件解除锁定 ? 2.如何将一个文件进行锁定让其他线程(IRP)等待我的读写完成 ? 谢谢指导 ! |
|
|
板凳#
发布于:2007-05-20 09:20
...晕~频繁读写文件——搜索fastfat的代码看看,这个STATUS是怎么raise出来的
|
|
|
地板#
发布于:2007-05-21 17:23
顶起来,就是字节锁的原因,但如何等待解锁呢 ?
|
|
|
地下室#
发布于:2007-05-21 23:53
Re:如何处理 STATUS_FILE_LOCK_CONFLICT
Generally Byte-Range Lock is associated with process not thread, though it's possible to obtain thread-specific byte-range locks. And obviously you are facing an exclusive write lock.My suggestions are first using FileMon or FileSpy to find out which process/thread is locking bytes, why and how. And also check which thread issues MJ_CREATE. If it's a system thread, try to queue all your create/read/write into another thread. Sometimes this can solve problem. Second, you can try to construct a blocking (SL_FAIL_IMMEDIATELY=FALSE) lock request MJ_LOCK_CONTROL/IRP_MN_LOCK to lock those bytes before read or write. But this may not be a good synchronization approach. And don't forget to unlock those bytes later. Or simply open the file exclusively. |
|
5楼#
发布于:2007-05-22 17:28
太有才了,学习啊..
|
|
6楼#
发布于:2007-05-26 19:29
谢谢 michaelgz
文件驱动和过滤驱动的开发都是 陷阱式 的,从这个坑跳到那个坑 |
|
|
7楼#
发布于:2008-12-26 00:45
学习了
|
|
8楼#
发布于:2008-12-27 16:54
这个是一个好的问题!
基本上是和filelock 相关的!这个filelock 是通过api 的调用来实现 一个文件的byterange的lock的! 还有一个情况就是 文件的大小正在发生变化的时候,有一个irp 并且是异步的, 因为是异步的,所以这个请求不能等待文件大小变化的完成,所以就立即返回了 conflict 的error。 因为你没有实现filelock 机制, 所以我认为你应该是在线程里面不断的写数据 ,不断的读数据,并且读的时候是超过了上次写的文件的大小,才返回这个错误的!当然了 这个应该不是什么问题! 一旦遇到lock 的error的时候,你可以在thread里 再次读你没有成功的操作。 如果 这样的测试没有通过的话,那么毫无疑问你的驱动里面的同步肯定有问题。 我在驱动里面是实现了 filelock 和oplock,现在是没有遇到情况 ,但是还进行大量的测试。 |
|