#endif
 }
 
-static void ctrl_handler(int signo)
-{
-       free(startptr);
-       printf("\nEnding\n");
-       sb();
-       exit(EXIT_SUCCESS);
-}
-
 static void cl_flush(void *p)
 {
 #if defined(__i386) || defined(__x86_64)
        unsigned long long cache_size = span;
        int ret;
 
-       /* set up ctrl-c handler */
-       if (signal(SIGINT, ctrl_handler) == SIG_ERR)
-               printf("Failed to catch SIGINT!\n");
-       if (signal(SIGHUP, ctrl_handler) == SIG_ERR)
-               printf("Failed to catch SIGHUP!\n");
-
        ret = fill_cache(cache_size, malloc_and_init_memory, memflush, op,
                         resctrl_val);
        if (ret) {
 
        exit(EXIT_SUCCESS);
 }
 
+/*
+ * Register CTRL-C handler for parent, as it has to kill
+ * child process before exiting.
+ */
+int signal_handler_register(void)
+{
+       struct sigaction sigact;
+       int ret = 0;
+
+       sigact.sa_sigaction = ctrlc_handler;
+       sigemptyset(&sigact.sa_mask);
+       sigact.sa_flags = SA_SIGINFO;
+       if (sigaction(SIGINT, &sigact, NULL) ||
+           sigaction(SIGTERM, &sigact, NULL) ||
+           sigaction(SIGHUP, &sigact, NULL)) {
+               perror("# sigaction");
+               ret = -1;
+       }
+       return ret;
+}
+
+/*
+ * Reset signal handler to SIG_DFL.
+ * Non-Value return because the caller should keep
+ * the error code of other path even if sigaction fails.
+ */
+void signal_handler_unregister(void)
+{
+       struct sigaction sigact;
+
+       sigact.sa_handler = SIG_DFL;
+       sigemptyset(&sigact.sa_mask);
+       if (sigaction(SIGINT, &sigact, NULL) ||
+           sigaction(SIGTERM, &sigact, NULL) ||
+           sigaction(SIGHUP, &sigact, NULL)) {
+               perror("# sigaction");
+       }
+}
+
 /*
  * print_results_bw:   the memory bandwidth results are stored in a file
  * @filename:          file that stores the results
 
        ksft_print_msg("Benchmark PID: %d\n", bm_pid);
 
-       /*
-        * Register CTRL-C handler for parent, as it has to kill benchmark
-        * before exiting
-        */
-       sigact.sa_sigaction = ctrlc_handler;
-       sigemptyset(&sigact.sa_mask);
-       sigact.sa_flags = SA_SIGINFO;
-       if (sigaction(SIGINT, &sigact, NULL) ||
-           sigaction(SIGTERM, &sigact, NULL) ||
-           sigaction(SIGHUP, &sigact, NULL)) {
-               perror("# sigaction");
-               ret = errno;
+       ret = signal_handler_register();
+       if (ret)
                goto out;
-       }
 
        value.sival_ptr = benchmark_cmd;
 
        /* Taskset benchmark to specified cpu */
        ret = taskset_benchmark(bm_pid, param->cpu_no);
        if (ret)
-               goto out;
+               goto unregister;
 
        /* Write benchmark to specified control&monitoring grp in resctrl FS */
        ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
                                      resctrl_val);
        if (ret)
-               goto out;
+               goto unregister;
 
        if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
            !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
                ret = initialize_mem_bw_imc();
                if (ret)
-                       goto out;
+                       goto unregister;
 
                initialize_mem_bw_resctrl(param->ctrlgrp, param->mongrp,
                                          param->cpu_no, resctrl_val);
                    sizeof(pipe_message)) {
                        perror("# failed reading message from child process");
                        close(pipefd[0]);
-                       goto out;
+                       goto unregister;
                }
        }
        close(pipefd[0]);
        if (sigqueue(bm_pid, SIGUSR1, value) == -1) {
                perror("# sigqueue SIGUSR1 to child");
                ret = errno;
-               goto out;
+               goto unregister;
        }
 
        /* Give benchmark enough time to fully run */
                }
        }
 
+unregister:
+       signal_handler_unregister();
 out:
        kill(bm_pid, SIGKILL);
        umount_resctrlfs();