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