[Normal] 프로그램 cpu사용률을 출력하는 exit 핸드러를 가지는 프로그램
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
static void showtimes(void) {
double ticks;
struct tms tinfo;
if ((ticks = (double) sysconf(_SC_CLK_TCK)) == -1)
perror("Failed to determine clock ticks per second");
else if (times(&tinfo) == (clock_t)-1)
perror("Failed to get times information");
else {
fprintf(stderr, "User time: %8.3f seconds\n",
tinfo.tms_utime/ticks);
fprintf(stderr, "System time: %8.3f seconds\n",
tinfo.tms_stime/ticks);
fprintf(stderr, "Children's user time: %8.3f seconds\n",
tinfo.tms_cutime/ticks);
fprintf(stderr, "Children's system time: %8.3f seconds\n",
tinfo.tms_cstime/ticks);
}
}
int main(void) {
if (atexit(showtimes)) {
fprintf(stderr, "Failed to install showtimes exit handler\n");
return 1;
}
/* rest of main program goes here */
return 0;
}
User time: 0.000 seconds
System time: 0.000 seconds
Children's user time: 0.000 seconds
Children's system time: 0.000 seconds