📜  C 语言中的 pthread_getcpuclockid()函数和示例

📅  最后修改于: 2022-05-13 01:55:22.994000             🧑  作者: Mango

C 语言中的 pthread_getcpuclockid()函数和示例

pthread_getcpuclockid()函数从指定线程返回 CPU 时间时钟的时钟 ID。

句法:

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

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

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

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

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

C
// C program to implement
// the above approach
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
  
#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);
}


C
// C program to implement
// the above approach
#include 
#include 
#include 
  
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;
}


输出:

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

C

// C program to implement
// the above approach
#include 
#include 
#include 
  
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;
}

输出: