5. When the producer and the consumer go to sleep, and don't
receive calls from anyone. A deadlock occurs
Producer Consumer
6. The solution to this problem is:
The use of semaphores or locks. A semaphore will regulate
the production and the consuming of the buffer, giving
access to only one process at a time.
http://bit.ly/rlGtNe
7. Pseudocode
define N 100
typedef int semaphore;
semaphore mutex = 1; void consumer(void){
semaphore empaty = N; int element:
semaphore full = 0; while(true){
void producer(void){ down(full);
int element; down(mutex);
while(true){ element=down_element();
element = produce_element();
down(empaty);
up(mutex);
down(full); up(empaty);
insert_element(element); consum_element(element)-,
up(mutex); }
up(full);
} }
}
10. First
"possible" solution
#define N 5
void filosofo(int i)
{
while(TRUE){
• think
• take left fork
• take right fork
• eat
• leave left fork
• leave right fork
}
}
11. Deadlock
• All of the philosophers are holding a fork
while waiting for other fork to be released.
• There are no forks available anymore.
• They can’t eat because they need two forks.
• Starvation
12. Eliminate deadlocks
• TryAcquire
• Release all the locks, wait and then
reacquire them in the right order
• Order the locks: Establish a time for locks
14. Suspension algorithms
There are three components of the time used in a process:
• Spinning: It’s the time the thread takes to check whether
the lock is available or not. During this time the process is
not executing.
• Suspending: Is the amount of time the thread is
suspended or sleeping, waiting for the lock to be available
• Resuming: Is the amount of time the thread takes to
restart the execution after acquiring the lock.
15. Optimal algorithm
• The lock is released in less than the suspend and resume
time, the process spins until it acquires the lock.
16. Optimal Algorithm
• The lock is released in more than the suspend and resume
time, the process should suspend immediately.
17. Other worse algorithm than SR
• if it spins for more than suspend and resume time, then suspend. The
worst case is when lock is free after a thread start the suspension. The
SR algorithm will cost the suspend and resume time but will spin less time
than this algorithm.
18. Bibliography
• Martin Rinard OS Notes http://people.csail.mit.edu/rinard/osnotes/
• Sistemas Operativos Modernos, Andrew S. Tanenbaum, 3rd Ed,
Prentice Hall.
• Dining philosophers image
HTTP://UPLOAD.WIKIMEDIA.ORG/WIKIPEDIA/COMMONS/THUMB/6/6A/DINING_ PHIL
OSOPHERS.PNG/200PX-DINING_PHILOSOPHERS.PNG
• Deadlock image
http://www.cs.rpi.edu/academics/courses/fall04/os/c10/deadlock2.gif
19. Gracias por su atencion
Blog:
http://os-sistemasoperativos.blogspot.com