用同步线程C语言测量时间

时间:2013-05-08 11:17:14

标签: c

thread_work函数中的时间测量不起作用 代码有点讨厌但我只是想让你看一下thread_work函数 并教我为什么 print_time 函数不断生成0值。

(为了以防万一我写完整个代码,我很抱歉你的眼睛,真的)

#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#define num_thread 20

char str[11];                               
void *thread_work(void *tid);               
void generate_str(int n);                   
void str_sort(int n);                       
void check_sort(void);
void print_time(struct timespec *myclock);         
void print_time_start(struct timespec *myclock);  
void print_time_end(struct timespec *myclock);
sem_t my_sem;

int main(void)
{
    pthread_t tid[num_thread];
    int ret; 
    int t;
    struct timespec t1[2];
    srand(time(NULL));    
    ret = sem_init(&my_sem, 0, 1);
    clock_gettime(CLOCK_REALTIME, &t1[0]);
    print_time_start(t1);   

    for(t=0; t<num_thread; t++)
    {
        ret = pthread_create(&tid[t], NULL, thread_work, (void *)t);
        usleep(1);
    }
    for(t=0; t<num_thread; t++)
        ret = pthread_join(tid[t], NULL);

    clock_gettime(CLOCK_REALTIME, &t1[1]);
    print_time_end(t1);

    sem_destroy(&my_sem);
    return 0;
}

void *thread_work(void *t)
{
    int n = (int )t;
    struct timespec t2[2]; 
    printf("########## Thread #%2d starting ########## \n",n);
    sleep(1);
    sem_wait(&my_sem);  //Entry Section

    clock_gettime(CLOCK_REALTIME, &t2[0]);    //Critical Section Start  
    generate_str(n);
    str_sort(n);
    check_sort();
    clock_gettime(CLOCK_REALTIME, &t2[1]);
    print_time(t2);              //Critical Section End

    sem_post(&my_sem);  //Exit Section
}

void str_sort(int n)
{
    int temp;
    int i, j;

    for(i=0; i<9; i++)
        for(j=0; j<9-i; j++)
        {
            if(str[j]>str[j+1])
            {
                temp=str[j];
                str[j]=str[j+1];
                str[j+1]=temp;
            }
        }
    printf("[%2d] ",n);
    for(i=0; i<10; i++)
        printf("%2c", str[i]);
}

void generate_str(int n)
{
    int i;
    int num;
    srand(n);       //differentiate the string of each threads
    for(i=0; i<10; i++)
    {
        num = (97+rand()%26);
        str[i]=num;
    }

    str[10]='\0';
}

void check_sort(void)
{
        int i;
    int count=0;
    for(i=0; i<9; i++)
    {
        if(str[i]>str[i+1])
            count++;
    }
    if(count != 0)
        printf(" [X]FALSE ");
    else
        printf(" [O]TRUE ");
}

void print_time(struct timespec *myclock)
{
    long delay, temp, temp_n, sec;
    sec = myclock[0].tv_sec % 60;
    printf(" %ld.%ld -> ", sec, myclock[0].tv_nsec);
    sec = myclock[1].tv_sec % 60;
    printf("%ld.%ld", sec, myclock[1].tv_nsec);
    if(myclock[1].tv_nsec >= myclock[0].tv_nsec) 
    { 
        temp = myclock[1].tv_sec - myclock[0].tv_sec; 
        temp_n = myclock[1].tv_nsec - myclock[0].tv_nsec; 
        delay = 1000000000 * temp + temp_n; 
    }
    else 
    { 
        temp = myclock[1].tv_sec - myclock[0].tv_sec - 1; 
        temp_n = 1000000000 + myclock[1].tv_nsec - myclock[0].tv_nsec; 
        delay = 1000000000 * temp + temp_n; 
    } 
    printf(", Interval : %ld ns\n", delay);
}

void print_time_start(struct timespec *myclock)
{
    long sec;
    sec = myclock[0].tv_sec % 60;
    printf("########## Thread: Start Time -> %ld.%ld\n", sec, myclock[0].tv_nsec);
}

void print_time_end(struct timespec *myclock)
{
    long delay, temp, temp_n, sec;
    sec = myclock[1].tv_sec % 60;
    printf("########## Thread: End Time -> %ld.%ld   ", sec, myclock[1].tv_nsec);

    if (myclock[1].tv_nsec >= myclock[0].tv_nsec) 
    { 
        temp = myclock[1].tv_sec - myclock[0].tv_sec; 
        temp_n = myclock[1].tv_nsec - myclock[0].tv_nsec; 
        delay = 1000000000 * temp + temp_n; //The unit of delay is nano second
    }

    else 
    { 
        temp = myclock[1].tv_sec - myclock[0].tv_sec - 1; 
        temp_n = 1000000000 + myclock[1].tv_nsec - myclock[0].tv_nsec; 
        delay = 1000000000 * temp + temp_n; //The unit of delay is nano second
    }  

    delay = delay / 1000;    //The unit of delay is now micro second
    printf("(Thread Execution Time -> %ld micro second)\n", delay);

    }

1 个答案:

答案 0 :(得分:1)

clock_gettime(CLOCK_REALTIME, &t2[0]);    //Critical Section Start  
generate_str(n);
str_sort(n);
check_sort();
clock_gettime(CLOCK_REALTIME, &t2[1]);

可能是这三种方法执行速度太快,系统时钟没有进展。您可以尝试通过将CLOCK_REALTIME更改为CLOCK_THREAD_CPUTIME_IDCLOCK_PROCESS_CPUTIME_ID来获得更高的解决方案。