共享内存执行得这么快?

时间:2019-03-17 19:53:06

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

我要在内核中做什么:

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 and Write。只写执行我已经尝试了多次,并且它总是这样做+我试图添加一个sleep(1);在我的用户模式下(认为它执行得非常快)。

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

1 个答案:

答案 0 :(得分:0)

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

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

简单检查,现在可以正常使用:D