当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 pthread_getcpuclockid()用法及代码示例


pthread_getcpuclockid() 函数返回来自指定线程的 CPU-time 时钟的时钟 ID。

用法:

int pthread_getcpuclockid (pthread_t id, clockid_t* clock_id);

参数:此方法接受以下参数:



  • thread:要获取时钟ID的线程ID,在调用pthread_create()或pthread_self()时可以得到。
  • clock_id:指向函数可以存储时钟 ID 的 clockid_t 对象的指针。

返回值:此方法返回特定线程的 CPU 时钟时间。

下面是一些示例来展示 pthread_getcpuclockid() 方法的实现:

范例1:下面的程序创建一个线程,然后使用 clock_gettime(2) 来检索总进程 CPU 时间和两个线程消耗的 per-thread CPU 时间。

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);
}

输出:

$ ./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

范例2:下面的程序创建一个进程,然后使用 clock_gettime(2) 来检索总进程 CPU 时间和两个进程消耗的父子进程 CPU 时间。

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;
}

输出:

[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




相关用法


注:本文由纯净天空筛选整理自siddheshsagar31082001大神的英文原创作品 pthread_getcpuclockid() function in C with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。