Skip to content
Related Articles

Related Articles

pthread_getcpuclockid() function in C with Examples

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 18 Nov, 2021
Improve Article
Save Article

The pthread_getcpuclockid() function returns the clock ID of the CPU-time clock from a specified thread.

Syntax:

int pthread_getcpuclockid (pthread_t id, clockid_t* clock_id);

Parameters: This method accepts following parameters:

  • thread: The ID of the thread that you want to get the clock ID for, which you can get when you call pthread_create() or pthread_self().
  • clock_id: A pointer to a clockid_t object where the function can store the clock ID.

Return value: This method returns the CPU clock time for a specific thread.

Below are some examples to show the implementation of the pthread_getcpuclockid() method:

Example 1: The program below creates a thread and then uses clock_gettime(2) to retrieve the total process CPU time and the per-thread CPU time consumed by the two threads.

C




// C program to implement
// the above approach
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
  
#define handle_error(msg) \
    do
{
    perror(msg);
    exit(EXIT_FAILURE);
}
while (0)
  
#define handle_error_en(en, msg) \
    do
{
    errno = en;
    perror(msg);
    exit(EXIT_FAILURE);
}
while (0)
  
    static void* thread_start(void* arg)
    {
        printf(
            "Subthread starting infinite loop\n");
        for (;;)
            continue;
    }
  
static void pclock(char* msg,
                   clockid_t cid)
{
    struct timespec ts;
    printf("%s", msg);
  
    if (clock_gettime(cid,
                      &ts)
        == -1)
        handle_error("clock_gettime");
    printf("%4jd.%03ld\n",
           (intmax_t)ts.tv_sec,
           ts.tv_nsec / 1000000);
}
  
// Driver code
int main(int argc,
         char* argv[])
{
    pthread_t thread;
    clockid_t cid;
    int s;
    s = pthread_create(&thread, NULL,
                       thread_start, NULL);
  
    if (s != 0)
        handle_error_en(s, "pthread_create");
  
    printf("Main thread sleeping\n");
    sleep(1);
  
    printf(
        "Main thread consuming some CPU time...\n");
  
    for (int j = 0; j < 2000000; j++)
        getppid();
  
    pclock("Process total CPU time: ",
           CLOCK_PROCESS_CPUTIME_ID);
    s = pthread_getcpuclockid(pthread_self(),
                              &cid);
  
    if (s != 0)
        handle_error_en(s, "pthread_getcpuclockid");
  
    pclock("Main thread CPU time:   ",
           cid);
  
    /* The preceding 4 lines of code could 
     have been replaced by:
     pclock("Main thread CPU time:   ", 
             CLOCK_THREAD_CPUTIME_ID); */
    s = pthread_getcpuclockid(thread, &cid);
  
    if (s != 0)
        handle_error_en(s, "pthread_getcpuclockid");
  
    pclock("Subthread CPU time: 1    ",
           cid);
  
    // Terminates both threads
    exit(EXIT_SUCCESS);
}


Output:

$ ./a.out
Main thread sleeping
Subthread starting infinite loop
Main thread consuming some CPU time…
Process total CPU time: 1.368
Main thread CPU time: 0.376
Subthread CPU time: 0.992

Example 2: The program below creates a process and then uses clock_gettime(2) to retrieve the total process CPU time and the parent and child process CPU time consumed by the two processes.

C




// C program to implement
// the above approach
#include <pthread.h>
#include <stdio.h>
#include <time.h>
  
pthread_cond_t cond2;
pthread_condattr_t cond2attr;
  
#define test(clk_id) \
    {                \
    printf("%s:%d\n",
#clk_id, clk_id);
           }
  
static void print_clock(char* msg,
                        clockid_t cid)
{
    struct timespec ts;
  
    printf("%s", msg);
  
    if (clock_gettime(cid, &ts) == -1)
        fprintf(stderr,
                "clock_gettime");
  
    printf("%4ld.%03ld\n",
           ts.tv_sec,
           ts.tv_nsec / 1000000);
}
  
void* test_task_fn(void* unused)
{
    printf("test_task_fn.\n");
  
    for (;;)
        continue;
  
    static int status = 12121;
  
    pthread_exit(&status);
    return NULL;
}
  
// Driver code
int main()
{
    int* pstatus;
    int ret;
    clockid_t clockid = 0;
    pthread_t thread_id;
  
    pthread_create(&thread_id, NULL,
                   test_task_fn, NULL);
  
    printf("Main thread sleeping\n");
    sleep(1);
  
    ret = pthread_getcpuclockid(
        thread_id, &clockid);
    print_clock("  Child", clockid);
  
    ret = pthread_getcpuclockid(
        pthread_self(), &clockid);
    print_clock(" Parent",
                clockid);
    print_clock("Process",
                CLOCK_PROCESS_CPUTIME_ID);
  
    pthread_join(thread_id,
                 (void**)&pstatus);
  
    printf("pstatus = %d\n",
           *pstatus);
  
    return 0;
}


Output:

[root@localhost pthread]# ./compile.sh pthread_getcpuclockid.c
[root@localhost pthread]# ./a.out
Main thread sleeping
test_task_fn.
Child 0.999
Parent 0.001
Process 1.000


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!