|
| БЕСПЛАТНАЯ ежедневная online лотерея! Выигрывай каждый день БЕСПЛАТНО! |
|
|
Reading from and Writing to a File
Every open file has a file pointer that specifies the next byte to be read or the location to receive the next byte written. When a file is opened for the first time, Windows places the file pointer at the beginning of the file. As each byte is read or written, Windows advances the file pointer. An application can also move the file pointer by using the SetFilePointer function.
An application reads from and writes to a file by using the ReadFile and WriteFile functions. These functions require a handle of a file to be opened for reading and writing, respectively. ReadFile and WriteFile read and write a specified number of bytes at the location indicated by the file pointer. The data is read and written exactly as given; the functions do not format the data. When the file pointer reaches the end of a file and the application attempts to read from the file, no error occurs, but no bytes are read. Therefore, reading zero bytes without an error means the program has reached the end of the file. Writing zero bytes does nothing.
An application can truncate or extend a file by using the SetEndOfFile function. This function sets the end of file to the current position of the file pointer. When an application writes to a file, Windows usually collects the data being written in an internal buffer and writes the data to the disk on a regular basis. An application can force the operating system to write the contents of the buffer to the disk by using the FlushFileBuffers function. Alternatively, an application can specify that write operations are to bypass the internal buffer and write directly to the disk by setting a flag when the file is created or opened by using the CreateFile function.
If there is data in the internal buffer when the file is closed, Windows does not automatically write the contents of the buffer to the disk before closing the file. If the application does not force the operating system to write the buffer to disk before closing the file, the caching algorithm determines when the buffer is written. Applications must not read from nor write to the input buffer that a read operation is using until the read operation completes. A premature access to the input buffer may lead to corruption of the data read into that buffer.
Locking and Unlocking Files
Although Windows allows more than one application to open a file and write to it, applications must not write over each other's work. An application can prevent this problem by temporarily locking a region in a file. The LockFile and LockFileEx functions lock a specified range of bytes in a file. The range may extend beyond the current end of the file. Locking part of a file prevents all other processes from reading or writing anywhere in the specified area. Attempts to read from or write to a region locked by another process always fail.
The LockFileEx function allows an application to specify either a shared lock or an exclusive lock. An exclusive lock denies all other processes both read and write access to the specified region of the file. A shared lock denies all processes write access to the specified region of the file, including the process that first locks the region. This can be used to create a read-only region in a file. The application unlocks the region by using the UnlockFile or UnlockFileEx function. An application should unlock all locked areas before closing a file.
Asynchronous Input and Output
Asynchronous input and output (asynchronous I/O) allows some I/O functions to return immediately, even though an I/O request is still pending. Asynchronous I/O enables an application to continue with other processing and wait for the I/O to be completed at a later time. Asynchronous I/O is also called overlapped I/O. The ReadFile and WriteFile functions enable an application to specify an OVERLAPPED structure that indicates where to position the file pointer before the read or write operation. The handle of the file being read from or written to must have been opened with the FILE_FLAG_OVERLAPPED flag. You can also create an event and put the handle in the OVERLAPPED structure; the wait functions can then be used to wait for the I/O operation to complete by waiting on the event handle.
An application can also wait on the file handle to synchronize the completion of an I/O operation, but doing so requires extreme caution. Each time an I/O operation is started, the operating system sets the file handle to the nonsignaled state. Each time an I/O operation is completed, the operating system sets the file handle to the signaled state. Therefore, if an application starts two I/O operations and waits on the file handle, there is no way to determine which operation is finished when the handle is set to the signaled state. If an application must perform multiple asynchronous I/O operations on a single file, it should wait on the event handle in the OVERLAPPED structure for each I/O operation, rather than on the file handle.
To cancel all pending asynchronous I/O operations, use the CancelIO function. This function only cancels operations issued by the calling thread for the specified file handle. The ReadFileEx and WriteFileEx functions enable an application to specify a routine to execute (see FileIOCompletionRoutine) when the asynchronous I/O request is completed. For more information, see Synchronization and Overlapped Input and Output.
I/O Completion Ports
I/O completion ports are used with asynchronous I/O. The CreateIoCompletionPort function associates an I/O completion port with one or more file handles. When an asynchronous I/O operation started on a file handle associated with an I/O completion port is completed, an I/O completion packet is queued to the port. This can be used to combine the synchronization point for multiple file handles into a single object. A thread uses the GetQueuedCompletionStatus function to wait for an I/O completion packet to be queued to the I/O completion port, rather than waiting directly for the asynchronous I/O to complete. Threads that block their execution on an I/O completion port are released in last-in-first-out (LIFO) order. This means that when an I/O completion packet is queued to the I/O completion port, the system releases the last thread to block its execution on the port.
The most important property of an I/O completion port is the concurrency value. The concurrency value of an I/O completion port is specified when the I/O completion port is created. This value limits the number of runnable threads associated with the I/O completion port. When the total number of runnable threads associated with the I/O completion port reaches the concurrency value, the system blocks the execution of the threads until the number of runnable threads associated with the I/O completion port drops below the concurrency value. The most efficient scenario occurs when there are I/O completion packets waiting in the queue, but no waits can be satisfied because the port has reached its concurrency limit. In this case, when a running thread calls GetQueuedCompletionStatus, it will immediately pick up the queued I/O completion packet. No context switches will occur, because the running thread is continually picking up I/O completion packets and the other threads are unable to run.
The best value to pick for the concurrency value is the number of CPUs on the machine. If your transaction required a lengthy computation, a larger concurrency value will allow more threads to run. Each transaction will take longer to complete, but more transactions will be processed at the same time. It is easy to experiment with the concurrency value to achieve the best effect for your application. The PostQueuedCompletionStatus function allows an application to queue its own special-purpose I/O completion packets to the I/O completion port without starting an asynchronous I/O operation. This is useful for notifying worker threads of external events.
The I/O completion port is freed when there are no more references to it. The port handle and every file handle associated with the I/O completion port reference the I/O completion port. All the handles must be closed to free the I/O completion port. To close the port handle, call the CloseHandle function.
| Пригласи друзей и счет твоего мобильника всегда будет положительным! |
| Пригласи друзей и счет твоего мобильника всегда будет положительным! |
Чтение от и Сочинения до Файла
Каждый открытый файл имеет файловый указатель, который определяет следующий байт, который нужно быть прочитан или позиция, чтобы получать письменный следующий байт. Когда файл открывается впервые, Окно устанавливает файловый указатель в начале файла. Так как каждый байт прочитан или написанное, Окно предоставляет файловый указатель. Приложение может также переместить файловый указатель используя функцию SetFilePointer.
Приложение читается из и пишется в файл используя ReadFile и функции WriteFile. Эти функции требуют ручку файла, который нужно открываться для чтения и сочинения, соответственно. ReadFile И WriteFile прочитанное и писать определенное количество байтов в позиции указанной файловым указателем. Данные прочитаны и записанное точно как дано; функции не форматируют данные. Когда файловый указатель достигает конца файла и прикладные попытки, чтобы читаться из файла, никакая ошибка не происходит, но никакие байты не прочитаны. Следовательно, нулевые байты чтения без ошибки означает что программа достигла конца файла. Нулевые байты Записи делает ничто.
Приложение может исключить или расширять файл используя функцию SetEndOfFile. Эта функция устанавливает конец файла в текущую позицию файлового указателя. Когда приложение записывается в файл, Windows обычно собирает данные, записыванные во внутреннем буфере и записывает данные на диск на регулярной основе. Приложение может заставить операционную систему, чтобы записывать содержание буфера на диск используя функцию FlushFileBuffers. Кроме того, приложение может определить, который записывает операции должно обходить внутренний буфер и записываться непосредственно на диск устанавливая флаг когда файл создан или открыт использованием функции CreateFile.
Если есть данные во внутреннем буфере когда файл закрыт, Windows автоматически не записывает содержание буфера на диск перед закрытием файла. Если приложение не заставляет операционную систему, чтобы записывать буфер на диск перед закрытием файла, кеширующий алгоритм определяется когда буфер записан. Приложения по-видимому не прочитаются из ни записываются в входной буфер, что операция чтения использует пока операция чтения не завершится. Преждевременный доступ к входному буферу может провести к коррупции данных прочитанных в этот буфер.
Блокировка Файлов и Разблокировки
Хотя Windows позволяет одно приложение, чтобы открывать файл и записываться этому, приложениям не должно записываться над каждой работой другого. Приложение может предохранить эту проблему временно запирая область в файле. LockFile И блокировка функций LockFileEx определенный дипазон байтов в файле. Дипазон может расшириться за текущим концом файла. Блокировка части файла мешает другие процессы чтобы читаться или запись везде в определенной области. Попытки, чтобы читаться из или записываться в область запертую другим процессом всегда терпеть неудачу.
Функция LockFileEx позволяет приложение, чтобы определять или коллективная блокировка или исключительная блокировка. Исключительная блокировка запрещит все другие процессы как прочитанные так и доступ записи к определенной области файла. Коллективная блокировка запрещится, все процессы записывают доступ к определенной области файла, включая процесс, что блокировка первого область. Это может быть использовано, чтобы создавать только для чтения область в файле. Приложение деблокирует область используя UnlockFile или функция UnlockFileEx. Приложение должно деблокировать все закрытые области перед закрытием файла.
Асинхронный Ввод и Выход
Асинхронный ввод и выход (асинхронный В/В) позволяет функции В/В, чтобы возвращать немедленно, даже если запрос В/В все еще рассматривается. Асинхронный В/В позволяет приложение, чтобы продолжать другое обрабатывать и ждать В/В, чтобы быть завершенн впоследствии. Асинхронный В/В также назван перекрывшее В/В. ReadFile И функции WriteFile допустимый приложение, чтобы определять ПЕРЕКРЫТУЮ структуру, которая указывается где, чтобы позиционировать файловый указатель прежде, чем чтение или запишет операцию. Ручка файла, прочитать из или записанного, чтобы по-видимому открыт флагом FILE_FLAG_OVERLAPPED. Вы можете также создать событие и помещать ручку в ПЕРЕКРЫТУЮ структуру; функции ожидания могут затем использован, чтобы ждать операцию В/В, чтобы завершаться ожидаясь в ручке события.
Приложение может также ожидаться в файловой ручке, чтобы синхронизировать завершение операции В/В, но делая так требует крайнее предупреждение. Всякий раз, когда операция В/В запущена, операционная система устанавливает файловую ручку в nonsignaled состояние. Всякий раз, когда операция В/В завершена, операционная система устанавливает файловую ручку в сигнальное состояние. Следовательно, если приложение начинает две операции В/В и ожидания в файловой ручке, нет пути определять, что какая операция завершена когда ручка установлена в сигнальное состояние. Если приложение должно выполнить многочисленные асинхронные операции В/В в единственном файле, оно должно ожидаться в ручке события в ПЕРЕКРЫТОЙ структуре для каждой операции В/В, а не в файловой ручке.
Для того, чтобы отменять все незаконченные асинхронные операции В/В, используйте функцию CancelIO. Эта функция только отменяет операции выпущенные вызовом резьбы для определенной файловой ручки. ReadFileEx И функции WriteFileEx допустимый приложение, чтобы определять программу, чтобы выполнять (смотри FileIOCompletionRoutine) когда асинхронный запрос В/В завершен. Более подробно, смотри Синхронизацию и Перекрывшую Ввод и Выход.
ПОРТЫ Завершения В/В
ПОРТЫ завершения В/В использованы асинхронным В/В. Функция CreateIoCompletionPort соединяет порт завершения В/В с одной или более файловыми ручками. Когда асинхронная операция В/В запущенная в файловую ручку связанную портом завершения В/В завершена, пакет завершения В/В поставлен в очередь в порт. Это может быть использовано, чтобы объединять точку синхронизации для многочисленных файловых ручек на единственный объект. Резьба использует функцию GetQueuedCompletionStatus, чтобы ждать пакет завершения В/В, который нужно ставиться в очередь в порт завершения В/В, а не ожидаясь непосредственно для асинхронного В/В, чтобы завершаться. Резьба, что блокировать их выполнение в порту завершения В/В выпущены в последний раз-в-сначала- порядке (LIFO). Это означает, что когда пакет завершения В/В поставлен в очередь в порт завершения В/В, система выпускает последнюю резьбу, чтобы блокировать свое выполнение в порту.
Наиболее важное свойство порта завершения В/В является величиной параллелизма. Величина параллелизма порта завершения В/В определена когда порт завершения В/В создан. Эта величина ограничивает количество работоспособной резьбы связанной портом завершения В/В. Когда общее число работоспособной резьбы связанной портом завершения В/В достигает величины параллелизма, система блокирует выполнение резьбы до количества работоспособной резьбы связанной каплями порта завершения В/В ниже величины параллелизма. Наиболее эффективный сценарий происходит когда есть пакеты завершения В/В, ожидавшиеся в очереди, но никакие ожидания не могут быть удовлетворены поскольку порт достиг своего предела параллелизма. В этом случае, когда прогон резьбы называет GetQueuedCompletionStatus, он немедленно приобретет поставленный в очередь пакет завершения В/В. Никакие контекстные ключи не произойдет, поскольку выполняющаяся резьба непрерывно приобретает пакеты завершения В/В и другая резьба - не в состоянии работать.
Оптимальное значение, чтобы выбираться для величины параллелизма - число CPUs в машине. Если ваша сделка требовала бы длительное вычисление, большая величина параллелизма позволит резьбы, чтобы работать. Каждая сделка возьмет более длинный, чтобы завершать, но больше сделок обработается в то же самое время. Это легко должно экспериментировать с величиной параллелизма, чтобы достигать наилучшего эффекта для вашего приложения. Функция PostQueuedCompletionStatus позволяет приложение, чтобы ставить в очередь свои собственные специализированные пакеты завершения В/В в порт завершения В/В не запуская асинхронную операцию В/В. Это полезное для уведомления резьбы рабочего о внешних событиях.
Порт завершения В/В освобожден когда нет больше ссылок на это. Ручка порта и каждой файловой ручки связывались ссылкой порта завершения В/В порт завершения В/В. Все ручки должны быть близкими к свободному порт завершения В/В. Для того, чтобы закрывать ручку порта, назовите функцию CloseHandle.
|
|
|
|
| |