Admin/bash_process/bash_process.c
changeset 62567 cb4e6ca06505
parent 62300 f41884b9c4f1
child 62568 3541bc1e97d2
     1.1 --- a/Admin/bash_process/bash_process.c	Wed Mar 09 13:12:51 2016 +0100
     1.2 +++ b/Admin/bash_process/bash_process.c	Wed Mar 09 13:33:21 2016 +0100
     1.3 @@ -3,11 +3,14 @@
     1.4  Bash process with separate process group id.
     1.5  */
     1.6  
     1.7 +#include <stdio.h>
     1.8  #include <stdlib.h>
     1.9 -#include <stdio.h>
    1.10  #include <string.h>
    1.11 +#include <sys/resource.h>
    1.12 +#include <sys/time.h>
    1.13  #include <sys/types.h>
    1.14  #include <sys/wait.h>
    1.15 +#include <time.h>
    1.16  #include <unistd.h>
    1.17  
    1.18  static void fail(const char *msg)
    1.19 @@ -22,26 +25,48 @@
    1.20  {
    1.21    /* args */
    1.22  
    1.23 -  if (argc < 2) {
    1.24 -    fprintf(stderr, "Bad arguments: missing pid file\n");
    1.25 +  if (argc < 3) {
    1.26 +    fprintf(stderr, "Bad arguments: PID_FILE and TIMING_FILE required\n");
    1.27      fflush(stderr);
    1.28      exit(1);
    1.29    }
    1.30    char *pid_name = argv[1];
    1.31 +  char *timing_name = argv[2];
    1.32  
    1.33  
    1.34 -  /* setsid */
    1.35 +  /* potential fork */
    1.36  
    1.37 -  if (setsid() == -1) {
    1.38 +  time_t time_start = time(NULL);
    1.39 +
    1.40 +  if (strlen(timing_name) > 0 || setsid() == -1) {
    1.41      pid_t pid = fork();
    1.42 -    int status;
    1.43  
    1.44      if (pid == -1) fail("Cannot set session id (failed to fork)");
    1.45      else if (pid != 0) {
    1.46 +      int status;
    1.47 +
    1.48        if (waitpid(pid, &status, 0) == -1) {
    1.49          fail("Cannot join forked process");
    1.50        }
    1.51  
    1.52 +      /* report timing */
    1.53 +
    1.54 +      if (strlen(timing_name) > 0) {
    1.55 +        long long timing_elapsed = (time(NULL) - time_start) * 1000;
    1.56 +
    1.57 +        struct rusage ru;
    1.58 +        getrusage(RUSAGE_CHILDREN, &ru);
    1.59 +
    1.60 +        long long timing_cpu =
    1.61 +          ru.ru_utime.tv_sec * 1000 + ru.ru_utime.tv_usec / 1000 +
    1.62 +          ru.ru_stime.tv_sec * 1000 + ru.ru_stime.tv_usec / 1000;
    1.63 +
    1.64 +        FILE *timing_file = fopen(timing_name, "w");
    1.65 +        if (timing_file == NULL) fail("Cannot open timing file");
    1.66 +        fprintf(timing_file, "%lld %lld", timing_elapsed, timing_cpu);
    1.67 +        fclose(timing_file);
    1.68 +      }
    1.69 +
    1.70        if (WIFEXITED(status)) {
    1.71          exit(WEXITSTATUS(status));
    1.72        }
    1.73 @@ -74,9 +99,10 @@
    1.74    /* shift command line */
    1.75  
    1.76    int i;
    1.77 -  for (i = 2; i < argc; i++) {
    1.78 -    argv[i - 2] = argv[i];
    1.79 +  for (i = 3; i < argc; i++) {
    1.80 +    argv[i - 3] = argv[i];
    1.81    }
    1.82 +  argv[argc - 3] = NULL;
    1.83    argv[argc - 2] = NULL;
    1.84    argv[argc - 1] = NULL;
    1.85