From fd647af47d1fe8fddc7e0c72c43bfdd31eaa2129 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 30 May 2012 11:10:07 +0200 Subject: Tighten up file and error handling --- src/indexamajig.c | 104 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/indexamajig.c b/src/indexamajig.c index 3c7b0cb9..223403ad 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -492,7 +492,7 @@ static void run_work(const struct index_args *iargs, c = sprintf(buf, "%i\n", pargs.indexable); w = write(results_pipe, buf, c); if ( w < 0 ) { - ERROR("write P0"); + ERROR("write P0\n"); } free(line); @@ -500,7 +500,6 @@ static void run_work(const struct index_args *iargs, } /* close my pipes */ fclose(fh); - close(filename_pipe); close(results_pipe); } @@ -611,10 +610,11 @@ int main(int argc, char *argv[]) int t_last_stats; pid_t *pids; int *filename_pipes; - int *result_pipes; + FILE **result_fhs; fd_set fds; int i; - int allDone, nFinished; + int allDone; + int *finished; copyme = new_copy_hdf5_field_list(); if ( copyme == NULL ) { @@ -1020,11 +1020,15 @@ int main(int argc, char *argv[]) FD_ZERO(&fds); filename_pipes = calloc(n_proc, sizeof(int)); - result_pipes = calloc(n_proc, sizeof(int)); - if ( (filename_pipes == NULL) || (result_pipes == NULL) ) { + result_fhs = calloc(n_proc, sizeof(FILE *)); + if ( filename_pipes == NULL ) { ERROR("Couldn't allocate memory for pipes.\n"); return 1; } + if ( result_fhs == NULL ) { + ERROR("Couldn't allocate memory for pipe file handles.\n"); + return 1; + } pids = calloc(n_proc, sizeof(pid_t)); if ( pids == NULL ) { @@ -1032,6 +1036,12 @@ int main(int argc, char *argv[]) return 1; } + finished = calloc(n_proc, sizeof(int)); + if ( finished == NULL ) { + ERROR("Couldn't allocate memory for process flags.\n"); + return 1; + } + /* Fork the right number of times */ for ( i=0; i fdmax ) { - fdmax = result_pipes[i]; - } + + int fd; + + if ( finished[i] ) continue; + + fd = fileno(result_fhs[i]); + FD_SET(fd, &fds); + if ( fd > fdmax ) fdmax = fd; + } r = select(fdmax+1, &fds, NULL, NULL, &tv); if ( r == -1 ) { - ERROR("select() failed!\n"); + ERROR("select() failed: %s\n", strerror(errno)); continue; } @@ -1127,15 +1155,27 @@ int main(int argc, char *argv[]) char *nextImage; char results[1024]; - - if ( !FD_ISSET(result_pipes[i], &fds) ) continue; - - r = read(result_pipes[i], results, 1024); - if ( r < 0 ) { - ERROR("read() failed!"); + char *rval; + int fd; + + if ( finished[i] ) continue; + + fd = fileno(result_fhs[i]); + if ( !FD_ISSET(fd, &fds) ) continue; + + rval = fgets(results, 1024, result_fhs[i]); + if ( rval == NULL ) { + if ( feof(result_fhs[i]) ) { + /* Process died */ + finished[i] = 1; + } else { + ERROR("fgets() failed: %s\n", + strerror(errno)); + } continue; } + chomp(results); n_indexable += atoi(results); n_processed++; @@ -1146,18 +1186,15 @@ int main(int argc, char *argv[]) prefix); if ( nextImage == NULL ) { - /* no more images */ - nFinished++; - if ( nFinished == n_proc ) { - allDone = 1; - } + /* No more images */ + finished[i] = 1; } else { r = write(filename_pipes[i], nextImage, strlen(nextImage)); r -= write(filename_pipes[i], "\n", 1); if ( r < 0 ) { - ERROR("write pipe"); + ERROR("write pipe\n"); } } @@ -1179,12 +1216,21 @@ int main(int argc, char *argv[]) } + allDone = 1; + for ( i=0; i