diff options
Diffstat (limited to 'src/common.c')
-rw-r--r-- | src/common.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..558dedf --- /dev/null +++ b/src/common.c @@ -0,0 +1,59 @@ +// +// Copyright (C) 2015 Aaron Ball <nullspoon@iohq.net> +// +// Cnetbench is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Cnetbench is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with cnetbench. If not, see <http://www.gnu.org/licenses/>. +// +#include "common.h" + +// +// Watches an array of pids for their exit, and updates the output array +// exit_times with each pid's exit timeval struct timestamp (epoch with +// microseconds). +// +// Note that this function does not start the pids, but only watches them. To +// calculate runtime, external code needs to track when the pids were created. +// +// @param pids Array of pid_t pids to be watched +// @param count Number of pids in the pids array +// @param exit_times Array of timeval structs that will contain exit times for +// all of the pids in the array. Must be at least the size of +// the pids array or unpredictable behavior may result. +// +// @return int Overall status of all exited pids (if any fail exit, returns 1) +// +int time_pids(pid_t* pids, int count, struct timeval *exit_times) { + int i; + int waiting = 1; + + while(waiting != 0) { + waiting = 0; + for(i = 0; i < count; i++) { + // Pid with this index has exited, skip it. + if(pids[i] == 0) + continue; + + // 0 = state change (exit) + if(waitpid(pids[i], NULL, WNOHANG) != 0) { + // Child has exited + pids[i] = 0; + // Write the exit time + gettimeofday(&exit_times[i], NULL); + } else { + waiting = 1; + } + } + sleep(1); + } + return 0; +} |