-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateProcess.cpp
More file actions
218 lines (180 loc) · 6.83 KB
/
Copy pathCreateProcess.cpp
File metadata and controls
218 lines (180 loc) · 6.83 KB
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
208
209
210
211
212
213
214
215
216
217
218
/*
M25-514 Kirgizov Temurmalik Kutbiddin ugli.
DOP. CreateProcess (Relocs Disable) impl.
*/
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <vector>
using namespace std;
const int ARRAY_SIZE = 1000000000;
const int A = 514;
const int B = 8;
const int MAX_PROCESSES = 10;
int calculate_threads_count(int A, int B) {
int X = 2 * A + B;
return X % 5 + 2;
}
int calculate_distribution_type(int A, int B) {
return (2 * A + B) % 4;
}
int main(int argc, char* argv[]) {
const int num_processes = calculate_threads_count(A, B);
const int distribution_type = calculate_distribution_type(A, B);
// Child process workers
if (argc == 4) {
int processID = atoi(argv[1]);
int start = atoi(argv[2]);
int end = atoi(argv[3]);
cout << "Процесс " << processID << ": запущен, PID=" << GetCurrentProcessId() << endl;
cout << "Процесс " << processID << ": адрес main = " << hex << (void*)main << dec << endl;
int chunk = end - start;
int buffer_size = chunk + 2;
unsigned char* local_data = new unsigned char[buffer_size];
srand(2025);
// Skip numbers until our range
for (int i = 0; i < start - 1; i++) {
rand();
}
// Left neighbor
if (processID > 0) {
local_data[0] = rand() & 0xff;
} else {
local_data[0] = 0;
}
// Main data
for (int i = 0; i < chunk; i++) {
local_data[i + 1] = rand() & 0xff;
}
// Right neighbor
if (processID < num_processes - 1) {
local_data[buffer_size - 1] = rand() & 0xff;
} else {
local_data[buffer_size - 1] = 0;
}
// Count local maximums
int local_count = 0;
for (int i = 1; i <= chunk; i++) {
int global_i = start + (i - 1);
if (global_i == 0) {
if (local_data[i] >= local_data[i + 1]) {
local_count++;
}
}
else if (global_i == ARRAY_SIZE - 1) {
if (local_data[i] >= local_data[i - 1]) {
local_count++;
}
}
else {
if (local_data[i] >= local_data[i - 1] &&
local_data[i] >= local_data[i + 1]) {
local_count++;
}
}
}
delete[] local_data;
return local_count;
}
// Parent process
cout << "A = " << A << ", B = " << B << endl;
cout << "Количество процессов = (X % 5) + 2 = (" << (2*A + B) << " % 5) + 2 = "
<< num_processes << endl;
cout << "Тип распределения = (X % 4) = (" << (2*A + B) << " % 4) = "
<< distribution_type << endl;
cout << endl;
cout << "Distribution: " << distribution_type;
switch(distribution_type) {
case 0: cout << " (Block L->R)"; break;
case 1: cout << " (Block R->L)"; break;
case 2: cout << " (Cyclic)"; break;
case 3: cout << " (Reverse Cyclic)"; break;
}
cout << endl;
cout << endl;
cout << "PID родителя: " << GetCurrentProcessId() << endl;
cout << "Адрес main в родителе: " << hex << (void*)main << dec << endl;
cout << endl;
// Путь к программе
char exePath[MAX_PATH];
GetModuleFileNameA(NULL, exePath, MAX_PATH);
// Process info arrays
PROCESS_INFORMATION processInfo[MAX_PROCESSES];
STARTUPINFOA startupInfo[MAX_PROCESSES];
// Block base size
int chunk_size = ARRAY_SIZE / num_processes;
// Storage for process ranges
vector<pair<int, int>> ranges(num_processes);
for (int i = 0; i < num_processes; i++) {
int logical_index; // Which chunk of data this process handles
switch (distribution_type) {
case 0:
logical_index = i;
break;
case 1:
logical_index = num_processes - 1 - i;
break;
case 2:
if (i % 2 == 0) {
logical_index = i / 2;
} else {
logical_index = num_processes - 1 - (i / 2);
}
break;
case 3:
logical_index = (i * 3) % num_processes;
break;
default:
logical_index = i;
}
ranges[i].first = logical_index * chunk_size;
ranges[i].second = (logical_index + 1) * chunk_size;
}
cout << "Создаю " << num_processes << " дочерних процессов" << endl;
cout << endl;
for (int i = 0; i < num_processes; i++) {
int start = ranges[i].first;
int end = ranges[i].second;
char cmdLine[512];
sprintf_s(cmdLine, "\"%s\" %d %d %d", exePath, i, start, end);
ZeroMemory(&startupInfo[i], sizeof(STARTUPINFOA));
startupInfo[i].cb = sizeof(STARTUPINFOA);
ZeroMemory(&processInfo[i], sizeof(PROCESS_INFORMATION));
BOOL success = CreateProcessA(
NULL, cmdLine, NULL, NULL, FALSE, 0,
NULL, NULL, &startupInfo[i], &processInfo[i]
);
if (!success) {
cerr << "ОШИБКА при создании процесса " << i << ": " << GetLastError() << endl;
return 1;
}
cout << "Процесс " << i << " (PID " << processInfo[i].dwProcessId
<< "): диапазон [" << start << ", " << end << ")" << endl;
}
cout << endl;
// Wait for all processes to finish
HANDLE handles[MAX_PROCESSES];
for (int i = 0; i < num_processes; i++) {
handles[i] = processInfo[i].hProcess;
}
WaitForMultipleObjects(num_processes, handles, TRUE, INFINITE);
// Collect results
int total_count = 0;
cout << "Результаты:" << endl;
for (int i = 0; i < num_processes; i++) {
DWORD exitCode;
GetExitCodeProcess(processInfo[i].hProcess, &exitCode);
cout << "Процесс " << i << " (диапазон [" << ranges[i].first << ", "
<< ranges[i].second << ")): " << exitCode << " максимумов" << endl;
total_count += exitCode;
CloseHandle(processInfo[i].hProcess);
CloseHandle(processInfo[i].hThread);
}
cout << endl;
cout << "Количество максимумов: " << total_count << " ===" << endl;
cout << endl;
cout << "Программа скомпилирована с флагами:" << endl;
cout << "-Wl,--image-base=0x400000" << endl;
cout << "-Wl,--disable-dynamicbase" << endl;
return 0;
}