Hi all,
I have a question. Can two processes (not same domain) read/write to a file without causing any data lost. Wont be able to implement lock resouce because it's separate processes. And also assume mutex wasnt used at both processes.
1. Another OS specific que, when we mention file handle, does OS assign only one handle for each file - reflecting at abovementioned scenario.
Pls advice Gurus out thr. TQ in advance.
topgun
Can two processes (not same domain) read/write to a file without causing any data los
Started by topgun_879, Sep 01 2010 05:33 PM
6 replies to this topic
#1
Posted 01 September 2010 - 05:33 PM
|
|
|
#2
Posted 01 September 2010 - 11:58 PM
When I try to open a file in ReadWrite mode with two processes I get this:
Unhandled Exception: System.IO.IOException: The process cannot access the file 'D:\Visual Studio 2010\Projects\TestApp\TestApp\bin\Release\myfile.txt' because it is being used by another process.
Unhandled Exception: System.IO.IOException: The process cannot access the file 'D:\Visual Studio 2010\Projects\TestApp\TestApp\bin\Release\myfile.txt' because it is being used by another process.
#3
Posted 02 September 2010 - 03:16 AM
Both processes can write to the file at the same time if both of them have opened the file with FileShare.ReadWrite or FileShare.Write flag. But then data loss may occur if processes have not been synchronized, but rather tried to write data simultaneously. If both processes try to do append operation, data may be interlaced. Data may also be written over if processes use Seek operation without negotiating the offset, in which case they both can use the same seek offset and write over each other's data.
It is not advised to open file for writing and allow other processes to do the same. This means that normal operation is either to open file with FileShare.None (exclusive access) or FileShare.Read (allowing others to read what process is writing, but not to write at the same time).
It is not advised to open file for writing and allow other processes to do the same. This means that normal operation is either to open file with FileShare.None (exclusive access) or FileShare.Read (allowing others to read what process is writing, but not to write at the same time).
#4
Posted 02 September 2010 - 03:22 AM
Momerath said:
When I try to open a file in ReadWrite mode with two processes I get this:
Unhandled Exception: System.IO.IOException: The process cannot access the file 'D:\Visual Studio 2010\Projects\TestApp\TestApp\bin\Release\myfile.txt' because it is being used by another process.
Unhandled Exception: System.IO.IOException: The process cannot access the file 'D:\Visual Studio 2010\Projects\TestApp\TestApp\bin\Release\myfile.txt' because it is being used by another process.
#5
Posted 20 September 2010 - 10:04 PM
Thank you very much for your technical insight, Zohanh. This really makes sense now.
How about the OS file handle. Let 's assume two different processes have opened a single file. Does this means, OS allocated two different handles for the same file or just one used by both processes? Thank you in advance.
Rgds,
topgun
How about the OS file handle. Let 's assume two different processes have opened a single file. Does this means, OS allocated two different handles for the same file or just one used by both processes? Thank you in advance.
Rgds,
topgun
#6
Posted 21 September 2010 - 01:22 AM
Hi,
Looks like, no matter how, if one process is writing to a file, another process cannot access the file. Tried all FileMode and FileShare at both app (writter& reader), it is still failing with exception on the second process.
Can anyone suggest somehting for this problem. Thank you.
Rgds,
topgun
Looks like, no matter how, if one process is writing to a file, another process cannot access the file. Tried all FileMode and FileShare at both app (writter& reader), it is still failing with exception on the second process.
Can anyone suggest somehting for this problem. Thank you.
Rgds,
topgun
#7
Posted 21 September 2010 - 02:11 AM
Here is the code which reads and writes to a file:
See how the file has been opened by reader and how by the writer.
Before every run, delete file.txt.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static void Read(string path)
{
FileInfo fi = new FileInfo(path);
FileSystemWatcher fsw = new FileSystemWatcher(fi.DirectoryName, fi.Name);
_readingStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_readingStream.Seek(0, SeekOrigin.End);
fsw.Changed += new FileSystemEventHandler(FileChanged);
fsw.NotifyFilter = NotifyFilters.LastWrite;
fsw.EnableRaisingEvents = true;
Thread.Sleep(60000);
}
static void FileChanged(object sender, FileSystemEventArgs e)
{
List<byte> rawData = new List<byte>();
byte[] buffer = new byte[1024];
int len = 0;
do
{
len = _readingStream.Read(buffer, 0, buffer.Length);
for (int i = 0; i < len; i++)
rawData.Add(buffer[i]);
}
while (len > 0);
byte[] data = rawData.ToArray();
char[] chars = UTF8Encoding.UTF8.GetChars(data);
string text = new string(chars);
Console.Write(text);
}
static void Write(string path)
{
while (true)
{
FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read);
int l = RNG.Next(70) + 1;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < l; i++)
sb.AppendFormat("{0}", i % 10);
sb.Append(Environment.NewLine);
string line = sb.ToString();
char[] chars = line.ToCharArray();
byte[] bytes = UTF8Encoding.UTF8.GetBytes(chars);
fs.Write(bytes, 0, bytes.Length);
fs.Dispose();
int pause = RNG.Next(1001);
Thread.Sleep(pause);
}
}
static void Main(string[] args)
{
string path = "file.txt";
if (File.Exists(path))
Read(path);
else
Write(path);
}
private static Random RNG = new Random();
static FileStream _readingStream;
}
}
Run multiple instances of this process. First instance will create the file named file.txt. Other instances will read from the same file. You'll see that all readers will print content of the file as it is written by the first instance.See how the file has been opened by reader and how by the writer.
Before every run, delete file.txt.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









