进程之间并发文件写入 [英] Concurrent File write between processes

查看:247
本文介绍了进程之间并发文件写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将来自不同进程的日志数据写入单个文件.

I need to write log data into a single file from different processes.

我使用的是Windows Mutex,它需要公共语言运行时支持.

I am using Windows Mutex which needs Common Language Runtime support for it.

Mutex^ m = gcnew Mutex( false,"MyMutex" );
m->WaitOne();
//... File Open and Write ..
m->ReleaseMutex()

我真的需要从C ++更改为C ++/CLI进行同步吗?

Do I really need to change from C++ to C++/CLI for synchronization?

如果不使用原子就可以了.但是我需要知道与本地互斥锁相比,使用此互斥锁是否会降低性能.

It is ok if the atomic is not used. But I need to know whether using this Mutex will slow down the performance compared to local mutex.

推荐答案

仅仅为了获取Mutex类而向您的C ++应用程序添加CLR支持是过分的.您可以使用几个选项在两个应用程序之间同步文件访问.

Adding CLR support to your C++ application just to get the Mutex class is overkill. There are several options available to you to synchronize your file access between two applications.

如果您需要从多个进程中写入文件,则使用互斥锁是一种很好的方法.使用互斥体

If you need to write a file from multiple processes, using a mutex is a good way to do it. Use the mutex functions in the Win32 API. (The .Net Mutex class is just a wrapper around those functions anyway.)

HANDLE mutex = CreateMutex(NULL, false, "MyMutex");

DWORD waitResult = WaitForSingleObject(mutex, INFINITE);
if (waitResult == WAIT_OBJECT_0)
{
    // TODO: Write the file
    WriteFile(...);

    ReleaseMutex(mutex);
}

正如另一个答案所述,您将需要通过共享打开文件,以便两个应用程序都可以一次打开它.但是,仅凭这本身可能还不够:如果您的两个应用程序都试图写入文件的同一区域,那么您仍然需要确保一次仅写入一个应用程序.想象一下,如果两个应用程序都查看文件的大小,那么它们都试图同时写入该字节偏移量:即使两个应用程序都试图仅附加到文件末尾,它们最终还是会互相干扰.

As the other answer noted, you will need to open the file with sharing, so that both of your applications can open it at once. However, that by itself may not be enough: If both of your applications are trying to write to the same area of the file, then you'll still need to make sure that only one application writes at a time. Imagine if both applications look at the size of the file, then both try to write to that byte offset at the same time: Even though both tried to just append to the end of the file, they ended up clobbering each other.

如果您纯粹是在写文件的末尾,并且从不尝试读取任何东西或在文件末尾以外的地方写东西,那么您可以使用一种特殊的模式,使您不必使用互斥锁.如果在dwDesiredAccess设置为FILE_APPEND_DATA | SYNCHRONIZE 且没有其他内容(不包括FILE_WRITE_DATA)的情况下打开文件,则操作系统将负责确保所有写入的数据到文件末尾,并且两个写数据的应用程序不会互相覆盖. MSDN :

If you're purely writing to the end of the file, and not ever attempting to read anything or to write anywhere other than the very end of the file, then there is a special mode you can use that will let you not use a mutex. If you open the file with dwDesiredAccess set to FILE_APPEND_DATA | SYNCHRONIZE and nothing else (don't include FILE_WRITE_DATA), then the OS will take care of making sure that all the data that gets written to the file at the end, and the two applications writing data do not overwrite each other. This behavior is documented on MSDN:

如果仅设置了FILE_APPEND_DATA和SYNCHRONIZE标志,则调用者只能将其写入文件的末尾,并且有关写入文件的所有偏移信息都将被忽略.但是,对于这种类型的写操作,该文件将根据需要自动扩展.

If only the FILE_APPEND_DATA and SYNCHRONIZE flags are set, the caller can write only to the end of the file, and any offset information about writes to the file is ignored. However, the file will automatically be extended as necessary for this type of write operation.

选项3:LockFile

您可以采用的另一种方法是使用LockFile方法.使用 LockFile (或雷蒙德·陈的博客上.

Option 3: LockFile

One other path you can take is to use the LockFile method. With LockFile (or LockFileEx), you can have both applications open the file, and have each app lock the section of the file that it wants to write to. This gives you more granularity than the mutex, allowing non-overlapping writes to happen at the same time. (Using LockFile on the entire file will give you the same basic effect as the mutex, with the added benefit that it will prevent other applications from writing the file while you're doing so.) There's a good example of how to use LockFile on Raymond Chen's blog.

这篇关于进程之间并发文件写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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