Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use warnings;
- use strict;
- use threads;
- use threads::shared;
- use Thread::Semaphore;
- use Time::HiRes qw(time usleep);
- my $TBAS= 100_000;
- my $FMIC= 1;
- my $FMIO= 1;
- my $FMAC= 2;
- my $TMIC= $TBAS*$FMIC;
- my $TMAC= $TBAS*$FMAC;
- my $prob= (shift||20);
- my $nsim= (shift||40);
- my $TOTAL :shared= 4; #máximo de clientes que caben en la barbería.
- my $sillon :shared= 0; #sillón del barbero.
- my $clientesEnBarberia :shared= 0;
- my $sillasClientes :shared= 0;
- my $mutex= Thread::Semaphore->new();
- my $sillonBarbero= Thread::Semaphore->new();
- my $barberoDormido= Thread::Semaphore->new(0);
- my $clienteEsperando= Thread::Semaphore->new(0);
- my $clienteConBarbero= Thread::Semaphore->new(0);
- my $clienteAtendido= Thread::Semaphore->new(0);
- my $clienteSeVa= Thread::Semaphore->new(0);
- threads->create(\&Barbero);
- for my $i (1..$nsim) {
- threads->create(\&Cliente,$i);
- }
- while (threads->list()){
- my @esperables= threads->list(threads::joinable);
- if (@esperables){
- $_->join for @esperables;
- }
- }
- sub Barbero {
- print "Se abre la barberia\n";
- $mutex->down();
- while (1) {
- if ($TOTAL == 0) { #no hay clientes, el barbero se duerme.
- &Dormir();
- }
- while ($clientesEnBarberia > 0) {
- $mutex->up();
- $clienteConBarbero->down();
- &SeAtiendeCliente();
- $clienteAtendido->up();
- $clienteSeVa->down();
- $mutex->down();
- }
- }
- $mutex->up();
- }
- sub SeAtiendeCliente {
- print "--> Barbero atendiendo cliente $sillon\n";
- usleep (&alea($TMIC, $TMAC));
- $clienteConBarbero->up();
- }
- sub Dormir {
- print "***Barbero se va a dormir***\n";
- $sillonBarbero->down();
- $sillon= -1;
- $mutex->up(); #soltamos el mutex antes de dormir.
- $barberoDormido->down();
- $mutex->down();
- &LiberaSillon();
- }
- sub LiberaSillon {
- $sillonBarbero->up();
- $sillon= 0;
- }
- print "aquí pasa el dormir\n";
- sub Cliente {
- my ($c)= @_; #creamos una lista de clientes.
- $mutex->down();
- print "-->Entra el cliente $c en la barberia\n";
- if ($clientesEnBarberia >= $TOTAL) { #comprueba que esta llena la barberia.
- print "-->La Barberia esta llena, el cliente $c ha entrado y se ha ido.\n";
- }
- $mutex->up();
- $mutex->down();
- if ($clientesEnBarberia < $TOTAL) {
- $clientesEnBarberia++;
- print "--Hay $c clientes en la barberia\n"
- }
- if ($sillon > 0) {
- $sillasClientes++;
- print "Hay $sillasClientes clientes sentados en sillas.\n";
- $mutex->up();
- $clienteEsperando->down();
- $mutex->up();
- $sillasClientes--;
- } elsif ($sillon < 0) {
- $barberoDormido->up();
- print "<--No hay clientes, el barbero se va a dormir.\n";
- }
- $mutex->up();
- $sillonBarbero->down();
- $mutex->down();
- $sillon= $c;
- $clienteConBarbero->up();
- $mutex->up();
- $clienteAtendido->down();
- $mutex->down();
- &LiberaSillon();
- $clienteEsperando->up() if ($sillasClientes);
- $clientesEnBarberia--;
- $clienteSeVa->up();
- print "<--Cliente $c se va.\n";
- $mutex->up();
- }
- $mutex->up();
- print "Se cierra la barberia.\n";
- exit(0);
- sub alea{
- (my $min, my $max)= @_;
- return ($min+int(rand($max-$min+1)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement