博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows核心编程:第10章 同步设备IO与异步设备IO
阅读量:5024 次
发布时间:2019-06-12

本文共 4138 字,大约阅读时间需要 13 分钟。

Github

//第10章 同步设备IO与异步设备IO.cpp: 定义应用程序的入口点。//#include "stdafx.h"#include "第10章 同步设备IO与异步设备IO.h"//可提醒IO回调VOID WINAPI funComplete(    _In_    DWORD dwErrorCode,    _In_    DWORD dwNumberOfBytesTransfered,    _Inout_ LPOVERLAPPED lpOverlapped){}//IO完成端口工作线程DWORD WINAPI workthread(LPVOID lpThreadParameter){    DWORD NumberOfBytesTransferred;    DWORD CompletionKey;    OVERLAPPED* pOverlapped;    BOOL bres = GetQueuedCompletionStatus(lpThreadParameter, &NumberOfBytesTransferred, &CompletionKey, &pOverlapped, INFINITE);    //GetQueuedCompletionStatusEx可获取多个IO请求结果    DWORD dres = GetLastError();    return 0;}int APIENTRY wWinMain(_In_ HINSTANCE hInstance,                     _In_opt_ HINSTANCE hPrevInstance,                     _In_ LPWSTR    lpCmdLine,                     _In_ int       nCmdShow){    //打开(创建)文件    HANDLE hFile = CreateFile(TEXT("第10章 同步设备IO与异步设备IO.cpp"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);    if (hFile == INVALID_HANDLE_VALUE)    {        MessageBox(nullptr, TEXT("打开文件失败!"), TEXT("error"), MB_OK);        return 0;    }    //文件类型    DWORD dres = GetFileType(hFile);    //dres == FILE_TYPE_DISK    //文件大小    LARGE_INTEGER li;    BOOL bres = GetFileSizeEx(hFile, &li);//逻辑大小    li.LowPart = GetCompressedFileSize(TEXT("第10章 同步设备IO与异步设备IO.cpp"), (DWORD*)&li.HighPart);//物理大小    //文件指针    li.QuadPart = 0;    bres = SetFilePointerEx(hFile, li, nullptr, FILE_END);    bres = SetFilePointerEx(hFile, li, &li, FILE_CURRENT);    bres = SetEndOfFile(hFile);    li.QuadPart = 0;    bres = SetFilePointerEx(hFile, li, nullptr, FILE_BEGIN);    //同步IO    char* buf = new char[100];    bres = ReadFile(hFile, buf, 100, &dres, nullptr);    delete []buf;    buf = nullptr;    //刷新缓冲区    bres = FlushFileBuffers(hFile);    //CancelSynchronousIo    //取消线程未完成的同步IO请求    //关闭文件    CloseHandle(hFile);    hFile = nullptr;    //异步IO    HANDLE hFile2 = CreateFile(TEXT("第10章 同步设备IO与异步设备IO.cpp"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);    OVERLAPPED ol;    ol.Offset = 1024;    ol.OffsetHigh = 0;    ol.hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);    char* buf2 = new char[1024];    bres = ReadFile(hFile2, buf2, 1024, nullptr, &ol);//异步方式总是返回FALSE    dres = GetLastError();    switch (dres)    {    case ERROR_IO_PENDING:        //IO请求添加成功        //WaitForSingleObject(hFile2, INFINITE);        WaitForSingleObject(ol.hEvent, INFINITE);        break;    case ERROR_INVALID_USER_BUFFER:    case ERROR_NOT_ENOUGH_MEMORY:        break;    case ERROR_NOT_ENOUGH_QUOTA:        break;    default:        break;    }    //取消队列中的IO请求    //bres = CancelIo(hFile2);    bres = CancelIoEx(hFile2, &ol);    //bres = CancelIoEx(hFile2, nullptr);    //bres = CloseHandle(hFile2);    if (ol.hEvent != nullptr)    {        CloseHandle(ol.hEvent);        ol.hEvent = nullptr;    }    //可提醒IO    OVERLAPPED ol2;    ol2.Offset = 1024;    ol2.OffsetHigh = 0;    ol2.hEvent = nullptr;    dres = ReadFileEx(hFile2, buf2, 1024, &ol2, funComplete);//回调函数在同一个线程空间    //置为可提醒状态    //APC队列中只要有一个,线程就不会进入睡眠    dres = SleepEx(INFINITE, TRUE);    //WaitForSingleObjectEx    //WaitForMultipleObjectsEx    //SignalObjectAndWait    //GetQueuedCompletionStatusEx    //MsgWaitForMultipleObjectsEx    dres = GetLastError();//WAIT_IO_COMPLETION    //手动添加一项到APC队列    //可以让目标线程结束睡眠    dres = QueueUserAPC((PAPCFUNC)funComplete, GetCurrentThread(), NULL);    //IO完成端口    HANDLE iocp = CreateIoCompletionPort(hFile2, nullptr, 1, 2);    HANDLE hts[2];    for (int i = 0; i < 2; ++i)        hts[i] = CreateThread(nullptr, 0, workthread, iocp, 0, nullptr);    bres = PostQueuedCompletionStatus(iocp, 100, 1, &ol); //模拟发送IO请求完成    bres = ReadFile(hFile2, buf2, 1024, nullptr, &ol2);    dres = GetLastError();    WaitForMultipleObjects(2, hts, TRUE, INFINITE);    for (int i = 0; i < 2; ++i)    {        CloseHandle(hts[i]);        hts[i] = nullptr;    }    CloseHandle(iocp);    iocp = nullptr;    CloseHandle(hFile2);    hFile2 = nullptr;    delete[]buf2;    buf2 = nullptr;    system("pause");    return 0;}

转载于:https://www.cnblogs.com/gongluck/p/9335299.html

你可能感兴趣的文章
Linux记录-salt分析
查看>>
Android Studio默认快捷键
查看>>
发布开源库到JCenter所遇到的一些问题记录
查看>>
第七周作业
查看>>
函数式编程与参数
查看>>
flush caches
查看>>
SSAS使用MDX生成脱机的多维数据集CUB文件
查看>>
ACM_hdu1102最小生成树练习
查看>>
MyBatis源码分析(一)--SqlSessionFactory的生成
查看>>
android中ListView点击和里边按钮或ImageView点击不能同时生效问题解决
查看>>
CTF常用工具之汇总
查看>>
java的面向对象 (2013-09-30-163写的日志迁移
查看>>
HDU 2191 【多重背包】
查看>>
51nod 1433 0和5【数论/九余定理】
查看>>
【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
查看>>
less 分页显示文件内容
查看>>
如何对数据按某列进行分层处理
查看>>
[Qt] this application failed to start because it could not find or load the Qt platform plugin
查看>>
Git Submodule管理项目子模块
查看>>
学会和同事相处的30原则
查看>>