os practical assignment 1
TRANSCRIPT
![Page 1: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/6.jpg)
Dining philosophers
![Page 7: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/8.jpg)
![Page 9: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/10.jpg)
![Page 11: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/11.jpg)
some possible solutions
![Page 12: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/13.jpg)
![Page 14: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/15.jpg)
![Page 16: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/16.jpg)
![Page 17: Os Practical Assignment 1](https://reader035.vdocument.in/reader035/viewer/2022062312/5563a337d8b42a2b6a8b5072/html5/thumbnails/17.jpg)
Gracias por su atencion
:)