Diferencia entre revisiones de «Solución ejercicio 7»

De Wiki de Sistemas Operativos
Saltar a: navegación, buscar
(correcion paréntesis)
 
Línea 79: Línea 79:
 
   down(s1);              //#2
 
   down(s1);              //#2
 
   f();                    //#3
 
   f();                    //#3
   if(get.s2 == 0)        //#4
+
   if(get(s2) == 0)        //#4
 
     up(s2);              //#5
 
     up(s2);              //#5
 
}
 
}
Línea 89: Línea 89:
 
   down(s2);              //#2
 
   down(s2);              //#2
 
   f();                    //#3
 
   f();                    //#3
   if(get.s1 == 0)        //#4
+
   if(get(s1) == 0)        //#4
 
     up(s1);              //#5
 
     up(s1);              //#5
 
}
 
}
 
</source>
 
</source>

Revisión actual del 14:25 15 dic 2011

Hilo h1

for (int i = 0; i<2; i++){ //#1
   while(cerrojo_p2);      //#2
      cerrojo_p1 = 1;      //#3
      f();                 //#4
      cerrojo_p1 = 0;      //#5
 
}

Hilo h2

for (int i = 0; i<2; i++){ //#1
   while(cerrojo_p1);      //#2
      cerrojo_p2 = 1;      //#3
      f();                 //#4
      cerrojo_p2 = 0;      //#5
 
}
     > = Fin de su ejecucion


    | #1| #2| #3|   |   |   | #4| #5| #1|   |   |   | #2| #2| #2|   |   |   | #2| #3| #4|   |   |   | #5| #1|   |
  h1|---|---|---|   |   |   |---|---|---|   |   |   |---|---|---|   |   |   |---|---|---|   |   |   |---|--->   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   | #1| #2| #2|   |   |   | #2| #3| #4|   |   |   | #5| #1| #2|   |   |   | #3| #4| #5|   |   | #1|
  h2|   |   |   |---|---|---|   |   |   |---|---|---|   |   |   |---|---|---|   |   |   |---|---|---|   |   |--->
    |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27
    Nota: La solución que estaba antes propuesta no tenia relación con el ejercicio 7.Esta solucion todavía no ha sido revisada por Pablo.

jesgonbel Esta es la solución que he obtenido al cambiar los cerrojos por semáforos:

Inicialmente s1=1 y s2=0

Hilo 1:

for (int i = 0; i<2; i++){ //#1
   down(s1);               //#2
   f();                    //#3
   up(s2);                 //#4
}

Hilo 2:

for (int i = 0; i<2; i++){ //#1
   down(s2);               //#2
   f();                    //#3
   up(s1);                 //#4
}
          X: El proceso es bloqueado por el semáforo.
          /: El proceso pasa a estado preparado.
          >: El proceso termina su ejecución.


    | #1| #2| #3|   |   | #4| #1| #2|   |   |   | #3| #4| #1|   |   |   |   |
  h1|---|---|---|   |   |---|---|---X   |   /   |---|---|--->   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   | #1| #2|   |   |   | #3| #4| #1|   |   |   | #2| #3| #4| #1|
  h2|   |   |   |---|---X   /   |   |---|---|---|   |   |   |---|---|---|---|
    |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18


antmorzam Yo a tu solución le haría una modificación como la siguiente:

Hilo 1:

for (int i = 0; i<2; i++){ //#1
   down(s1);               //#2
   f();                    //#3
   if(get(s2) == 0)         //#4
     up(s2);               //#5
}

Hilo 2:

for (int i = 0; i<2; i++){ //#1
   down(s2);               //#2
   f();                    //#3
   if(get(s1) == 0)         //#4
     up(s1);               //#5
}