File: clock.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (29 lines) | stat: -rw-r--r-- 601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
  clockid_t id;
  struct timespec ts;

  if (clock_getcpuclockid(getpid(), &id) != 0) {
    perror("clock_getcpuclockid");
    exit(EXIT_FAILURE);
  }

  while (1) {
    if (clock_gettime(id, &ts) == -1) {
      perror("clock_gettime");
      exit(EXIT_FAILURE);
    }

    printf("CPU-time clock (%d) for PID %s is %ld.%09ld seconds\n",
           id, argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
    fflush(stdout);
    sleep(1);
  }
  exit(EXIT_SUCCESS);
}