Hosein Zarifi
Hosein Zarifi

Reputation: 69

Find CPU times and system times of process in linux

I have a main program that creates two children and each children calls execlv. At the end of the program how do I calculate the CPU times and system times of the parent and two process?

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>



int main()
{


    pid_t pid1,pid2,wid;  // variable for parent and two children
  char *my_args[3];     // strign array for containing the arguments for executing sigShooter1
 // int aInt = 368;       //
  char str[15];     // strign to contain the pids of children when passing as command line arguments


   pid1 = fork();
   if (pid1 < 0)
    {
      fprintf(stderr, ": fork failed: %s\n", strerror(errno));
      exit(1);
    }

    if(pid1 == 0)
    {
      my_args[0] = "sigperf1";
      my_args[1] = "0";
      my_args[2] = NULL;
      execv("sigshooter1",my_args);
      fprintf(stderr,"sigshooter1 cannot be executed by first child...");
      exit(-1);
    }

    pid2 = fork();

    if(pid2 < 0)
    {
       fprintf(stderr, ": fork failed: %s\n", strerror(errno));
       exit(1);
    }

    if(pid2 == 0)
    {
      sprintf(str, "%d", pid1);
      my_args[0] = "sigperf1";
      my_args[1] = str;
      my_args[2] = NULL; 
     // printf("this is converted = %s\n",my_args[1]);
      //sleep(1);
      execv("sigshooter1",my_args);
      fprintf(stderr,"sigshooter1 cannot be executed by second child...");
      exit(-1);
    }


wid = wait(NULL);



}

Upvotes: 1

Views: 65

Answers (1)

Dmitry Grigoryev
Dmitry Grigoryev

Reputation: 3203

You'll need a profiler for that. For starters, you can run perf stat ./a.out to get the total CPU time of all three processes, and perf stat -i ./a.out to get the CPU time of parent process only.

If you need something more detailed, take a look at more serious tools like valgrind or gprof.

Upvotes: 1

Related Questions