2012-05-05 17 views
1

スレッドとメモリを使用してページングアルゴリズムを実装しようとしています。コードを実行すると、最初の2つのスレッドだけが実行され、3番目のスレッドは実行されません。私のCプログラムで何が問題になっています

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

私の3番目のスレッドが実行されていない理由を誰かが教えてくれれば、非常に役に立ちます。

#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

私は3番目のスレッドt3に何か問題があると推測しています。または、メインのスレッドを宣言したときなどです。 –

答えて

3

私のプログラムを実行すると、3番目のスレッドが正常に実行されます。 t3()の出力の最後に"\n"を追加しよう:

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

行の末尾には改行がない場合は、お使いの端末がそれを表示しないことがあります。

+0

ありがとう!それは働いた:) –

関連する問題