Asynchronous file operations for C #

Source: Internet
Author: User
Tags benchmark requires sleep thread

First of all, the main difference between synchronous and asynchronous operations. In a synchronous I/O operation, the method remains in the waiting state until the I/O operation completes. In asynchronous I/O operations, when I/O is started, the program's methods can be transferred to perform other operations, greatly increasing the efficiency of program execution.

Because Windows is a multitasking operating system, the system may receive multiple I/O operation requests at the same time, requiring a variety of operations on disk files. If you use synchronization, you can have at most one I/O operation at a time, while other tasks are waiting, and the utilization of the system will be greatly reduced. asynchronous I/O operations are a good solution to this performance problem.

The stream class supports either synchronous read-write or asynchronous read-write in the same stream. Stream class is an abstract class, it provides us with BeginRead, BeginWrite, Endreader, EndWrite, read, write, Seek, and other member methods, in conjunction with the completion of the reading and writing operation of the convection. All of these methods are virtual methods. Therefore, when we design a derived class of the stream class ourselves, we should overload these methods in the member methods read and write of the class for reading and writing, and design their synchronous and asynchronous execution code. The Beginread,endread,beginwrite and EndWrite methods provide an asynchronous read-write operation by default, and if your derived class's read and write methods perform a synchronous operation, then the program does not provide a good efficiency. Only when they perform asynchronous operations can we effectively improve the execution efficiency of the program.

The Stream class also provides a ReadByte and WriteByte method for one read-write, when we need to write our own method to throw an exception.

The following code is in the. NET online Help, an example of an asynchronous read-write operation that simulates the work of a multiprocessor system.

Program Listing 17-8:

Using System;
Using System.IO;
Using System.Threading;
Using Benchutil;  public class bulkimageprocasync{Public const String imagebasename= ' tmpimage-'; public const int numimages=200;
    const int numpixels=512*512; Processimage has a simple O (N) Loop,and We can vary the number//of times we repeat this loop to make the app more CP
U-bound or more io-bound.
   public static int processimagerepeats=20;
   Threads must decrement Numimagestofinish,and protect//their access to it via a mutex.
   public static int numimagestofinish=numimages;
   public static Object numimagesmutex=new object[0];
   The waitobject is signalled as all image processing are done.
   public static Object waitobject=new object[0];
Internal static Perftimer Pf=new Perftimer ("Asynchronous Bulk Image Processor");
         public class imagestateobject{public byte[] pixels;
public int imagenum;
 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<numpixels;i++) pixels[i]= (byte) i; for (int i=0;i<numimages;i++) {FileStream fs=new FileStream (imagebasename+i+ ". tmp", Filemode.create,filea Ccess.
          Write,fileshare.none,8192,false); Fs. Write (pixes,0,pixels.
     Length); FlushFileBuffers (fs.
          GetHandle ()); Fs.
     Close ();
} Console.WriteLine ("Done."); public static void Readinimagecallback (IAsyncResult asyncresult) {imagestateobject state= (imagestateobject)
Asyncresult.asyncstate;
Console.WriteLine ("Image" +state.imagenum+ "was read.) "+ (asyncresult.completedsynchronously?")
  Synchronously ":" asyncchronously "));
  Stream stream= (stream) Asyncresult.asyncobject; int Bytesread=stream.
  EndRead (asyncresult); if (bytesread!=numpixels) throw new Exception ("in readinimagecallback,got wrong number of bytes to the image!
  Got: "+bytesread);   Processimages (State.pixels,state.imagenum); Stream. ClOSE ();
  Now write out the image. Using async IO Here probably swamps threadpool,since//there are blocked ThreadPool threads on Soon-to-be-//spawne D//threadpool Threads FileStream fs=new FileStream (imagebasename+state.imagenum+ ". Done", filemode.create,fileaccess .
  Write,fileshare.none,4096,false); Fs.
  Write (State.pixels,0,numpixels); IAsyncResult Writeresult=fs.  Beginwrtie (State.pixels,//0,numpixels,null,null); Fs.
  EndWrite (Writeresult); Fs.
  Close ();
  Release memory as soon as possible,especially global state.  State.pixels=null;
  The record, that, image is done now.
    Lock (Numimagemutex) {numimagestofinish--;
       if (numimagestofinish==0) {monitor.enter (waitobject);
       Monitor.pulse (waitobject);
     Monitor.Exit (waitobject);
 }} public static void Processimage (byte[] pixels,int imagenum) {//console.writeline ("processimage" +imagenum);
 Do some cpu-intensive operation on the image. for (int i=0;i<processimagereports;i++) for (inT j=0;j<numpixels;j++) pixels[j]+=1;
Console.WriteLine ("Processimage" +imagenum+ "done.");}
  public static void Processimagesinbulk () {Console.WriteLine ("Processing images ...");
  int Timer=pf.starttimer ("processimages");
  int Timer=pf.starttimer ("Total time");
  Numimagestofinish=numimages;
  AsyncCallback readimagescallback=new AsyncCallback (Readinimagecallback);
      for (int i=0;i<numimages;i++) {imagestateobject state=new imagestateobject ();
      State.pixels=new Byte[numpixels];
    State.imagenum=i;
  Because very large items are read only once,the buffer//on the file stream can is very small to save memory.
  FileStream fs=new FileStream (imagebasename+i+ ". tmp", FileMode.Open, Fileaccess.read,fileshare.read,1,true); Fs.
  BeginRead (state.pixels,0,numpixels,readimagecallback,state);
  }//ensure all image processing are done.
  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 they complete.
       Numleft: "+numimagestofinish); 
       Monitor.Enter (waitobject);
       Monitor.Wait (waitobject);
   Monitor.Exit (waitobject);
    } pf.stoptimer (timer);
  Pf.outputstoppedtime ();
         public static void Cleanup () {for (int i=0;i<numimages;i++) {file.delete (imagebasename+i+ ". tmp");
     File.delete (imagebasename+i+ ". Done"); } public static void Trytocleardiskcache () {//try to force all pending writes to Disk,and to clear the//d  ISK cache of any data.
     Byte[] bytes=new byte[100* (1<<20)]; for (int i=0;i<bytes.
     length;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 ASYCN I
     O "); Console.WriteLine ("Simulates applying a simple transformation to" +numimages+ "\" imgAes\ "");
     Console.WriteLine ("Ie,async FileStream & Threadpool 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-" +processimagerepeats);
     } makeimagefiles ();
         Trytocleardiskcache ();
     Processimageinbulk ();
  Cleanup ();
[DllImport ("KERNEL32", setlasterror=true)] static extern void flushfilebuffers (int handle);

This is a program that uses synchronous methods to achieve the same function.
Program List 17-9: Using System;
Using System.IO;
Using System.Threading;
Using Benchutil; public class Bulkimageprocsync {public Const String imagebasename= ' tmpimage-'; public const int numimages=200; public Co
 NST int numpixels=512*512; Processimage has a simple O (N) Loop,and We can vary the number//of times we repeat this loop to make the app more cpu-
Bound or more io-bound.
public static int processimagerepeats=20; Internal Static PeRftimer pf=new Perftimer ("Synchronous Bulk Image Processor");
  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<numpixels;i++) pixels[i]= (byte) i; for (int i=0;i<numimages;i++) {FileStream fs=new FileStream (imagebasename+i+ ". tmp", FileMode.Create, FileAccess.Write
     , Fileshare.none,8192,false); Fs. Write (pixels,0,pixels.
     Length); FlushFileBuffers (fs.   GetHandle ()); Fs.
    Close ();
  } Console.WriteLine ("Done.");
    public static void Processimage (byte[] pixels,int imagenum) {//console.writeline ("processimage" +imagenum); Do some cpu-intensive operation on the "image for" (int i=0;i<processimagereports;i++) for (int j=0;j<numpixels;j
    + +) Pixels[j]+=1;
  Console.WriteLine ("Processimage" +imagenum+ "//done.");} public static void Processimagesinbulk () {Console.WriteLine ("Processing images ...");
    int Timer=pf.starttimer ("Total time");
    Byte[] Pixels=new Byte[numpixels]; for (int i=0;i<numimages;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 ();
   } pf.stoptimer (timer);
 Pf.outputstoppedtime ();
  public static void Cleanup () {for (int i=0;i<numimages;i++) {file.delete (imagebasename+i+ ". tmp");
   File.delete (imagebasename+i+ ". Done");
   The public static void Trytocleardiskcache () {bute[] bytes=new byte[100* (1<<20)]; for (int i=0;i<bytes.
   length;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 IO ");
  
    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-" +processimagerepeats);
  } makeimagefiles ();
     Trytocleardiskcache ();
     Processimagesinbulk ();
  Cleanup ();
[DllImport "KERNEL32", setlasterror=true)] static extern void flushfilebuffers (int handlw); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.