Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use warnings;
  4. use strict;
  5. use threads;
  6. use threads::shared;
  7. use Thread::Semaphore;
  8. use Time::HiRes qw(time usleep);
  9.  
  10. my $TBAS= 100_000;
  11. my $FMIC= 1;
  12. my $FMIO= 1;
  13. my $FMAC= 2;
  14.  
  15. my $TMIC= $TBAS*$FMIC;
  16. my $TMAC= $TBAS*$FMAC;
  17.  
  18. my $prob= (shift||20);
  19. my $nsim= (shift||40);
  20.  
  21. my $TOTAL :shared= 4; #máximo de clientes que caben en la barbería.
  22. my $sillon :shared= 0; #sillón del barbero.
  23. my $clientesEnBarberia :shared= 0;
  24. my $sillasClientes :shared= 0;
  25.  
  26. my $mutex= Thread::Semaphore->new();
  27. my $sillonBarbero= Thread::Semaphore->new();
  28. my $barberoDormido= Thread::Semaphore->new(0);
  29. my $clienteEsperando= Thread::Semaphore->new(0);
  30. my $clienteConBarbero= Thread::Semaphore->new(0);
  31. my $clienteAtendido= Thread::Semaphore->new(0);
  32. my $clienteSeVa= Thread::Semaphore->new(0);
  33.  
  34. threads->create(\&Barbero);
  35. for my $i (1..$nsim) {
  36. threads->create(\&Cliente,$i);
  37. }
  38.  
  39. while (threads->list()){
  40. my @esperables= threads->list(threads::joinable);
  41. if (@esperables){
  42. $_->join for @esperables;
  43. }
  44. }
  45.  
  46. sub Barbero {
  47. print "Se abre la barberia\n";
  48. $mutex->down();
  49. while (1) {
  50. if ($TOTAL == 0) { #no hay clientes, el barbero se duerme.
  51. &Dormir();
  52. }
  53. while ($clientesEnBarberia > 0) {
  54. $mutex->up();
  55. $clienteConBarbero->down();
  56. &SeAtiendeCliente();
  57. $clienteAtendido->up();
  58. $clienteSeVa->down();
  59. $mutex->down();
  60. }
  61. }
  62. $mutex->up();
  63. }
  64.  
  65. sub SeAtiendeCliente {
  66. print "--> Barbero atendiendo cliente $sillon\n";
  67. usleep (&alea($TMIC, $TMAC));
  68. $clienteConBarbero->up();
  69. }
  70.  
  71. sub Dormir {
  72. print "***Barbero se va a dormir***\n";
  73. $sillonBarbero->down();
  74. $sillon= -1;
  75. $mutex->up(); #soltamos el mutex antes de dormir.
  76. $barberoDormido->down();
  77. $mutex->down();
  78. &LiberaSillon();
  79. }
  80.  
  81. sub LiberaSillon {
  82. $sillonBarbero->up();
  83. $sillon= 0;
  84. }
  85.  
  86. print "aquí pasa el dormir\n";
  87.  
  88. sub Cliente {
  89. my ($c)= @_; #creamos una lista de clientes.
  90. $mutex->down();
  91. print "-->Entra el cliente $c en la barberia\n";
  92. if ($clientesEnBarberia >= $TOTAL) { #comprueba que esta llena la barberia.
  93. print "-->La Barberia esta llena, el cliente $c ha entrado y se ha ido.\n";
  94. }
  95. $mutex->up();
  96. $mutex->down();
  97. if ($clientesEnBarberia < $TOTAL) {
  98. $clientesEnBarberia++;
  99. print "--Hay $c clientes en la barberia\n"
  100. }
  101. if ($sillon > 0) {
  102. $sillasClientes++;
  103. print "Hay $sillasClientes clientes sentados en sillas.\n";
  104. $mutex->up();
  105. $clienteEsperando->down();
  106. $mutex->up();
  107. $sillasClientes--;
  108. } elsif ($sillon < 0) {
  109. $barberoDormido->up();
  110. print "<--No hay clientes, el barbero se va a dormir.\n";
  111. }
  112. $mutex->up();
  113. $sillonBarbero->down();
  114.  
  115. $mutex->down();
  116. $sillon= $c;
  117. $clienteConBarbero->up();
  118. $mutex->up();
  119. $clienteAtendido->down();
  120.  
  121. $mutex->down();
  122. &LiberaSillon();
  123. $clienteEsperando->up() if ($sillasClientes);
  124. $clientesEnBarberia--;
  125. $clienteSeVa->up();
  126. print "<--Cliente $c se va.\n";
  127. $mutex->up();
  128. }
  129. $mutex->up();
  130.  
  131. print "Se cierra la barberia.\n";
  132. exit(0);
  133.  
  134. sub alea{
  135. (my $min, my $max)= @_;
  136. return ($min+int(rand($max-$min+1)));
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement