Operating systems

      Carmen Suárez
       Abraham Silva
         Gaby García
Problem unbounded-bufer (producer-
                consumer)
Producer and Consumer share a common buffer
But not everything is happiness and perfection...
The producer tries to get to the buffer and add
elements but it’s already full
The consumer tries to get to the buffer but it’s
empty
When the producer and the consumer go to sleep, and don't
receive calls from anyone. A deadlock occurs




           Producer                  Consumer
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
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);
}                              }
}
http://bit.ly/rlGtNe
Dining Philosophers
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
}
}
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
Eliminate deadlocks


•   TryAcquire
•   Release all the locks, wait and then
    reacquire them in the right order
•   Order the locks: Establish a time for locks
Solution
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.
Optimal algorithm

•   The lock is released in less than the suspend and resume
    time, the process spins until it acquires the lock.
Optimal Algorithm


•   The lock is released in more than the suspend and resume
    time, the process should suspend immediately.
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.
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
Gracias por su atencion 

Blog:
        http://os-sistemasoperativos.blogspot.com

Operating systems

  • 1.
    Operating systems Carmen Suárez Abraham Silva Gaby García
  • 2.
    Problem unbounded-bufer (producer- consumer) Producer and Consumer share a common buffer But not everything is happiness and perfection...
  • 3.
    The producer triesto get to the buffer and add elements but it’s already full
  • 4.
    The consumer triesto get to the buffer but it’s empty
  • 5.
    When the producerand the consumer go to sleep, and don't receive calls from anyone. A deadlock occurs Producer Consumer
  • 6.
    The solution tothis 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 typedefint 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); } } }
  • 8.
  • 9.
  • 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
  • 13.
  • 14.
    Suspension algorithms There arethree 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 algorithmthan 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 suatencion  Blog: http://os-sistemasoperativos.blogspot.com