summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2010-02-23 15:02:48 +0100
committerDavid Sommerseth <davids@redhat.com>2010-02-23 17:30:43 +0100
commit4e37a2b689fc0d081e41ca656be48aba53ff350b (patch)
treefab1ab04de5f975c6c50ae81b9f87140cc970db9
parentf273b4bd85fe7d8561bf8c9030b6a9d8bb8b8e9f (diff)
downloadrt-tests-4e37a2b689fc0d081e41ca656be48aba53ff350b.tar.gz
Added signal handling in hackbench
When receiving SIGINT or SIGTERM, it will now reap all worker threads/processes and properly stop them.
-rw-r--r--src/hackbench/hackbench.c32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/hackbench/hackbench.c b/src/hackbench/hackbench.c
index 7a731d3..c8d12bd 100644
--- a/src/hackbench/hackbench.c
+++ b/src/hackbench/hackbench.c
@@ -26,6 +26,7 @@
#include <sys/poll.h>
#include <limits.h>
#include <getopt.h>
+#include <signal.h>
static unsigned int datasize = 100;
static unsigned int loops = 100;
@@ -60,8 +61,17 @@ typedef union {
long long error;
} childinfo_t;
+childinfo_t *child_tab = NULL;
+unsigned int total_children = 0;
+unsigned int signal_caught = 0;
+
inline static void sneeze(const char *msg) {
- fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno));
+ /* Avoid calling these functions when called from a code path
+ * which involves sigcatcher(), as they are not reentrant safe.
+ */
+ if( !signal_caught ) {
+ fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno));
+ }
}
static void barf(const char *msg)
@@ -385,15 +395,22 @@ static void process_options (int argc, char *argv[])
}
}
-
+void sigcatcher(int sig) {
+ /* All caught signals will cause the program to exit */
+ signal_caught = 1;
+ if( child_tab && (total_children > 0) ) {
+ reap_workers(child_tab, total_children, 1);
+ }
+ fprintf(stderr, "** Operation aborted **\n");
+ exit(0);
+}
int main(int argc, char *argv[])
{
- unsigned int i, total_children;
+ unsigned int i;
struct timeval start, stop, diff;
int readyfds[2], wakefds[2];
char dummy;
- childinfo_t *child_tab;
process_options (argc, argv);
@@ -403,13 +420,18 @@ int main(int argc, char *argv[])
printf("Each sender will pass %d messages of %d bytes\n", loops, datasize);
fflush(NULL);
- child_tab = malloc(num_fds * 2 * num_groups * sizeof(childinfo_t));
+ child_tab = calloc(num_fds * 2 * num_groups, sizeof(childinfo_t));
if (!child_tab)
barf("main:malloc()");
fdpair(readyfds);
fdpair(wakefds);
+ /* Catch some signals */
+ signal(SIGINT, sigcatcher);
+ signal(SIGTERM, sigcatcher);
+ signal(SIGHUP, SIG_IGN);
+
total_children = 0;
for (i = 0; i < num_groups; i++) {
int c = group(child_tab, total_children, num_fds, readyfds[1], wakefds[0]);