简体   繁体   中英

How do I record the process execution time in nanoseconds using C on a linux operating system using clock_getttime()?

I am trying to record the time of execution for a process using C while on a linux operating system, I believe it is CentOs, it is a school server and I don't know the hardware specs. The program I am trying to run looks in a directory and runs all the jobs in the directory that follow the pattern Job1, Job2 etc. I am running these files which are executable files by using the system call system() fucntion with an absolute path to each executable file as a parameter (ie system(jobpath);).

I am also trying to time how long it takes to run the job that I just called by calling clock_gettime() before and after the system(jobpath) call. I have two timespec variables which I am passing to the two different calls to clock_gettime() called start and end. I am also using the CLOCK_PROCESS_CPUTIME_ID clock ID, and the code for that part looks something like this:

           clock_gettime(CLOCK_PROCESS_CPUTIME_ID, start);
           system(jobpath)//is a path to a job that takes 2-3 seconds in real time to finish
           clock_gettime(CLOCK_PROCESS_CPUTIME_ID, end);

The only problem is that when I go to print out the tv_nsec variable for end, I get a zero value when it obviously does not take 0 nanoseconds to run the jobs. Can anyone tell me why the function will record zero nanoseconds for something that obviously takes more time than that, and what I can possibly do to get real results?

The command executed by system will be run in another process so it won't add any cpu time to the current process.

Edit: As to your second question, you could use clock_gettime with CLOCK_MONOTONIC to time the actual elapsed time of the system call, but that won't tell you the cpu elapsed time of the system call. I'm not sure if the actual elapsed time is good enough for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM