2012-05-05 25 views
1

我試圖使用線程實現內存和不同的分頁算法。現在,當我運行我的代碼時,只有前兩個線程運行,而不是第三個線程。這是它打印的內容:我的C程序出了什麼問題

Thread one: Task 1, Sequence 1 
Thread two: Task 1, Sequence 2 

如果有人能告訴我爲什麼我的第三個線程沒有運行,那將會非常有幫助。

#include<stdio.h> 
#include<stdlib.h> 
#include<pthread.h> 

pthread_mutex_t MemoryLock; 
int k = 10; 
int m = 10; 
int n = 1000; 
int seq = 0; 
int start = 0; //for thread 2 
int end = 10; //end is equal to value of k for thread 2 

int disk[1000]; 

int MemoryLookupTable[10]; //Stores which variable is present in memory 
int PhysicalMemory[10]; //Stores the value of the variables 
int MetaTable[10]; //Meta level information 

void least_recent(int i){ 
    int t, position, smallest, disk_num; 

    t = 1; 
    smallest = MetaTable[0]; 
    position = 0; 

    while(t < m){ 
     if(smallest > MetaTable[t]){ 
      smallest = MetaTable[t]; 
      position = t; 
     } 
     t++; 
    } 

    disk_num = MemoryLookupTable[position]; 
    disk[disk_num] = PhysicalMemory[position]; 

    PhysicalMemory[position] = disk[i]; 
    MemoryLookupTable[position] = i; 
    MetaTable[position] = seq; 
} 

void most_recent(int i){ 
    int t, position, largest, disk_num; 

    t = 1; 
    largest = MetaTable[0]; 
    position = 0; 

    while(t < m){ 
     if(largest < MetaTable[t]){ 
      largest = MetaTable[t]; 
      position = t; 
     } 
     t++; 
    } 

    disk_num = MemoryLookupTable[position]; 
    disk[disk_num] = PhysicalMemory[position]; 

    PhysicalMemory[position] = disk[i]; 
    MemoryLookupTable[position] = i; 
    MetaTable[position] = seq; 
} 

void random_order(int i){ 
    int r, disk_num; 
    r = m * (rand()/(RAND_MAX + 1.0)); 

    disk_num = MemoryLookupTable[r]; 
    disk[disk_num] = PhysicalMemory[r]; 

    PhysicalMemory[r] = disk[i]; 
    MemoryLookupTable[r] = i; 
} 

int fetch(int i){ 
    int x = 0; 
    int x_i; 

    x_i = disk[i]; 
    while(x < m){ 
     if(x_i == PhysicalMemory[x]) 
      return x_i; 
     x++; 
    } 

    return -1; 
} 

void PageIn(int i){ 
    int x = 0; 
    int present; 

    present = fetch(i); 
    if(present == -1) 
     return; //Do not need to page in, since variable is already there. 

    least_recent(i); 
    //most_recent(i); I am testing each algorithm at a time, so these are commented out. 
    //random_order(i); 
} 

void *t1(){ 
    int sum, task, r, i, fetched; 
    sum, task = 0; 
    r = rand() % (n - k) + k; 

    for(i = 0; i < k - 1; i++){ 
     pthread_mutex_lock(&MemoryLock); 
     seq++; 
     fetched = fetch(i); 
     if(fetched == -1){ 
      PageIn(i); 
      pthread_mutex_unlock(&MemoryLock); 
      if(i = (k - 2)) 
      break; 
     } 
     sum = sum + disk[i]; 
     pthread_mutex_unlock(&MemoryLock); 
    } 

    sum = sum + disk[r]; 
    task++; 
    printf("\nThread one: Task %d, Sequence %d", task, seq); 

    return NULL; 
} 

void *t2(){ 
    int sum, task, i, fetched; 
    sum, task = 0; 

    for(i = start; i < end; i++){ 
     pthread_mutex_lock(&MemoryLock); 
     seq++; 
     fetched = fetch(i); 
     if(fetched == -1){ 
      PageIn(i); 
      pthread_mutex_unlock(&MemoryLock); 
      if(i = (end - 1)){ 
      sum = sum + disk[i]; 
      break; 
      } 
     } 
     sum = sum + disk[i]; 
     pthread_mutex_unlock(&MemoryLock); 
    } 

    sum = sum + disk[i]; 
    task++; 
    start++; 
    if(end == n){ 
     start = 0; 
     end = k; 
    } 
    else 
     end++; 
    printf("\nThread two: Task %d, Sequence %d", task, seq); 

    return NULL; 
} 

void *t3(){ 
    int sum, task, r, i, fetched; 

    for(i = 0; i < k; i++){ 
     pthread_mutex_lock(&MemoryLock); 
     seq++; 
     r = n * (rand()/(RAND_MAX + 1.0)); 
     fetched = fetch(r); 
     if(fetched == -1){ 
      PageIn(r); 
      pthread_mutex_unlock(&MemoryLock); 
      if(i = (k - 1)){ 
      sum = sum + disk[r]; 
      break; 
      } 
     } 
     sum = sum + disk[r]; 
     pthread_mutex_unlock(&MemoryLock); 
    } 

    sum = sum + disk[r]; 
    task++; 
    printf("\nThread three: Task %d, Sequence %d", task, seq); 

    return NULL; 
} 

main(){ 
    int pt1, pt2, pt3, i, j, randNum; 
    pthread_t thread1, thread2, thread3; 

    for(i = 0; i < n; i++){ 
     randNum = 200 * (rand()/(RAND_MAX + 1.0)); 
     disk[i] = randNum; 
    } 

    for(j = 0; j < m; j++) //initializing array to empty 
     MemoryLookupTable[j] = -1; 

    if((pt1 = pthread_create(&thread1, NULL, t1, NULL))) 
     printf("Thread creation failed: %d\n", pt1); 

    if((pt2 = pthread_create(&thread2, NULL, t2, NULL))) 
     printf("Thread creation failed: %d\n", pt2); 

    if((pt3 = pthread_create(&thread3, NULL, t3, NULL))) 
     printf("Thread creation failed: %d\n", pt3); 

    pthread_join(thread1, NULL); 
    pthread_join(thread2, NULL); 
    pthread_join(thread3, NULL); 

    pthread_exit(0); 
} 
+5

這是相當多的代碼來通讀。你可以嘗試通過刪除代碼來隔離問題,直到你有一個更小的測試用例嗎? – jjrv

+3

當你有一個複雜的程序,通常是一個好主意,使程序的副本,並通過消除一切不相關的問題,只是有剩餘重現此問題的幾行簡化它。那麼你(和我們)只能集中代碼的相關部分。 –

+0

我猜有什麼不對我的第三個線程,這是T3。或者當我在main中聲明線程時。 –

回答

3

我運行程序,第三個線程執行成功。嘗試添加"\n"t3()的輸出端:

printf("\nThread three: Task %d, Sequence %d\n", task, seq); 

如果在一行的末尾沒有換行符,你的終端可能無法顯示。

+0

謝謝!這工作:) –