内核模式下的互斥体

在用户模式下使用的互斥对象貌似也是经过内核的一层包装....

用户模式下的互斥对象



	VOID MyThread1(PVOID pContext){
		KdPrint(("Enter MyThread1"));
		KMUTEX *pkMutex=(KMUTEX*)pContext;

		KdPrint(("MyThread1 KeWaitForSingleObject..."));
		KeWaitForSingleObject(pkMutex,Executive,KernelMode,FALSE,0);
		KdPrint(("MyThread1 KeWaitForSingleObject...ok"));

		KeStallExecutionProcessor(50);//强迫停止50ms,模拟耗时执行一段代码
		KeReleaseMutex(pkMutex,FALSE);
		KdPrint(("Leave MyThread1"));
		PsTerminateSystemThread(STATUS_SUCCESS);
	}

	VOID MyThread2(PVOID pContext){
		KdPrint(("Enter MyThread2"));
		KMUTEX *pkMutex=(KMUTEX*)pContext;

		KdPrint(("MyThread2 KeWaitForSingleObject..."));
		KeWaitForSingleObject(pkMutex,Executive,KernelMode,FALSE,0);
		KdPrint(("MyThread2 KeWaitForSingleObject...ok"));

		KeStallExecutionProcessor(50);//强迫停止50ms,模拟耗时执行一段代码
		KeReleaseMutex(pkMutex,FALSE);
		KdPrint(("Leave MyThread2"));
		PsTerminateSystemThread(STATUS_SUCCESS);
	}
	KdPrint(("Testing........hahaha..... MemoryCat.com"));
	HANDLE hThread1,hThread2;
	KMUTEX kMutex;
	KeInitializeMutex(&kMutex,0);

	PsCreateSystemThread(&hThread1,NULL,NULL,NtCurrentProcess(),NULL,MyThread1,&kMutex);
	PsCreateSystemThread(&hThread2,NULL,NULL,NtCurrentProcess(),NULL,MyThread2,&kMutex);

	VOID *pArray[2];
	ObReferenceObjectByHandle(hThread1,0,NULL,KernelMode,&pArray[0],NULL);
	ObReferenceObjectByHandle(hThread2,0,NULL,KernelMode,&pArray[1],NULL);

	KeWaitForMultipleObjects(2,pArray,WaitAll,Executive,KernelMode,FALSE,NULL,NULL);
	ObDereferenceObject(pArray[0]);
	ObDereferenceObject(pArray[1]);

	KdPrint(("KeWaitForMultipleObjects ,,,end...."));

评论
文章信息
作者: admin
目录: driver
创建: 2014-03-23
更新: 2014-03-23
如果文章对你有帮助,请博主喝杯咖啡:-)