2013-05-08 44 views
0

中的時間測量thread_work函數不起作用。
代碼有點討厭,但我只想讓你看看thread_work函數 並教我爲什麼print_time函數保持生成0值。同步線程C語言中的時間測量

(我寫的整個代碼以防萬一,我對你的眼睛對不起,真的)

#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

你調試了你的代碼嗎? – 2013-05-08 11:23:44

+0

是的。和代碼工作很好,除了print_time(t2)在thread_work – leehoyoung 2013-05-08 11:25:50

+0

我在窗口的CMD(使用minGW)在下面像它一樣gcc -oa ac -lpthread – leehoyoung 2013-05-08 11:32:59

回答

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以獲得更高級的解決方案。

+0

**非常感謝** – leehoyoung 2013-05-08 11:42:11