简体   繁体   中英

Time measuring in Synchronized threaded C language

The time measuring in thread_work function is not working.
Code is a little bit nasty but I just want you to look at the thread_work function and teach me why the print_time function keeps generating 0 value.

(I write the whole code just in case, I'm sorry for your eyes, really)

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

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

Could be that the three methods execute so fast that the system clock doesn't progress. You could try and get a higher solution by changing CLOCK_REALTIME to CLOCK_THREAD_CPUTIME_ID or CLOCK_PROCESS_CPUTIME_ID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM