17
Emmanuel Alejandro García Solís - 1450138 Maximiliano Hernández Castillo - 1453557 Adán de Jesús Silva Cuéllar - 1462847 Team OS-Project-EVA http://os-projecteva.blogsp ot.com/ Operating Systems First Practical Assignment

Os Practical Assignment 1

Embed Size (px)

Citation preview

Page 1: Os Practical Assignment 1

Emmanuel Alejandro García Solís - 1450138 Maximiliano Hernández Castillo - 1453557Adán de Jesús Silva Cuéllar - 1462847

Team OS-Project-EVA

http://os-projecteva.blogspot.com/

Operating SystemsFirst Practical Assignment

Page 2: Os Practical Assignment 1

Lock::Lock(const char* debugName) {    name = debugName;    semaphore = new Semaphore("lock", 1);    lockHolder = NULL;}

Lock::~Lock() {    delete semaphore;}

void Lock::Acquire() {    semaphore->P();    lockHolder = currentThread;}

Implementation of Lockssynch.cc synch.h

class Lock { public:

Lock(const char*debugName); ~Lock(); void Acquire(); void Release(); bool IsHeldByCurrentThread() bool tryAcquire();

private: const char* name;Thread *lockHolder;

Semaphore *semaphore;};

Page 3: Os Practical Assignment 1

Implementation of Lockssynch.cc synch.h

class Lock { public:

Lock(const char*debugName); ~Lock(); void Acquire(); void Release(); bool IsHeldByCurrentThread() bool tryAcquire();

private: const char* name;Thread *lockHolder;

Semaphore *semaphore;};

bool Lock::IsHeldByCurrentThread(){    return lockHolder == currentThread;}

void Lock::Release() {    ASSERT(IsHeldByCurrentThread());    lockHolder = NULL;    semaphore->V();}

bool Lock::tryAcquire(){ if(lockHolder == NULL){ semaphore->P(); lockHolder = currentThread; return true; } else{

return false; }}

/*Almost the same than Acquire(), except that, if someone has the lock, it will return immediately instead of waiting*/

Page 4: Os Practical Assignment 1

Implementation of C. V.synch.cc

Condition::Condition(const char* debugName, Lock* conditionLock){     name = debugName;    waitQueue = new List<Semaphore *>;}

void Condition::Wait(Lock *conditionLock) {    Semaphore *waiter;    ASSERT(conditionLock>IsHeldByCurrentThread()); waiter = new Semaphore("condition", 0);    waitQueue->Append(waiter);    conditionLock->Release();    waiter->P();    conditionLock->Acquire();    delete waiter; }

/*Queue containing allocatedSemaphores for each waiting thread*/

/*1. Checks if the current thread has the lock.2. Creates a semaphore, initially 0.3. Adds the semaphore to the queue.4. Releases the lock, and goes to sleep until someone sends a Signal.5. When someone sends a Signal, reaquires the lock and deletes the semaphore*/

Page 5: Os Practical Assignment 1

Implementation of C. V.synch.cc

void Condition::Signal(Lock *conditionLock) {    Semaphore *waiter;    ASSERT(conditionLock->IsHeldByCurrentThread());

    if(!waitQueue->IsEmpty()){        waiter = waitQueue->Remove();        waiter->V();    }}void Condition::Broadcast(Lock *conditionLock) {    while(!waitQueue->IsEmpty()){        Signal(conditionLock);    }}

/*Checks if the the current thread has the lock*/

/*If it’s true, removes the last semaphore from the wait queue, assigns it to the waiter semaphore, and calls V, to wake him up*/

/* Wake up all threads waiting on this condition until the wait queue is empty*/

Page 6: Os Practical Assignment 1

Dining philosophers

Page 7: Os Practical Assignment 1

Dining philosophers

In computer science, the dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.

Page 8: Os Practical Assignment 1
Page 9: Os Practical Assignment 1

Deadlock

Refers to a specific condition when two or more processes are each waiting for the other to release a resource, or more than two processes are waiting for resources in a circular chain.

Page 10: Os Practical Assignment 1
Page 11: Os Practical Assignment 1

some possible solutions

Page 12: Os Practical Assignment 1

By cyclic turn

By Several turns.Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, hegives his token to the next philosopher at his right.

Page 13: Os Practical Assignment 1
Page 14: Os Practical Assignment 1

By Several turns

Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, hegives his token to the next philosopher at his right.

Page 15: Os Practical Assignment 1
Page 16: Os Practical Assignment 1
Page 17: Os Practical Assignment 1

Gracias por su atencion

:)