1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* Windows 内核下StartIo例程试验 3环代码
编译方法参见makefile. TAB = 8
*/
#include <stdio.h>
#include <windows.h>
#pragma comment( linker, "/Entry:Jmain" )
#pragma comment( linker, "/subsystem:console" )
#define DEVICE_NAME "\\\\.\\SysLinkCustomStartIo"
//===========================================================================
//线程过程, 向设备发送15次, 写入请求
//===========================================================================
DWORD __stdcall ThreadProc( PVOID pContext ) {
UCHAR ucBuf[10];
ULONG i, j;
DWORD dwByteWrite;
BOOL bRet;
OVERLAPPED StOverlapped[15] = {0};
HANDLE hEvent[15] = {0};
__try {
for( i = 0; i < sizeof( StOverlapped ) / sizeof( StOverlapped[0] ); i++ ) {
StOverlapped[i].hEvent = CreateEvent( NULL, FALSE, TRUE, NULL );
if ( !StOverlapped[i].hEvent ) {
printf( "创建同步事件失败!\n" );
return -1;
}
}
for( i = 0; i < sizeof( StOverlapped ) / sizeof( StOverlapped[0] ); i++ ) {
hEvent[i] = StOverlapped[i].hEvent;
}
Sleep( 1000 );
for( i = 0; i < sizeof( StOverlapped ) / sizeof( StOverlapped[0] ); i++ ) {
RtlFillMemory( ucBuf, sizeof( ucBuf ), i + 'a' );
bRet = WriteFile( *( PHANDLE )pContext, ucBuf, sizeof( ucBuf ),
&dwByteWrite, &StOverlapped[i] );
if ( !bRet && GetLastError() != ERROR_IO_PENDING ) {
printf( "写入设备失败!\n" );
return -1;
} else {
for( j = 0; j < sizeof( ucBuf ); j++ ) {
printf( "%c\t", ucBuf[j] );
}
printf( "\n" );
}
}
//这个复杂度加了一些中间有可能还会有取消IRP的出现
//CancelIo(*(PHANDLE)pContext);
WaitForMultipleObjects( sizeof( StOverlapped ) / sizeof( StOverlapped[0] ),
&hEvent[0], TRUE, INFINITE );
printf( "设备处理完毕!\n" );
} __finally {
for( i = 0; i < sizeof( hEvent ) / sizeof( hEvent[0] ); i++ ) {
if ( StOverlapped[i].hEvent ) {
CloseHandle( StOverlapped[i].hEvent );
}
}
}
return 0;
}
//===========================================================================
int Jmain( ) {
HANDLE hDevice = NULL;
HANDLE hThead[2] = {0};
DWORD dwTemp;
do {
hDevice = CreateFile( DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
if ( hDevice == INVALID_HANDLE_VALUE ) {
printf( "打开设备失败\n" );
break;
}
hThead[0] = CreateThread( NULL, 0, &ThreadProc, &hDevice, 0, &dwTemp );
if ( !hThead[0] ) {
printf( "创建线程1失败!\n" );
break;
} else {
printf( "创建线程1成功, 线程1已经开始运行!\n" );
}
hThead[1] = CreateThread( NULL, 0, &ThreadProc, &hDevice, 0, &dwTemp );
if ( !hThead[1] ) {
printf( "创建线程2失败!\n" );
break;
} else {
printf( "创建线程2成功, 线程2已经开始运行!\n" );
}
printf( "主线程开始等待两个线程返回!\n" );
WaitForMultipleObjects( 2, hThead, TRUE, INFINITE );
printf( "两个线程都已经返回!\n" );
} while ( FALSE );
//---------------------------------------------------------------------------
if ( hDevice ) {
CloseHandle( hDevice );
}
if ( hThead[0] ) {
CloseHandle( hThead[0] );
}
if ( hThead[1] ) {
CloseHandle( hThead[1] );
}
system( "pause" );
return 0;
}
|