views:

168

answers:

5

I am building programming contest software. A user's program is received by our judging system and is evaluated by compiling it and running it via a fork() and exec(). The parent process waits for the child (submission's process) to exit, and then cleans it up.

To give useful information about the program's run, I want to measure the CPU time and peak memory used by the program. Does the Linux kernel keep track of these values? Is there any other way to get this information?

A: 

A low-tech (but easy) solution is to dump the output of top in batch mode periodically and parse it afterwards.

leonm
A: 

You can check the top command. That might be of some help.

Jay
Would be better if the downvote is explained? I know that people have posted better answers for this question, but, I don't think this answer was totally irrelevant or wrong. Please correct me if I am wrong.
Jay
A: 

The time(1) program may help, i guess. It is much simpler than polling top.

An excerpt from the man page:

   Disregarding  the
   name  of  the  utility, GNU makes it output lots of useful information,
   not only about time used, but also on other resources like memory,  I/O
   and  IPC calls (where available).
Roman D
+4  A: 

You can use the getrusage() or acct() (more info here) syscalls

jpalecek
+5  A: 

If you call the wait4() system call to reap the child when it finishes, it will fill out a struct rusage structure with the resource usage of the child (ru_utime and ru_stime hold the user and system CPU time used by the child respectively).

caf