Sistemas Distribuídos

2° Trabalho 

 

Erik Tronkos

Pedro Boueke

UFRJ - 2016.05.19

Linguagem

  • Orientação a Objetos
  • Baixo nível (veloz)
  • Compilada
  • Portável

Spinlock

s

Time & Memory

#define MEMORY_SIZE 5000000000

chronos::high_resolution_clock::now()

pthread_t *threads = new pthread_t[k];

if (N > (long)MEMORY_SIZE)

          arr_size = CHUNK_SIZE;      chunks = an/(arr_size+1) + 1;

int8_t* arr = new int8_t[arr_size];

Threads

for (curr_chunk; curr_chunk < chunks; ++curr_chunk) 

       // fill numbers array up to index arr_size

       long block = n / k; range[] = {0, block};

       for each argument

             // pass range to argument and increment it

         pthread_create(&threads[i], NULL, partialSum, (void *) arguments[i]);

struct Arguments

    int8_t* arr; 

    long* range;

    long* distributed_sum;

    Lock *lock;

Arguments** arguments = new Arguments* [k];

Partial Sum

void* partialSum (void *arguments)

// for i in range...

           partial_sum += (long) obj->arr[i];

acquire(obj->lock);

*obj->distributed_sum += partial_sum;​

release(obj->lock);

void acquire(struct Lock *spinlock)

        while(__sync_lock_test_and_set(&spinlock->held, 1));

void release (struct Lock *spinlock)

          spinlock->held = 0;

Semáforo

Producer

 while(produced < max_production) {
        srand(((int) time(NULL)) * produced);
        produce(id); }

Produce

lock(mutex);

product = (rand() % (10000000))+1

is_not_full.wait

     products.push(product);
     ++produced;

     is_not_empty.notifyAll();

Consumer

 while(consumed < max_production)
    {
        consume(id);
    }

Consume

lock(mutex);
is_not_empty.wait 

    product = products.top();
    products.pop();
    ++consumed;

    is_not_full.notify_all();

Trabalho 2 SD - UFRJ

By Pedro Boueke

Trabalho 2 SD - UFRJ

  • 397