Asynchronous file IO

xiaoxiao2021-03-06  47

Asynchronous file I / O

Synchronous I / O means that the method is blocked before the I / O operation is completed, and the method returns its data after the I / O operation is completed. Using asynchronous I / O, users can call BeginRead or BeGinWrite. The main thread can continue other work, later, the user will be able to process data. In addition, multiple I / O requests can be hang at the same time.

To get notifications when this data is available, you can call EndRead or endwrite to pass the IASYNCRESULT corresponding to the I / O request you sent. You can also provide a callback method that calls EndRead or endWrite to calculate how many bytes of reading or writing. When many I / O requests are hang, asynchronous I / O can provide better performance, but usually require some important adjustments to your application to work properly.

The Stream class supports mixing synchronous and asynchronous reading and writing on the same stream, regardless of whether the operating system is allowed. STREAM provides the default asynchronous reading and write operations in accordance with its synchronous implementation, and provides the implementation of the default synchronous read and write operations based on its asynchronous implementation.

When implementing a Stream's derived class, you must provide implementation for synchronous or asynchronous READ and WRITE methods. While it is allowed to rewrite Read and Write, and the default implementation of the asynchronous method (Beginread, Endread, BeginWrite and EndWrite) will work with the implementation of the synchronization method, this cannot provide the most effective performance. Similarly, if you provide an implementation of asynchronous methods, synchronous read and write methods will work normally, but if you specifically implement synchronization methods, performance is usually better. The default implementation of ReadByte and WritebyTe calls with a synchronization READ and WRITE method with an element byte array. When derived from the stream, if there is an internal byte buffer, it is highly recommended to rewrite these methods to access the internal buffer, which will be significantly improved.

Connecting to the stream weight of the backup memory or one of the asynchronous READ and WRITE methods to get the functionality of another method by default. If the flow does not support asynchronous or synchronous operations, the implementator only needs an appropriate method to trigger an exception.

The following example is an asynchronous implementation of a hypothetical image processor that is subsequently implemented. This code is used to execute the operation of the CPU resource on each file in the directory. For more information, see the ".NET Asynchronous Programming Model" topic in the .NET Framework developer specification.

[C #]

Using system;

Using system.io;

Using system.threading;

Using system.Runtime.InteropServices;

Using system.runtime.remoting.Messaging;

Public Class BulkimageProcasync

{

Public const string imagebasename = "tmpimage-";

Public const INT NumImages = 200;

Public const Int numpixels = 512 * 512;

// processimage has a simple o (n) loop, and you can vary the number

// of Times You Repeat That Loop To Make The Application More CPU-

// bound or more io-bound.

Public static int processimageRepeats = 20;

// Threads Must Decrement NumImageStofinish, and protect // their access to it through a mutex.

Public static int numimagestofinish = NumImages;

Public static object numimagesMutex = new object [0];

// WaitObject is Signalled When All Image Processing IS DONE.

Public static Object WaitObject = New Object [0];

Public Class ImageStateObject

{

PUBLIC BYTE [] PIXELS;

Public int imageum;

PUBLIC FILESTREAM FS;

}

Public static void makeimagefiles ()

{

INT SIDES = (int) Math.sqrt (Numpixels);

Console.write ("Making" NumImages " SIDES " X " SIDES

"Images ...");

Byte [] pixels = new byte [numpixels];

For (int i = 0; i

Pixels [i] = (byte) i;

For (int i = 0; i

{

FILESTREAM FS = New FileStream (ImageBaseName i ". TMP",

FileMode.create, FileAccess.write, Fileshare.none,

8192, False);

Fs.write (Pixels, 0, Pixels.length);

FlushfileBuffers (fs.handle);

fs.close ();

}

Console.writeline ("DONE.");

}

Public Static Void ReadinImageCallback (IasyncResult AsyncResult)

{

ImagestateObject State = (imagestateObject) asyncResult.asyncState;

Stream stream = state.fs;

INT BYTESREAD = stream.endread (asyncREAD);

IF (BytesRead! = Numpixels)

Throw New Exception ("in ReadinImageCallback, Got The Wrog

Number of bytes from the image: {0}. ", bytesread;

ProcessImage (state.pixels, state.imagenum);

stream.close ();

// Now write out the image.

// USING Asynchronous I / O Here APPEARS NOT To BE BEST PRACTICE.

// IT ends up swamping the threadpool, Because the Threadpool

// Threads Are Blocked on I / O Requests That Were Just Queued To

// THREADPOOL.FILESTREAM FS = New

FILESTREAM (ImageBaseName State.imagenum ". Done,

FileMode.create, FileAccess.write, Fileshare.none, 4096,

FALSE);

fs.write (state.pixels, 0, numpixels);

fs.close ();

// this application model Uses Too Much Memory.

// Releaseing Memory as soon as possible is a good idea,

// especially global state.

State.pixels = null;

// Record That an image is finished now.

LOCK (NumImagesmutex)

{

NumImagestofinish -;

IF (NumImagestofinish == 0)

{

Monitor.enter (WaitObject);

Monitor.pulse; WaitObject);

Monitor.exit (WaitObject);

}

}

}

Public static void processimage (byte [] pixels, int imagenu

{

Console.WriteLine ("ProcessImage";

// Perform Some CPU-Intensive Operation On The Image.

For (int i = 0; i

For (int J = 0; J

Pixels [J] = 1;

Console.writeline ("ProcessImage" ImageNum "DONE.");

}

Public Static Void ProcessImagesNbulk ()

{

Console.writeline ("Processing Images ...");

Long t0 = environment.tickcount;

NumImagestofinish = NumImages;

AsyncCallback readimageCallback = New

AsyncCallback (ReadinImageCallback);

For (int i = 0; i

{

ImagestateObject State = new imagestateObject ();

State.pixels = new byte [numpixels];

State.imagenum = i;

// Very Large Items Are Read Only ONCE, SO You CAN Make THE

// Buffer on the filestream very small to save memory.

FILESTREAM FS = New FileStream (ImageBaseName i ". TMP",

Filemode.open, FileAccess.read, Fileshare.Read, 1, True;

State.fs = fs;

Fs.beginread (state.pixels, 0, numpixels, readimagecallback, state);

}

// determine WHETHER ALL IMAGES Are Done Being Processed.

// if NOT, Block Until All Are Finished.

Bool Mustblock = FALSE;

LOCK (NumImagesmutex)

{

IF (NumImagestofinish> 0)

Mustblock = true;

}

IF (MustBlock)

{

Console.writeline ("All worker threads are queued. Blocking

Until The completion. Numleft: {0}. ", NumImagestofinish;

Monitor.enter (WaitObject);

Monitor.wait (WaitObject);

Monitor.exit (WaitObject);

}

Long t1 = environment.tickcount;

Console.writeline ("Total Time Processing images: {0} MS",

(T1-T0));

}

Public static void cleanup ()

{

For (int i = 0; i

{

File.delete (ImageBaseName i ". TMP");

File.delete (ImageBaseName i ". DONE");

}

}

Public static void trytocleardiskcache ()

{

// Try to force all pending writes to disk, and clear the

// Disk cache of any data.

Byte [] bytes = new byte [100 * (1 << 20)];

For (int i = 0; i

BYTES [I] = 0;

BYTES = NULL;

Gc.collect ();

Thread.sleep (2000);

}

Public static void main (string [] args)

{

Console.writeline ("Bulk Image Processing Sample Application,

Using async io. ");

Console.Writeline ("Simulates Applying A Simple Transformation To

" NumImages " / "images /");

Console.writeline ("(Async Filestream & Threadpool Benchmark");

Console.Writeline ("Warning - this Test Requires" (Numpixels *

NumImages * 2) "Bytes of Temporary Space");

IF (args.length == 1)

{

ProcessimageRepeats = int32.parse (Args [0]);

Console.writeline ("ProcessImage Inner loop - {0}.",

ProcessimageRepeats;

}

MakeImagefiles ();

Trytocleardiskcache ();

ProcessImagesNbulk ();

Cleanup ();

}

[DLLIMPORT ("kernel32", setLastError = true)]

Private static extern void flushfilebuffrs (intptr handle);

}

The following is a synchronization example of the same hypothesis.

[C #]

Using system;

Using system.io;

Using system.threading;

Using system.Runtime.InteropServices;

Using system.runtime.remoting.Messaging;

Public Class BulkimageProcsync

{

Public const string imagebasename = "tmpimage-";

Public const INT NumImages = 200;

Public const Int numpixels = 512 * 512;

// processimage has a simple o (n) loop, and you can vary the number

// of Times You Repeat That Loop To Make The Application More CPU-

// bound or more io-bound.

Public static int processimageRepeats = 20;

Public static void makeimagefiles ()

{

INT SIDES = (int) Math.sqrt (Numpixels);

Console.write ("MAKING" NumImages " SIDES " X " SIDES "

Images ... ");

Byte [] pixels = new byte [numpixels];

For (int i = 0; i

Pixels [i] = (byte) i;

For (int i = 0; i

{

FILESTREAM FS = New FileStream (ImageBaseName i ". TMP",

FileMode.create, FileAccess.write, Fileshare.none,

8192, False);

Fs.write (Pixels, 0, Pixels.length);

FlushfileBuffers (fs.handle);

fs.close ();

}

Console.writeline ("DONE.");

}

Public static void processimage (byte [] pixels, int imagenu

{

Console.WriteLine ("ProcessImage";

// Perform Some CPU-Intensive Operation On The Image.

For (int i = 0; i

For (int J = 0; J

Pixels [J] = 1;

Console.writeline ("ProcessImage" ImageNum "DONE.");

Public Static Void ProcessImagesNbulk ()

{

Console.writeline ("Processing Images ...");

Long t0 = environment.tickcount;

Byte [] pixels = new byte [numpixels];

For (int i = 0; i

{

FILESTREAM INPUT = New FileStream (ImageBaseName i ". TMP",

FileMode.Open, FileAccess.read, Fileshare.Read,

4196, False;

Input.read (Pixels, 0, Numpixels);

INPUT.CLOSE ();

ProcessImage (Pixels, i);

FILESTREAM OUTPUT = New FileStream (ImageBaseName i ", DONE",

FileMode.create, FileAccess.write, Fileshare.none,

4196, False;

Output.write (Pixels, 0, Numpixels);

Output.close ();

}

Long t1 = environment.tickcount;

Console.writeline ("Total Time Processing images: {0} MS",

(T1-T0));

}

Public static void cleanup ()

{

For (int i = 0; i

{

File.delete (ImageBaseName i ". TMP");

File.delete (ImageBaseName i ". DONE");

}

}

Public static void trytocleardiskcache ()

{

Byte [] bytes = new byte [100 * (1 << 20)];

For (int i = 0; i

BYTES [I] = 0;

BYTES = NULL;

Gc.collect ();

Thread.sleep (2000);

}

Public static void main (string [] args)

{

Console.writeline ("Bulk Image Processing Sample Application,

Using Synchronous I / O ");

Console.Writeline ("Simulates Applying A Simple Transformation To

" NumImages " / "images /");

Console.writeline ("(IE, Sync FileStream Benchmark));

Console.Writeline ("Warning - this Test Requires" (Numpixels *

NumImages * 2) "Bytes of TMP Space");

IF (args.length == 1) {

ProcessimageRepeats = int32.parse (Args [0]);

Console.writeline ("ProcessImage Inner loop -

" processimagerepection;

}

MakeImagefiles ();

Trytocleardiskcache ();

ProcessImagesNbulk ();

Cleanup ();

}

[DLLIMPORT ("kernel32", setLastError = true)]

Private static extern void flushfilebuffrs (intptr handle);

}

转载请注明原文地址:https://www.9cbs.com/read-80742.html

New Post(0)