【问题标题】:Shared memory is executing so fast?共享内存执行得这么快?
【发布时间】:2019-08-08 05:23:33
【问题描述】:

我想在内核中做什么:

while (TRUE)
{
    //DbgPrintEx(0, 0, "First loop is running \n");
    ReadSharedMemory();


        if (!(PCHAR)SharedSection == NULL && strcmp((PCHAR)SharedSection, "Read") == 0)
        {
            DbgPrintEx(0, 0, "Read looping \n");

            RtlZeroMemory(SharedSection, sizeof(SharedSection));
            break;
        }
        else if (!(PCHAR)SharedSection == NULL && strcmp((PCHAR)SharedSection, "Write") == 0)
        {
            DbgPrintEx(0, 0, "Write looping \n");
            RtlZeroMemory(SharedSection, sizeof(SharedSection));
            break;
        }
    LARGE_INTEGER Timeout;
    Timeout.QuadPart = RELATIVE(SECONDS(1));
    KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
}

在用户模式下:

    auto pBufW = (char*)MapViewOfFile(hMapFileW, FILE_MAP_WRITE, 0, 0, 4096);


RtlCopyMemory(pBufW, "Read", 4);

printf("message has been sent to kernel! \n");


UnmapViewOfFile(pBufW);

Sleep(10);

auto pBfW = (char*)MapViewOfFile(hMapFileW, FILE_MAP_WRITE, 0, 0, 4096);


RtlCopyMemory(pBfW, "Write", 5);

printf("message has been sent to kernel! \n");


UnmapViewOfFile(pBfW);

当我调用 Read 和 Write 时,我无法弄清楚为什么。只有写执行我已经尝试过多次并且它总是这样做+我试图添加一个睡眠(1);在我的用户模式下(认为它执行得非常快)。

基本上我只需要它们像 Read 应该先执行然后 Write 一样正常执行。

【问题讨论】:

  • 如果你的意思是if (SharedSection != nullptr),那么请这样写...
  • 另外,strcmp 需要一个 NUL-terminated 字符串,而您没有提供...
  • @Ben Voigt RtlCopyMemory(pBfW, "Write", 5); 我是,看到“写”是 6 个字符,包括空终止符我做 5?你能解释一下如果我用错误的方式做那我该怎么做吗?
  • 你用MapViewOfFile做什么?这有什么意义?
  • @RbMm 用于向我的驱动程序发送字符串以与之通信?

标签: c++ c windows kernel shared-memory


【解决方案1】:

有人帮我解决了,这就是我所做的

  while ( memcmp( pBufW, "Read", 4) == 0 )
    {
           Sleep(1);
    }
UnmapViewOfFile(pBufW);

简单检查,现在可以正常工作了:D

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 2015-05-02
    • 2011-06-15
    • 2012-09-21
    • 2014-10-08
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多