1 |
joko |
1.1 |
/* Betriebssystem & Middleware |
2 |
|
|
* |
3 |
|
|
* Betriebssystemarchitektur SS 2006 |
4 |
|
|
* |
5 |
|
|
* Uebung 4.4 |
6 |
|
|
*/ |
7 |
joko |
1.3 |
|
8 |
joko |
1.17 |
// $Id: bmp_fractal.c,v 1.16 2006/07/02 12:21:01 joko Exp $ |
9 |
joko |
1.3 |
|
10 |
|
|
#include <windows.h> |
11 |
joko |
1.1 |
#include <stdio.h> |
12 |
|
|
#include <errno.h> |
13 |
|
|
|
14 |
|
|
#define XSIZE 500 |
15 |
|
|
#define YSIZE 500 |
16 |
|
|
#include "algorithm.h" |
17 |
|
|
|
18 |
joko |
1.17 |
#define MAX_COMMAND_LINE 65536 |
19 |
|
|
#define MMAP_NAME "bmp_fractal" |
20 |
|
|
|
21 |
joko |
1.11 |
BOOL VERBOSE = FALSE; |
22 |
joko |
1.9 |
|
23 |
joko |
1.3 |
|
24 |
joko |
1.2 |
/* BMP Header */ |
25 |
joko |
1.1 |
unsigned char header[54]={0x42,0x4d, // signature BM |
26 |
|
|
0xe6,0x71,0x0b,0x0, // filesize 750054 |
27 |
|
|
0x0,0x0,0x0,0x0, // reserved |
28 |
|
|
0x36,0x0,0x0,0x0, // image offset 54 |
29 |
|
|
0x28,0x0,0x0,0x0, // size of header follows 40 |
30 |
|
|
0xf4,0x1,0x0,0x0, // with of image 500 |
31 |
|
|
0xf4,0x1,0x0,0x0, // height of image 500 |
32 |
|
|
0x1,0x0, // number of planes 1 |
33 |
|
|
0x18,0x0, // number of pixel 24 |
34 |
|
|
0x0,0x0,0x0,0x0, // compression |
35 |
|
|
0xb0,0x71,0x0b,0x0, // size of image 750000 |
36 |
|
|
0x0,0x0,0x0,0x0, // xres |
37 |
|
|
0x0,0x0,0x0,0x0, // yres |
38 |
|
|
0x0,0x0,0x0,0x0, // number of colortables |
39 |
|
|
0x0,0x0,0x0,0x0 // number of important colors |
40 |
|
|
}; |
41 |
|
|
|
42 |
joko |
1.3 |
|
43 |
|
|
void printErrorAndExit(const char *msg, DWORD err) { |
44 |
|
|
LPSTR lpMsgBuf; |
45 |
|
|
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| |
46 |
|
|
FORMAT_MESSAGE_FROM_SYSTEM | |
47 |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
48 |
|
|
NULL, |
49 |
|
|
err, |
50 |
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
51 |
|
|
(LPTSTR) &lpMsgBuf, |
52 |
|
|
0, |
53 |
|
|
NULL )) |
54 |
|
|
{ |
55 |
joko |
1.15 |
fprintf(stderr, "%s: %s\n", msg, lpMsgBuf); |
56 |
joko |
1.3 |
LocalFree(lpMsgBuf); |
57 |
|
|
} |
58 |
|
|
else |
59 |
|
|
{ |
60 |
joko |
1.15 |
fprintf(stderr, "Error at FormatMesage: %d\n",err=GetLastError()); |
61 |
joko |
1.3 |
} |
62 |
|
|
exit(err); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
void write_blank_file(char *filename) { |
67 |
|
|
|
68 |
|
|
FILE *fd; |
69 |
|
|
int len, i, img_size; |
70 |
|
|
|
71 |
|
|
// open file handle |
72 |
|
|
fd = fopen(filename, "wb+"); |
73 |
|
|
if (NULL == fd) { |
74 |
joko |
1.15 |
perror("Error while opening file for writing"); |
75 |
joko |
1.3 |
exit(1); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
// write bmp header to file |
79 |
|
|
len = fwrite(header, 1, sizeof(header), fd); |
80 |
|
|
|
81 |
|
|
// error checking |
82 |
|
|
if (-1 == len || len != sizeof(header)) { |
83 |
joko |
1.15 |
perror("Error while writing header to file"); |
84 |
joko |
1.3 |
exit(2); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
// write three null-bytes for each pixel to file to create a black picture |
88 |
|
|
img_size = XSIZE * YSIZE; |
89 |
|
|
for (i = 0; i < img_size; i++) { |
90 |
|
|
len = fwrite("\0\0\0", 1, 3, fd); |
91 |
|
|
if (-1 == len || len != 3) { |
92 |
joko |
1.15 |
perror("Error while writing data to file"); |
93 |
joko |
1.3 |
exit(4); |
94 |
joko |
1.1 |
} |
95 |
joko |
1.3 |
} |
96 |
|
|
|
97 |
|
|
// close file handle |
98 |
joko |
1.17 |
if (fclose(fd) != 0) { |
99 |
|
|
perror("Error while closing file handle"); |
100 |
|
|
exit(1); |
101 |
|
|
} |
102 |
joko |
1.3 |
} |
103 |
|
|
|
104 |
|
|
|
105 |
joko |
1.6 |
// arguments for each thread |
106 |
|
|
typedef struct _WorkerArguments { |
107 |
joko |
1.7 |
int start_row; |
108 |
|
|
int number_of_rows; |
109 |
joko |
1.6 |
unsigned char * pBitmap; |
110 |
|
|
} WORKERARGS, *PWORKERARGS; |
111 |
|
|
|
112 |
|
|
// worker thread - main entry function |
113 |
joko |
1.10 |
DWORD WINAPI fractal_create_segment (LPVOID lpParam) { |
114 |
joko |
1.6 |
|
115 |
|
|
// thread stuff |
116 |
|
|
int thread_id; |
117 |
|
|
PWORKERARGS args; |
118 |
joko |
1.7 |
unsigned char *pDataBitmapSegment; |
119 |
joko |
1.6 |
|
120 |
|
|
// fractal calculation |
121 |
|
|
int x, y; |
122 |
|
|
char bgr[3]; |
123 |
|
|
|
124 |
|
|
// get worker arguments |
125 |
|
|
args = (PWORKERARGS)lpParam; |
126 |
joko |
1.7 |
|
127 |
|
|
// calculate pointer to beginning of segment |
128 |
|
|
pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); |
129 |
joko |
1.9 |
|
130 |
|
|
// debugging |
131 |
|
|
if (VERBOSE) { |
132 |
joko |
1.17 |
thread_id = GetCurrentThreadId(); |
133 |
joko |
1.11 |
fprintf(stdout, "----------------------------------------------\n"); |
134 |
|
|
fprintf(stdout, "thread_id: %i\n", thread_id); |
135 |
|
|
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
136 |
|
|
fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows); |
137 |
|
|
fprintf(stdout, "segment_start: %p\n", pDataBitmapSegment); |
138 |
joko |
1.9 |
} |
139 |
joko |
1.6 |
|
140 |
|
|
// calculate fractal |
141 |
|
|
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
142 |
joko |
1.11 |
//fprintf(stdout, "calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
143 |
joko |
1.6 |
for (x = 0; x < XSIZE; x++) { |
144 |
joko |
1.17 |
|
145 |
|
|
// call to function in static linked library |
146 |
joko |
1.6 |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
147 |
|
|
|
148 |
|
|
// transfer color values to current pixel |
149 |
joko |
1.7 |
pDataBitmapSegment[0] = bgr[0]; |
150 |
|
|
pDataBitmapSegment[1] = bgr[1]; |
151 |
|
|
pDataBitmapSegment[2] = bgr[2]; |
152 |
joko |
1.6 |
|
153 |
|
|
// move pointer to next pixel |
154 |
joko |
1.7 |
pDataBitmapSegment += 3; |
155 |
joko |
1.6 |
|
156 |
|
|
} |
157 |
joko |
1.9 |
//no padding required because 1500%4 =0 ??? |
158 |
joko |
1.6 |
} |
159 |
|
|
|
160 |
joko |
1.9 |
if (VERBOSE) |
161 |
joko |
1.11 |
fprintf(stdout, "thread finished: %i\n", thread_id); |
162 |
joko |
1.6 |
return 0; |
163 |
|
|
|
164 |
|
|
} |
165 |
|
|
|
166 |
joko |
1.14 |
BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value) { |
167 |
joko |
1.13 |
int i; |
168 |
|
|
char * opt_current_name; |
169 |
|
|
char * opt_current_value; |
170 |
joko |
1.14 |
|
171 |
joko |
1.13 |
//printf("searching for: '%s'\n", opt_name); |
172 |
|
|
|
173 |
|
|
for (i = 1; i < argc; i++) { |
174 |
|
|
opt_current_name = argv[i]; |
175 |
|
|
if (strcmp(opt_current_name, opt_name) == 0) { |
176 |
|
|
opt_current_value = argv[i+1]; |
177 |
|
|
if (opt_current_value != NULL) { |
178 |
joko |
1.14 |
strcpy(opt_value, opt_current_value); |
179 |
joko |
1.13 |
} |
180 |
|
|
return TRUE; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
return FALSE; |
184 |
|
|
} |
185 |
|
|
|
186 |
joko |
1.6 |
|
187 |
joko |
1.3 |
int main(int argc, char *argv[]) { |
188 |
|
|
|
189 |
joko |
1.4 |
// MMF support |
190 |
|
|
DWORD err; |
191 |
joko |
1.3 |
HANDLE hMap, hFile; |
192 |
|
|
LPVOID pData; |
193 |
joko |
1.9 |
unsigned char *pDataBitmap; |
194 |
joko |
1.4 |
|
195 |
joko |
1.5 |
// workers |
196 |
joko |
1.16 |
int workers; |
197 |
joko |
1.7 |
int worker_index, worker_rows, worker_startrow; |
198 |
joko |
1.6 |
HANDLE *worker_handles; |
199 |
|
|
PWORKERARGS worker_args; |
200 |
joko |
1.16 |
int worker_count; |
201 |
joko |
1.6 |
|
202 |
joko |
1.10 |
// threads or processes? |
203 |
joko |
1.13 |
BOOL use_processes = FALSE; |
204 |
joko |
1.12 |
BOOL is_worker_process = FALSE; |
205 |
joko |
1.10 |
|
206 |
|
|
// information for creating processes |
207 |
|
|
STARTUPINFO si; |
208 |
|
|
PROCESS_INFORMATION pi; |
209 |
joko |
1.17 |
char szCmdline[MAX_COMMAND_LINE]; |
210 |
joko |
1.10 |
|
211 |
joko |
1.13 |
// command line stuff |
212 |
|
|
char arg_option[1024]; |
213 |
joko |
1.14 |
char arg_value[1024]; |
214 |
|
|
char *bmp_filename; |
215 |
|
|
char *verbose_option = ""; |
216 |
joko |
1.10 |
|
217 |
joko |
1.14 |
|
218 |
joko |
1.17 |
// check command line arguments |
219 |
joko |
1.14 |
if (argc < 2) { |
220 |
|
|
fprintf(stderr, "Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); |
221 |
|
|
exit(EXIT_FAILURE); |
222 |
|
|
} |
223 |
joko |
1.17 |
|
224 |
joko |
1.11 |
|
225 |
joko |
1.17 |
// parse command line arguments |
226 |
joko |
1.14 |
if (scan_argv(argc, argv, "--verbose", arg_value)) { |
227 |
|
|
VERBOSE = TRUE; |
228 |
joko |
1.10 |
} |
229 |
joko |
1.13 |
|
230 |
|
|
if (scan_argv(argc, argv, "--worker", arg_value)) { |
231 |
|
|
use_processes = TRUE; |
232 |
|
|
is_worker_process = TRUE; |
233 |
|
|
|
234 |
|
|
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
235 |
joko |
1.14 |
if (strlen(arg_value) == 0) { |
236 |
|
|
fprintf(stderr, "Please specify number of processes!\n"); |
237 |
|
|
exit(EXIT_FAILURE); |
238 |
|
|
} |
239 |
joko |
1.13 |
use_processes = TRUE; |
240 |
|
|
is_worker_process = FALSE; |
241 |
|
|
workers = atoi(arg_value); |
242 |
|
|
|
243 |
|
|
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
244 |
joko |
1.14 |
if (strlen(arg_value) == 0) { |
245 |
|
|
fprintf(stderr, "Please specify number of threads!\n"); |
246 |
|
|
exit(EXIT_FAILURE); |
247 |
|
|
} |
248 |
joko |
1.13 |
use_processes = FALSE; |
249 |
|
|
is_worker_process = FALSE; |
250 |
|
|
workers = atoi(arg_value); |
251 |
|
|
|
252 |
|
|
} |
253 |
|
|
|
254 |
joko |
1.10 |
|
255 |
joko |
1.11 |
if (VERBOSE && use_processes) { |
256 |
|
|
fprintf(stdout, "===================================================== "); |
257 |
joko |
1.12 |
if (is_worker_process) |
258 |
joko |
1.11 |
fprintf(stdout, "WORKER-PROCESS\n"); |
259 |
|
|
else |
260 |
|
|
fprintf(stdout, "MASTER-PROCESS\n"); |
261 |
joko |
1.10 |
} |
262 |
joko |
1.14 |
|
263 |
joko |
1.3 |
|
264 |
joko |
1.14 |
// master creates memory mapped file ("empty" image) |
265 |
joko |
1.12 |
if (!is_worker_process) { |
266 |
joko |
1.10 |
|
267 |
joko |
1.14 |
if (argc < 4) { |
268 |
|
|
fprintf(stderr, "Must give filename of image as third argument!\n"); |
269 |
|
|
exit(EXIT_FAILURE); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
bmp_filename = argv[3]; |
273 |
|
|
|
274 |
|
|
// create empty bmp-file (black background) |
275 |
|
|
write_blank_file(bmp_filename); |
276 |
|
|
|
277 |
joko |
1.12 |
// open file for reading and writing |
278 |
joko |
1.14 |
hFile = CreateFile(bmp_filename, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
279 |
joko |
1.10 |
if (INVALID_HANDLE_VALUE == hFile) { |
280 |
|
|
err = GetLastError(); |
281 |
|
|
printErrorAndExit("Error at CreateFile",err); |
282 |
|
|
} |
283 |
joko |
1.3 |
|
284 |
joko |
1.12 |
// create the file mapping object |
285 |
joko |
1.17 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, MMAP_NAME); |
286 |
joko |
1.10 |
if (NULL == hMap) { |
287 |
|
|
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
288 |
|
|
} |
289 |
joko |
1.12 |
|
290 |
|
|
// worker uses existing memory mapped file |
291 |
joko |
1.10 |
} else { |
292 |
|
|
|
293 |
|
|
// open existing mapping object |
294 |
joko |
1.17 |
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, MMAP_NAME); |
295 |
joko |
1.10 |
if (NULL == hMap) |
296 |
|
|
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
297 |
joko |
1.3 |
} |
298 |
|
|
|
299 |
joko |
1.12 |
// map the whole file into the process context |
300 |
joko |
1.3 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
301 |
|
|
if (NULL == pData) { |
302 |
|
|
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
|
306 |
|
|
// calculate pointer to beginning of bitmap |
307 |
|
|
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
308 |
|
|
|
309 |
|
|
// debugging |
310 |
joko |
1.9 |
if (VERBOSE) { |
311 |
joko |
1.11 |
fprintf(stdout, "pos. of file: %p\n", pData); |
312 |
|
|
fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap); |
313 |
joko |
1.9 |
} |
314 |
joko |
1.3 |
|
315 |
joko |
1.10 |
|
316 |
joko |
1.12 |
if (use_processes && is_worker_process) { |
317 |
joko |
1.10 |
|
318 |
|
|
if (VERBOSE) |
319 |
joko |
1.11 |
fprintf(stdout, "inside worker-process\n"); |
320 |
joko |
1.10 |
|
321 |
joko |
1.11 |
// get segment information from command line |
322 |
joko |
1.10 |
worker_startrow = atoi(argv[2]); |
323 |
|
|
worker_rows = atoi(argv[3]); |
324 |
|
|
|
325 |
|
|
// allocate memory for one worker's arguments |
326 |
|
|
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
327 |
joko |
1.15 |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
328 |
joko |
1.10 |
|
329 |
|
|
// assign worker's arguments |
330 |
|
|
worker_args[0].start_row = worker_startrow; |
331 |
|
|
worker_args[0].number_of_rows = worker_rows; |
332 |
|
|
worker_args[0].pBitmap = pDataBitmap; |
333 |
|
|
|
334 |
|
|
fractal_create_segment(&worker_args[0]); |
335 |
joko |
1.12 |
|
336 |
|
|
// cleanup mmap-handle |
337 |
|
|
if (!CloseHandle(hMap)) |
338 |
|
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
339 |
joko |
1.10 |
|
340 |
joko |
1.13 |
return 0; |
341 |
joko |
1.10 |
} |
342 |
|
|
|
343 |
joko |
1.3 |
/* |
344 |
joko |
1.9 |
// turn bitmap into white canvas |
345 |
joko |
1.3 |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
346 |
joko |
1.9 |
*pDataBitmap = 255; |
347 |
|
|
pDataBitmap++; |
348 |
joko |
1.3 |
} |
349 |
joko |
1.9 |
exit(0); |
350 |
joko |
1.3 |
*/ |
351 |
|
|
|
352 |
joko |
1.9 |
// allocate memory for bitmap |
353 |
|
|
/* |
354 |
|
|
if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL) |
355 |
|
|
perror("malloc"), exit(1); |
356 |
|
|
*/ |
357 |
|
|
|
358 |
joko |
1.6 |
// allocate memory for table of all worker handles |
359 |
joko |
1.9 |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
360 |
joko |
1.15 |
perror("Error while allocating memory for worker handles via malloc"), exit(1); |
361 |
joko |
1.6 |
|
362 |
|
|
// allocate memory for table of all worker arguments |
363 |
joko |
1.9 |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
364 |
joko |
1.15 |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
365 |
joko |
1.8 |
|
366 |
joko |
1.6 |
|
367 |
joko |
1.10 |
// calculate bitmap segment length for workers |
368 |
joko |
1.5 |
worker_rows = YSIZE / workers; |
369 |
joko |
1.9 |
if (VERBOSE) |
370 |
joko |
1.11 |
fprintf(stdout, "rows for each worker: %i\n", worker_rows); |
371 |
joko |
1.10 |
|
372 |
|
|
// start workers |
373 |
joko |
1.5 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
374 |
|
|
|
375 |
joko |
1.9 |
// debugging: just run single thread |
376 |
joko |
1.7 |
//if (worker_index == 1) |
377 |
|
|
// continue; |
378 |
|
|
|
379 |
joko |
1.5 |
// number of row to start for each worker |
380 |
|
|
worker_startrow = worker_index * worker_rows; |
381 |
|
|
|
382 |
|
|
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
383 |
|
|
if (worker_index == workers - 1) { |
384 |
|
|
worker_rows = YSIZE - worker_startrow; |
385 |
joko |
1.9 |
if (VERBOSE) |
386 |
joko |
1.11 |
fprintf(stdout, "rows for last worker: %i\n", worker_rows); |
387 |
joko |
1.5 |
} |
388 |
joko |
1.6 |
|
389 |
joko |
1.10 |
// assign each worker's arguments |
390 |
joko |
1.6 |
worker_args[worker_index].start_row = worker_startrow; |
391 |
|
|
worker_args[worker_index].number_of_rows = worker_rows; |
392 |
|
|
worker_args[worker_index].pBitmap = pDataBitmap; |
393 |
|
|
|
394 |
joko |
1.10 |
if (!use_processes) { |
395 |
joko |
1.6 |
|
396 |
joko |
1.10 |
worker_handles[worker_index] = CreateThread( |
397 |
|
|
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
398 |
|
|
0, // SIZE_T dwStackSize, |
399 |
|
|
&fractal_create_segment, // LPTHREAD_START_ROUTINE lpStartAddress, |
400 |
|
|
&worker_args[worker_index], // LPVOID lpParameter, |
401 |
|
|
0, // DWORD dwCreationFlags, |
402 |
|
|
NULL // LPDWORD lpThreadId |
403 |
|
|
); |
404 |
|
|
|
405 |
|
|
if (!worker_handles[worker_index]) |
406 |
|
|
printErrorAndExit("CreateThread failed", GetLastError()); |
407 |
|
|
|
408 |
|
|
} else { |
409 |
|
|
|
410 |
joko |
1.14 |
if (VERBOSE) |
411 |
|
|
verbose_option = "--verbose"; |
412 |
joko |
1.17 |
_snprintf(szCmdline, MAX_COMMAND_LINE - 1, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); |
413 |
joko |
1.11 |
if (VERBOSE) |
414 |
|
|
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
415 |
joko |
1.17 |
|
416 |
|
|
// prepare data structures for CreateProcess |
417 |
joko |
1.10 |
ZeroMemory( &si, sizeof(si) ); |
418 |
|
|
si.cb = sizeof(si); |
419 |
|
|
ZeroMemory( &pi, sizeof(pi) ); |
420 |
|
|
|
421 |
|
|
if (!CreateProcess( |
422 |
|
|
NULL, // No module name (use command line) |
423 |
|
|
szCmdline, // Command line |
424 |
|
|
NULL, // Process handle not inheritable |
425 |
|
|
NULL, // Thread handle not inheritable |
426 |
|
|
FALSE, // Set handle inheritance to FALSE |
427 |
|
|
0, // No creation flags |
428 |
|
|
NULL, // Use parent's environment block |
429 |
|
|
NULL, // Use parent's starting directory |
430 |
|
|
&si, // Pointer to STARTUPINFO structure |
431 |
|
|
&pi // Pointer to PROCESS_INFORMATION structure |
432 |
|
|
)) |
433 |
|
|
printErrorAndExit("CreateProcess failed", GetLastError()); |
434 |
|
|
|
435 |
|
|
worker_handles[worker_index] = pi.hProcess; |
436 |
|
|
|
437 |
|
|
} |
438 |
|
|
|
439 |
joko |
1.5 |
} |
440 |
|
|
|
441 |
joko |
1.9 |
if (VERBOSE) |
442 |
joko |
1.11 |
fprintf(stdout, "waiting for workers to finish...\n"); |
443 |
joko |
1.16 |
|
444 |
|
|
// wait for all workers |
445 |
|
|
for (worker_index = 0; worker_index < workers; worker_index += MAXIMUM_WAIT_OBJECTS) { |
446 |
|
|
worker_count = ((workers - worker_index) > MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (workers - worker_index); |
447 |
|
|
if (WaitForMultipleObjects(worker_count, &worker_handles[worker_index], TRUE, INFINITE) == WAIT_FAILED) |
448 |
|
|
printErrorAndExit("Error at WaitForMultipleObjects", GetLastError()); |
449 |
|
|
} |
450 |
joko |
1.7 |
|
451 |
joko |
1.9 |
// debugging: just run single thread |
452 |
joko |
1.7 |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
453 |
|
|
// perror("WaitForSingleObject"); |
454 |
|
|
|
455 |
|
|
// close all worker handles |
456 |
joko |
1.17 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
457 |
|
|
if (!CloseHandle(worker_handles[worker_index])) |
458 |
|
|
printErrorAndExit("Error at CloseHandle for worker handles", GetLastError()); |
459 |
|
|
} |
460 |
joko |
1.7 |
|
461 |
joko |
1.12 |
// write the result into the file |
462 |
joko |
1.17 |
if (!FlushViewOfFile(pData, 0)) |
463 |
|
|
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
464 |
joko |
1.3 |
|
465 |
joko |
1.12 |
// remove the mapped file |
466 |
joko |
1.17 |
if (!UnmapViewOfFile(pData)) |
467 |
|
|
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
468 |
joko |
1.3 |
|
469 |
joko |
1.12 |
// cleanup handles |
470 |
joko |
1.17 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) |
471 |
|
|
printErrorAndExit("Error at CloseHandle for memory mapped file", GetLastError()); |
472 |
joko |
1.1 |
|
473 |
joko |
1.9 |
free(worker_args); |
474 |
|
|
free(worker_handles); |
475 |
joko |
1.8 |
|
476 |
|
|
return 0; |
477 |
|
|
|
478 |
joko |
1.1 |
} |