File Copy

Note: These samples do only work on Windows NT !

Sample 1

This single-threaded version shows how to multiplex I/O to a number of files with a single thread.

This is the most efficient mechanism if you do not need the asynchronous completion model that the dual-threaded version offers.
Only one thread and one I/O completion port is used. The file handles for the source and destination file are both associated with the same port. The thread starts off by posting a number of overlapped reads from the source file. It then waits on the I/O completion port.
Whenever a read completes, it immediately turns it around into a write to the destination file. Whenever a write completes, it immediately posts the next read from the source file.

Sample 2

Usage of Completion Ports 

Intended to demonstrate how to complete I/O in a different thread   asynchronously than the I/O was started from. This is useful for people who want a more powerful mechanism of asynchronous completion callbacks than Win32 provides (i.e. VMS developers who are used to ASTs).
Two threads are used. The first thread posts overlapped reads from the source file. These reads complete to an I/O completion port the second thread is waiting on. The second thread sees the I/O completion and posts an overlapped write to the destination file. The write completes to another I/O completion port that the first thread is waiting on. The first thread sees the I/O completion and posts another overlapped read.

 

Sample 3

Usage of Fibers 

This example implements a fiber based file copy operation. Note that a number of different techiques exists for copying files programmatically; this sample simply illustrates the fiber APIs.
This example makes use of a fiber data structure which is used to determine the behavior and state of the fiber. One data structure exists for each fiber; the pointer to the data structure is passed to the fiber at fiber creation time via the lpParameter fiber parameter.
The executing thread in the process makes a call to ConvertThreadToFiber, which allows fibers to be scheduled by the caller. This also allows the resultant fiber to be scheduled by another fiber.
Next, two additional fibers are created, one fiber which performs read operations against a specified file, and another fiber which performs the write operations against a specified file.
The primary fiber then schedules the read fiber. After a succesful read, the read fiber schedules the write fiber. After a succesful write
in the write fiber, the write fiber schedules the read fiber. When the read/write cycle has completed, the primary fiber is scheduled, which results in the display of the read/write status. If an error occurs during the read or write fibers, the primary fiber is scheduled and status of the read/write is displayed.
The fibers, fiber data structures, and file handles are then freed prior to process termination.

 

If you experience problems while compiling sample 3 try adding _WIN32_WINNT=0x0400 to the "defines" of your project settings. If you are developing for windows 2000 add _WIN32_WINNT=0x0500

One Step Ahead Software

 

Feel free to contact us for general information, questions or comments