Mailslot, c++, win API
08.12.2021, 14:48. Показов 2215. Ответов 0
Добрый день! Пытаюсь выполнить задачу, поставленную в университете. Необходимо выполнить умножение двух матриц, используя межпроцессорное взаимодействие.
Есть три программы (процесса):
1) Клиент, который запрашивает всю информацию у пользователя и отдает тому или иному процессу (то есть он использует только комнату WrateFile)
2) Сервер, который получает что-то от пользователя (то есть использует только команду ReadFile)
3) Сервер, который получает что-то от пользователя (то есть использует только команду ReadFile)
Все работает хорошо, но в момент, когда Процесс пытается отдать информацию клиенту, то все ломается с ошибкой 5.
Может, кто-то сталкивался?
Код Клиента:
| C++ | 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
| #include "iostream"
#include "math.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
void main() {
setlocale(LC_ALL, "Russian");
int row1 = 0, col1 = 0, row2 = 0, col2 = 0;
HANDLE Mailslot1;
HANDLE Mailslot2;
DWORD BytesWritten;
DWORD NumberOfBytesRead;
int buffer;
if ((Mailslot1 = CreateFile(TEXT("\\\\.\\mailslot\\first_mailslot"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
printf("Ошибка открытия слота %d\n", GetLastError());
getchar();
return;
}
if ((Mailslot2 = CreateFile(TEXT("\\\\.\\mailslot\\second_mailslot"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
printf("Ошибка открытия слота %d\n", GetLastError());
getchar();
return;
}
int k = 0;
while (k == 0){
printf("\nВведите количество строк первой матрицы:");
cin >> row1;
printf("\nВведите количество столбцов первой матрицы:");
cin >> col1;
cout << endl << endl;
printf("\nВведите количество строк второй матрицы:");
cin >> row2;
printf("\nВведите количество столбцов второй матрицы:");
cin >> col2;
if (row1 != col2){
k = 0;
printf("\nВведите корректное значение!");
}else{
k = 1;
}
}
//отправляем данные о размерах матрицы первому процессу
if (WriteFile(Mailslot1, (LPVOID)&row1, sizeof(row1), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot1, (LPVOID)&col1, sizeof(col1), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot1, (LPVOID)&row2, sizeof(row2), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot1, (LPVOID)&col2, sizeof(col2), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
//отправляем данные о размерах матрицы второму процессу
if (WriteFile(Mailslot2, (LPVOID)&row1, sizeof(row1), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot2, (LPVOID)&col1, sizeof(col1), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot2, (LPVOID)&row2, sizeof(row2), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (WriteFile(Mailslot2, (LPVOID)&col2, sizeof(col2), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
//создаем матрицы
int** matrix_1 = new int* [row1];
for (size_t i = 0; i < row1; ++i){
matrix_1[i] = new int[col1];
}
int** matrix_2 = new int* [row2];
for (size_t i = 0; i < row2; ++i){
matrix_2[i] = new int[col2];
}
cout << endl << endl;
cout << "Заполните элементы первой матрицы:\n";
for (size_t i = 0; i < row1; ++i) {
cout << "Введите " << col1 << " элементов(-a) " << i + 1 << " строки " << endl;
for (size_t j = 0; j < col1; ++j) {
cin >> matrix_1[i][j];
//первому процессу
if (WriteFile(Mailslot1, (LPVOID)&matrix_1[i][j], sizeof(matrix_1[i][j]), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
//второму процессу
if (WriteFile(Mailslot2, (LPVOID)&matrix_1[i][j], sizeof(matrix_1[i][j]), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
}
}
cout << endl << endl;
cout << "Заполните элементы второй матрицы:\n";
for (size_t i = 0; i < row2; ++i) {
cout << "Введите " << col2 << " элементов(-a) " << i + 1 << " строки " << endl;
for (size_t j = 0; j < col2; ++j) {
cin >> matrix_2[i][j];
//первому процессу
if (WriteFile(Mailslot1, (LPVOID)&matrix_2[i][j], sizeof(matrix_2[i][j]), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
//второму процессу
if (WriteFile(Mailslot2, (LPVOID)&matrix_2[i][j], sizeof(matrix_2[i][j]), &BytesWritten, NULL) == 0) {
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
}
}
int** matrix_3 = new int* [row1];
for (size_t i = 0; i < row1; ++i) {
matrix_3[i] = new int[col2];
}
for (int i = 0; i < row1; ++i){
for (int j = 0; j < col2; ++j) {
matrix_3[i][j] = 0;
if (((j + 1) + ((i + 1) * col2)) % 2 != 0) {//если элемент не четный
//то его считает первый процесс
if (WriteFile(Mailslot1, (LPVOID)&i, sizeof(i), &BytesWritten, NULL) == 0) {//передаем i
printf("Ошибка отправки %d\n", GetLastError());//передаем И
getchar();
return;
}
if (WriteFile(Mailslot1, (LPVOID)&j, sizeof(j), &BytesWritten, NULL) == 0) {//передаем j
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0) {//Получаем ячейку
matrix_3[i][j] = buffer;
}
}
else {
if (WriteFile(Mailslot2, (LPVOID)&i, sizeof(i), &BytesWritten, NULL) == 0) {//передаем i
printf("Ошибка отправки %d\n", GetLastError());//передаем И
getchar();
return;
}
if (WriteFile(Mailslot2, (LPVOID)&j, sizeof(j), &BytesWritten, NULL) == 0) {//передаем j
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
if (ReadFile(Mailslot2, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0) {//Получаем ячейку
matrix_3[i][j] = buffer;
}
}
}
}
for (int i = 0; i < row1; ++i){
for (int j = 0; j < col2; ++j){
cout << matrix_3[i][j]<<" ";
}
cout << "\n";
}
CloseHandle(Mailslot1);
CloseHandle(Mailslot2);
} |
|
Код Сервера (у обоих серверов код один по сути, меняется только имя почтового ящика)
| C++ | 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
| #include "iostream"
#include "math.h"
#include <windows.h>
#include <omp.h>
using namespace std;
void main() {
setlocale(LC_ALL, "Russian");
HANDLE Mailslot1;
int buffer;
unsigned int row1 = 0, col1 = 0, row2 = 0, col2 = 0;
DWORD NumberOfBytesRead;
DWORD BytesWritten;
if ((Mailslot1 = CreateMailslot(TEXT("\\\\.\\mailslot\\first_mailslot"), 0, MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE) {
printf("Ошибка при создании %d\n", GetLastError());
getchar();
return;
}
//получаем размер матриц
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0){
row1 = buffer;
}
cout << "row1: " << row1 << endl;
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0){
col1 = buffer;
}
cout << "col1: " << col1 << endl;
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0){
row2 = buffer;
}
cout << "row2: " << row2 << endl;
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0){
col2 = buffer;
}
cout << "col2: " << col2 << endl;
//создаем матрицы
int** matrix_1 = new int* [row1];
for (size_t i = 0; i < row1; ++i) {
matrix_1[i] = new int[col1];
}
cout << "Матрица 1 - построена" << endl;
int** matrix_2 = new int* [row2];
for (size_t i = 0; i < row2; ++i) {
matrix_2[i] = new int[col2];
}
cout << "Матрица 2 - построена" << endl;
//заполняем матрицы
for (size_t i = 0; i < row1; ++i) {
for (size_t j = 0; j < col1; ++j) {
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0) {
matrix_1[i][j] = buffer;
}
}
}
cout << "Матрица 1 - получена" << endl;
for (size_t i = 0; i < row2; ++i) {
for (size_t j = 0; j < col2; ++j) {
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0)
{
matrix_2[i][j] = buffer;
}
}
}
cout << "Матрица 2 - получена" << endl;
int count = 0;
if (row1 * col2 % 2 != 0) {
count = (row1 * col2) + 1 / 2;
}
else {
count = (row1 * col2) / 2;
}
cout << "count = " << count << endl;
int i = 0, j = 0, c = 1;
while (c != count){
cout << "c = " << c << endl;
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0) {
i = buffer;
}
cout << "i: " << i;
if (ReadFile(Mailslot1, (LPVOID)&buffer, sizeof(buffer), &NumberOfBytesRead, NULL) != 0) {
j = buffer;
}
cout << "j: " << j;
int m3 = 0;
for (int k = 0; k < col1; k++) {
m3 += matrix_1[i][k] * matrix_2[k][j];
}
if (WriteFile(Mailslot1, (LPVOID)&m3, sizeof(m3), &BytesWritten, NULL) == 0) {//передаем m3
printf("Ошибка отправки %d\n", GetLastError());
getchar();
return;
}
cout << m3;
c++;
}
CloseHandle(Mailslot1);
} |
|
0
|