I/O in Windows (read/write input/output)

Source: Internet
Author: User
Introduction

Write an application on Windows Program Most of the time, it is inseparable from reading and writing files and network communication.
For example, a service application may accept users' requests from the network adapter, process and compute the requests, and finally return the data required by the user end, which may also involve reading and writing the disk, these are all I/O operations. Therefore, to design a stable, efficient, and Scalable Application, you must clarify the I/O mechanism of windows.

I. Two read/write mechanisms

Input input/output, which has two mechanisms:

1. synchronous I/O: When a thread executes an input/output function, the function returnsCode.
2 asynchronous I/O: When a thread executes an input/output function, the function returns immediately after the read/write operation is completed. The thread can first execute the following statements, when performing the following operations, you can check whether the read/write operations you just initiated have been completed.

Explanation:

It can be seen that asynchronous I/O is more efficient, and synchronous I/O will block thread execution and should not be promoted.

Sometimes we think that when we execute an I/O operation each time, we open a new thread to let it execute the synchronous I/O function, and then our original thread will execute the work, this is fine, but in fact we simulate and implement asynchronous I/O. In fact, when we call asynchronous I/O functions, the operating system also maintains a new thread to perform I/O operations for us.

However, creating a new thread consumes CPU resources. In addition, when I/O operations are performed, the CPU needs to be calculated and the buffer from the disk or network adapter is required, read data to the memory, or write data in the memory to the buffer zone of the disk or network adapter. Therefore, the I/O operation thread is not sleeping, but busy, so we come to the conclusion that if our computer only has two CPUs and the number of I/O threads simultaneously exceeds two, the CPU will be allocated to the time slice because I/O threads are not sleeping, so frequent switching between threads will also reduce the performance of our applications.

Therefore, we can conclude that the number of I/O threads executed at the same time should not be greater than the number of CPUs. A queue is required for the I/O Requests initiated by the customer, the number of I/O operations processed concurrently should be equal to the number of CPUs. If too many I/O requests are processed concurrently, too frequent CPU switching will waste CPU resources on thread switching, this seriously reduces program performance.

Ii. Pre-and Post-read/write operations: Create and release the kernel objects of the devices to read/write

To read/write a device, you must first create a kernel object for reading/writing the device. After reading/writing, release the kernel object.

1. The function for creating an I/O Kernel Object: createfile (...) is the same as other kernel objects. what we get is just a handle.
2. Disable the function of the I/O Kernel Object: closehandle (...)

Explanation:

Handle createfile (
Lptstr lpfilename, // Device to read/write
DWORD dwdesiredaccess, // Access Mode: 0 (do not read/write, such as changing the device configuration) generic_read, generic_write (can be different or)
DWORD dw1_mode, // Sharing Mode: file_1__delete file_1__read file_1__write
Lpsecurity_attributes lpsecurityattributes, // You can set the inheritance of kernel objects in security attributes.
DWORD dwcreationdistribution, // Creation Method: create_new create_always open_existing open_always truncate_existing
DWORD dwflagsandattributes, // File Attributes and flag
Handle htemplatefile // File Attribute template. If this parameter is specified, the file attribute in the previous parameter is ignored.
);

/*
Parameter dwflagsandattributes
1) file attributes:
(File_attribute _) archive compressed hidden normal offline readonly system temporary
2) Logo:
(File_flag _) write_through overlapped no_buffering random_access sequential_scan delete_on_close backup_semantics posix_semantics
*/This createfile function returns a file kernel object handle. The file mentioned here is not a disk file in a narrow sense, but also contains some devices, such as serial ports, parallel ports, and mail slots, named pipelines and anonymous pipelines. In addition, some device handles are not created through createfile. The following table describes the functions used to create device kernel objects.

DeviceFunction used to create a device Kernel Object

File createfile (pszname is the path name or UNC path name ).

Logical Disk Drive createfile (pszname is \. \ x :). X is the drive letter. You can open the drive to format or check the drive size.

Physical disk drive createfile (pszname is \. \ physicaldrivex ). X is the serial number of the physical drive, such as physicaldrive0.

Serial createfile (pszname is "comx ")

Concurrent createfile (pszname is "lptx ")

Createmailslot (pszname is \. \ Mailslot \ mailslotname)

Client createfile (pszname is \ servername \ Mailslot \ mailslotname)

Name the pipeline server createnamedpipe (pszname is \. \ PIPE \ pipename)

Name the pipeline client createfile (pszname is \ servername \ PIPE \ pipename)

Createpipe

Socket socket, accept or acceptex

Console createconsolescreenbuffer or getstdhandle

These functions create an I/O Kernel Object and obtain the handle value of the object. Then, we can call the functions related to the specific device, and input the device kernel object handle to communicate with the device.

Like other kernel objects, you can call closehandle to disable the I/O kernel objects created by createfile.

Bool closehandle (handle hobject );

However, if the device is a socket, you must call closesocket.

Int Closesocket (socket S ); If there is a device handle, you can use getfiletype to investigate the specific device type. DWORD getfiletype (handle hdevice );
Return Value:
Value Description
File_type_unknown Unknown type
File_type_disk Disk Files
File_type_char Character file, usually a parallel port device or console device
Fiel_type_pipe The specified file is named pipe or anonymous pipe.

 

Related Article

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.