共享内存执行得如此之快? [英] Shared memory execute so fast ?

查看:116
本文介绍了共享内存执行得如此之快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内核模式:



kernel mode :

<pre>VOID DriverLoop() {

	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);
	}
}







用户模式:






user mode :

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);





i无法理解为什么我打电话读和写 。只写写执行我多次尝试过它总是做这个+我试图添加一个睡眠(1);在我的用户模式下(认为它执行得非常快)。



基本上我只需要它们正常执行就像Read应该先执行然后再写。



我尝试了什么:



i试图加入1和10的睡眠秒,它没有工作idk我在这里做错了。



i can't figure it out why when i call Read and Write . only Write execute i have tried it multiple times and its always doing this + i have tried to add a sleep(1); in my user mode (thought it was executing real fast).

basically i just need them to execute normally like Read should be executed first then Write .

What I have tried:

i have tried to add a sleep of 1 and 10 seconds and it didn't work idk what i am doing wrong here.

推荐答案

你做错了希望不同执行环中的两个进程可以协调自己纯粹的运气。



正如我之前给你写的那样,你需要使用信号机制。这可以是一个简单的计数器,进程监视更改,也可以是需要完成工作和完成时发出信号的事件(或两个)。肯定有更多的可能性,但这两种可能性非常简单。
What you are doing wrong is hoping that two processes in different execution rings can coordinate themselves by pure luck.

As I have written to you before, you need to use a signalling mechanism. This can be a simple counter that the processes watch for a change or it could be an event (or two) that is/are signalled when work needs to be done and when it is complete. There are certainly many more possibilities but those are two that are quite simple to implement.


这篇关于共享内存执行得如此之快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆