1

私はスレッドを勉強している初心者です。共通のカウンターをインクリメントするいくつかのスレッドを開始して、0 から 10000 までカウントするために、os161 との相互排除の問題を解決する宿題があります。同期プリミティブを使用して改善する方法がわかりません。助けてください。

#include <types.h>
#include <lib.h>
#include <test.h>
#include <thread.h>
#include <synch.h>

enum {
    NADDERS = 10,    /* the number of adder threads */
    NADDS   = 10000, /* the number of overall increments to perform */
};

/*
 * **********************************************************************
 * Declare the counter variable that all the adder() threads increment 
 *
 * Declaring it "volatile" instructs the compiler to always (re)read the
 * variable from memory and not optimise by removing memory references
 * and re-using the content of a register.
 */
volatile unsigned long int counter;


/*
 * Declare an array of adder counters to count per-thread
 * increments. These are used for printing statistics.
 */  
unsigned long int adder_counters[NADDERS];


/* We use a semaphore to wait for adder() threads to finish */
struct semaphore *finished;

/*
 * **********************************************************************
 * ADD YOUR OWN VARIABLES HERE AS NEEDED
 * **********************************************************************
 */

/*
 * adder()
 *
 *  Each adder thread simply keeps incrementing the counter until we
 *  hit the max value.
 *
 * **********************************************************************
 * YOU NEED TO INSERT SYNCHRONISATION PRIMITIVES APPROPRIATELY 
 * TO ENSURE COUNTING IS CORRECTLY PERFORMED.
 * **********************************************************************
 *
 * You should not re-write the existing code.
 *
 * * Only the correct number of increments are performed
 * * Ensure x+1 == x+1 
 * * Ensure that the statistics kept match the number of increments
 * * performed.
 *
 *
 */

static void adder(void * unusedpointer, unsigned long addernumber)
{
    unsigned long int a, b;
    int flag = 1;

    /*
     * Avoid unused variable warnings.
     */
    (void) unusedpointer; /* remove this line if variable is used */

    while (flag) {
        /* loop doing increments until we achieve the overall number
           of increments */

        a = counter;

        if (a < NADDS) {

            counter = counter + 1;

            b = counter;

            /* count the number of increments we perform  for statistics */
            adder_counters[addernumber]++;    

            /* check we are getting sane results */
            if (a + 1 != b) {
                kprintf("In thread %ld, %ld + 1 == %ld?\n", addernumber, a, b) ;
            }
        }
        else {
            flag = 0;
        }
    }

    /* signal the main thread we have finished and then exit */
    V(finished);

    thread_exit();
}

/*
 * math()
 *
 * This function:
 *
 * * Initialises the counter variables
 * * Creates a semaphore to wait for adder threads to complete
 * * Starts the define number of adder threads
 * * waits, prints statistics, cleans up, and exits
 */
int maths (int nargs, char ** args)
{
    int index, error;
    unsigned long int sum;

    /*
     * Avoid unused variable warnings.
     */

    (void) nargs;
    (void) args;

    /* create a semaphore to allow main thread to wait on workers */

    finished = sem_create("finished", 0);

    if (finished == NULL) {
        panic("maths: sem create failed");
    }

    /*
     * **********************************************************************
     * INSERT ANY INITIALISATION CODE YOU REQUIRE HERE
     * **********************************************************************
     */


    /*
     * Start NADDERS adder() threads.
     */


    kprintf("Starting %d adder threads\n", NADDERS);

    for (index = 0; index < NADDERS; index++) {

        error = thread_fork("adder thread", &adder, NULL, index, NULL);

        /*
         * panic() on error.
         */

        if (error) {
            panic("adder: thread_fork failed: %s\n", strerror(error));
        }
    }


    /* Wait until the adder threads complete */

    for (index = 0; index < NADDERS; index++) {
        P(finished);
    }

    kprintf("Adder threads performed %ld adds\n", counter);

    /* Print out some statistics */
    sum = 0;
    for (index = 0; index < NADDERS; index++) {
        sum += adder_counters[index];
        kprintf("Adder %d performed %ld increments.\n", 
                index, adder_counters[index]);
    }
    kprintf("The adders performed %ld increments overall\n", sum);

    /*
     * **********************************************************************
     * INSERT ANY CLEANUP CODE YOU REQUIRE HERE 
     * **********************************************************************
     */


    /* clean up the semaphore we allocated earlier */
    sem_destroy(finished);
    return 0;
}
4

3 に答える 3

5

あなたはそのドメインの初心者なので、派手なものは使用しないでください。ミューテックスでカウンターを保護するだけです。

// with static linkage somewhere
pthread_mutex_t countMut = PTHREAD_MUTEX_INITIALIZER;
size_t count = 0;

// in the functions
pthread_mutex_lock(&countMut);
++count;
pthread_mutex_unlock(&countMut);
于 2011-04-02T08:11:14.323 に答える
2

いくつかのマイナーなこと。

  1. これらの値に列挙型を使用しないでください-定義を使用してください-列挙型は、フルーツ、エラータイプなど、同様のタイプのものに使用されます。スレッドの数と増分の数は異なります。

  2. volatileは、何にも違いはありません。単に、読み取りを最適化しないようにコンパイラーに指示するだけです。しかし、あなたはインクリメントしているので、書く前に常に変数を読むつもりです

そして主な質問に。

  1. これに対する最も簡単な解決策は、インターロックされたインクリメントです。GCC本能はそのような機能を提供します
于 2011-04-02T03:37:03.420 に答える
0

カウンターがメモリ内にあり、「揮発性」としてマークされている場合、「カウンター = カウンター + 1」はアトミック操作ではないことに注意してください。そのため、1 つずつ追加する操作は、ある種のミューテックスによって保護する必要があります。

os161 のロック機能を使用して、スレッド間の共有データを保護できます。コードは次のようになります。

// declare a global lock variable so every threads can access it
static struct lock* counter_lock;

// initialize the lock before you fork threads
counter_lock = lock_create("counter lock");

// when each thread tries to access the counter, use lock to protect it
lock_acquire(counter_lock);
counter++;
lock_release(counter_lock);

// destroy the lock after all threads are done
lock_destroy(counter_lock);

もちろん、割り当てとして、自分で /kern/thread/synch.c にこれらのロック インターフェイスを実装する必要があります。

于 2012-03-10T13:09:55.800 に答える