From bd9e975a30d3d5c761f77c0b0115b0a5accb59ce Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 11 May 2016 18:30:31 +1200 Subject: Fix bug with processing times < 1s --- fractal-gen.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/fractal-gen.c b/fractal-gen.c index 9b6b2e2..30f405b 100644 --- a/fractal-gen.c +++ b/fractal-gen.c @@ -32,7 +32,9 @@ #include #include #include +#include #include +#include #include static struct section_generator generators[] = { @@ -55,6 +57,7 @@ int main(int argc, char **argv) data_section* sections = NULL; data_section *s = NULL; generator_func generator = NULL; + pid_t child = 0; /* who are we? */ argv0 = argv[0]; @@ -77,11 +80,13 @@ int main(int argc, char **argv) } /* Allocate memory for sections */ - if ((sections = malloc(sizeof(data_section)*cores)) == NULL) - { - perror("malloc"); +/* if ((sections = malloc(sizeof(data_section)*cores)) == NULL)*/ + sections = mmap(NULL, sizeof(data_section)*cores, PROT_READ|PROT_WRITE, + MAP_SHARED|MAP_ANONYMOUS, -1, 0); +/* { + perror("mmap"); return 1; - } + }*/ ram_nice = (size*size)/clust_total; if (ram_nice < 1024) @@ -122,7 +127,7 @@ int main(int argc, char **argv) while(i-- + 1) free(sections[i].data); - free(sections); + munmap(sections, sizeof(data_section)*cores); return 1; } sections[i].core = i; @@ -133,18 +138,28 @@ int main(int argc, char **argv) } s = &(sections[cores-1]); - while((x = s->idx) < s->datasize) + + switch (child = fork()) { - fprintf(stderr, "Thread %d: %.4f%%\r", - cores-1, - 100.f*(double)x/s->datasize); - sleep(1); + case 0: + while((x = s->idx) < s->datasize) + { + fprintf(stderr, "Thread %d: %.4f%%\r", + cores-1, + 100.f*(double)x/s->datasize); + sleep(1); + } + break; + case -1: + perror("progress thread fork"); + default: + break; } - /* Wait for each thread to complete */ for (i = 0; i < cores; i++) pthread_join(sections[i].thread, NULL); + kill(child, SIGKILL); /* Output PGM Header */ printf("P5\n%d\n%d\n255\n",size,size/clust_total); @@ -165,7 +180,7 @@ int main(int argc, char **argv) for (i = 0; i < cores; i++) free(sections[i].data); - free(sections); + munmap(sections, sizeof(data_section)*cores); return 0; } -- cgit v1.1