1

スレッドを使用してメモリとさまざまなページング アルゴリズムを実装しようとしています。コードを実行すると、最初の 2 つのスレッドのみが実行され、3 番目のスレッドは実行されません。これはそれが印刷するものです:

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); 
}
4

1 に答える 1

3

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

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

行末に改行がない場合は、端末に表示されない場合があります。

于 2012-05-05T20:20:42.070 に答える